bmkdubai
asked on
sql query stock aging
Dear Experts
I have a stock receipt table1 (stockid,date,qty,rate) , i wish to get a query
(stockid, balance_qty, age30,age60,age90, age>90) on a particular day. Please try to help rgds Benny
I have a stock receipt table1 (stockid,date,qty,rate) , i wish to get a query
(stockid, balance_qty, age30,age60,age90, age>90) on a particular day. Please try to help rgds Benny
ASKER
i have a stock receipts table which contain.
stockid, date, qty, rate
abc, 01/06/2011, 10, 35.25
bcd, 15/06/2011, 15,32
xyz, 01/7/2011, 25,36
qpr, 30/08/2011, 50, 40
qxy, 08/09/2011, 100 ,42
i wish to get a query on a particular day
stockid, total_stock, age0-30, age-31-60,age-61-90,age>90
abc, 10,0,0,0,10
bcd,15,0,0,0,15
qxy,100,0,100,0,0
means how old is the stock
xyz
qpr
qxy
stockid, date, qty, rate
abc, 01/06/2011, 10, 35.25
bcd, 15/06/2011, 15,32
xyz, 01/7/2011, 25,36
qpr, 30/08/2011, 50, 40
qxy, 08/09/2011, 100 ,42
i wish to get a query on a particular day
stockid, total_stock, age0-30, age-31-60,age-61-90,age>90
abc, 10,0,0,0,10
bcd,15,0,0,0,15
qxy,100,0,100,0,0
means how old is the stock
xyz
qpr
qxy
I would do something like the following:
select stockid, sum(qty) total_stock
, coalesce(sum("0-30"),0) "0-30"
, coalesce(sum("31-60"),0) "31-60"
, coalesce(sum("61-90"),0) "61-90"
, coalesce(sum(">90"),0) ">90"
from
(
select stockid, qty
, case when days between 0 and 30 then qty end "0-30"
, case when days between 31 and 60 then qty end "31-60"
, case when days between 61 and 90 then qty end "61-90"
, case when days > 90 then qty end ">90"
from
(
select sr.stockid, sr.qty, (to_date(:baseDate) - sr.sdate) days
from stockreceipts sr
)
)
group by stockid
order by stockid
;
select stockid, sum(qty) total_stock
, coalesce(sum("0-30"),0) "0-30"
, coalesce(sum("31-60"),0) "31-60"
, coalesce(sum("61-90"),0) "61-90"
, coalesce(sum(">90"),0) ">90"
from
(
select stockid, qty
, case when days between 0 and 30 then qty end "0-30"
, case when days between 31 and 60 then qty end "31-60"
, case when days between 61 and 90 then qty end "61-90"
, case when days > 90 then qty end ">90"
from
(
select sr.stockid, sr.qty, (to_date(:baseDate) - sr.sdate) days
from stockreceipts sr
)
)
group by stockid
order by stockid
;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
not 100% clear so far ...