Hi,
Need some help on a sql query.
I have already got the data but want know an idea on how to arrange data.
Query result is like this
ItemCode | ItemName | ExpDate | Qty | Expiry |
BAL0120 | Hit Minis Cocoa Cream 130g | 29/09/16 | 4027 | 1 |
BAL0132 | Butter Leibniz 100g | 12/10/16 | 497 | 1 |
BAL0143 | Zoo Jungle 100g | 28/04/16 | 457 | 1 |
BAL0143 | Zoo Jungle 100g | 26/07/16 | 602 | 2 |
BAL0204 | Choco Leibniz 125g | 07/10/16 | 1245 | 1 |
BAL0205 | Choco Friend 100g | 30/04/16 | 1 | 1 |
BAL0205 | Choco Friend 100g | 14/07/16 | 3613 | 2 |
BAL0205 | Choco Friend 100g | 04/10/16 | 6048 | 3 |
Want get the report like
Item Code | Item Description | Expiry 1 | Qty | Expiry 2 | Qty | Expiry 3 | Qty | Expiry 4 | Qty | Total |
BAL0120 | Hit Minis Cocoa Cream 130g | 29/09/16 | 4027 | 4027 | ||||||
BAL0132 | Butter Leibniz 100g | 12/10/16 | 497 | 497 | ||||||
BAL0143 | Zoo Jungle 100g | 28/04/16 | 457 | 26/07/16 | 602 | 1059 | ||||
BAL0204 | Choco Leibniz 125g | 07/10/16 | 1245 | 1245 | ||||||
BAL0205 | Choco Friend 100g | 30/04/16 | 1 | 14/07/16 | 3613 | 04/10/16 | 6048 | 9662 |
Query is below
select ItemCode,ItemName,DistNumber,ExpDate,Qty,ROW_NUMBER() OVER(PARTITION BY ItemCode ORDER BY ItemCode,ExpDate) as Expiry from
(select T0.ItemCode, T2.ItemName,T0.DistNumber,T0.ExpDate,SUM(T1.Quantity) as Qty
from OBTN T0 left join OBTQ T1 on T0.ItemCode=T1.ItemCode and T0.SysNumber=T1.SysNumber left join OITM T2 on T0.ItemCode=T2.ItemCode
where T1.Quantity>0
group by T0.ItemCode,T2.itemName,T0.DistNumber,T0.ExpDate) s