sql server - Alternative way to select row value without using scalar value function in sql -


i have 2 table in sql pricetable , targettable. want price item based on few conditions joining 2 tables. 2 tables are,

pricetable pricetable image

    itemrelation    fromdate    todate  amount     12-pbst 2016-09-30  1900-01-01  85.31     12-bbst 2016-08-08  1900-01-01  95.31     12-mbst 2016-09-08  1900-01-01  15.31     12-tbst 2016-07-20  1900-01-01  65.31 

targettable target table image

   itemid   itemname    periodstart targetquantity     12-pmch meadow chandan  05-01-16    36     12-pmch meadow chandan  06-01-16    36     12-pmgu meadow gulab    04-01-16    36     12-pmlv meadow lavendor 04-01-16    36 


have written scalar valued function price , it's working fine when try retrieve less 10 rows. try pull more 50 records takes long time produce results, scalar function is,

create function [dbo].[gettargetitemprice](@item nvarchar(50),@date  nvarchar(50)) returns integer  begin declare @id integer set @id= (select top(1) amount pricedisctable itemrelation=@item  , @date between fromdate , todate) return @id end 

query price

select     dbo.getdistargetitemprice(itemid,periodstart,t.custaccount) price,     t.targetquantityas qty di_targettransactive t     periodstart between '2017-08-01' , '2017-08-31' ,     t.custaccount='dska-100323' group t.custaccount 


sample output :

price   qty   itemid    targetprice 85.31   1173    12-pbst 99706 95.31   352     12-bbst 33440 15.31   1172    12-mbst 17580 35.31   352     12-sbst 12320 65.31   586     12-tbst 38090 

i tried outer apply query gives me wrong price results, alternative solution or sub query scalar valued function in sql


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -