sql server - Calculating cumulative summation using SQL based on Product Number -


i have wrote sql query allocates products customer based on quantity remaining each product in stock.

i.e. if ourqty = 55 , there 70 outstanding orders split between 5 customers (customer 1 = 35, customer 2 = 10, customer 3 = 3, customer 4 = 16, customer 5 = 6) sql query allocate reserved product quantity each customer sequentially until ourqty = 0 or of outstanding orders have been fulfilled.

my sql query:

with allocate       (select rd.ponum                                  ponum,                  od.orderline                              orderline,                  rd.partnum                                partnum,                  p.partdescription                         partdescription,                  od.ordernum                               ordernum,                  rd.ourqty                                 ourqty,                  od.number01                               reserved,                  rd.ourqty - od.number01                   newourqty,                  c.custnum                                 custnum,                  c.name                                    name,                  ( rd.ourqty - sum(od.number01)                                  on (                                    order od.ordernum) ) cumulativesum             dbo.rcvdtl rd (nolock)                  inner join dbo.part p (nolock)                          on rd.partnum = p.partnum                  inner join dbo.orderdtl od (nolock)                          on rd.partnum = od.partnum                  inner join dbo.orderhed oh (nolock)                          on od.ordernum = oh.ordernum                  inner join dbo.customer c (nolock)                          on od.custnum = c.custnum            ( rd.ponum = 73 )                  , ( od.number01 > 0 )                  , ( od.openline = 1 ))  select ponum,         orderline,         partnum,         partdescription,         ordernum,         ourqty,         reserved,         newourqty,         custnum,         name,         cumulativesum + reserved 'left in stock',         case           when ( cumulativesum + reserved ) - reserved > 0 reserved           else ( case                    when ( cumulativesum + reserved ) > 0                    ( cumulativesum + reserved )                    else 0                  end )         end                      allocated    allocate  

this works fine when 'partnum' specified parameter. problem arises when query table without specifying 'partnum' since table contains multiple 'partnums' affects accuracy of 'cumulativesum' used check if (ourqty < cumulativesum) , such prevent allocation of stock not physically exist.

my original idea pass database table 'partnum' parameter given 'ponum' iteratively , merge results 1 dataset on application. unfortunately program using calls sql query cannot pass 'partnum' sql query parameter.

therefore, possible calculate cumulative sum each partnum in table using sql?

i using microsoft sql server 2012


Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -

c# - String.format() DateTime With Arabic culture -