Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
dhinasql
Posting Yak Master
195 Posts |
Posted - 2011-07-25 : 13:47:27
|
| Friends,I have product billing table, Item Code Product Running Bill Rate Qty Amount1001 AC 1 4000 1 40001001 AC 1 4000 2 80001002 Ash 1 5000 1 50001003 kmar 1 2500 1 25001001 AC 2 4000 2 80001002 Ash 2 5000 2 100001003 kmr 2 2500 2 50001001 AC 3 4000 1 40001002 Ash 3 5000 1 50001003 kmar 3 2500 2 5000Please find the expected output when i pass Item code, and Running BillItem Code=1001 and Running Bill =1-----------------------------------------Item Previous.Qty Pre.Amnt ThisQty This_Amount TotalQty Total Amount1001 0 0 3 12000 3 12000Item Code=1002 and Running =1----------------------------------------1002 0 0 1 5000 1 5000Item Code=1001 and Running Bill =2---------------------------------------1001 3 12000 2 8000 5 20000Item Code=1002 and Running Bill =2-----------------------------------1002 1 5000 2 10000 3 15000Item Code=1001 and Running Bill =3--------------------------------------1001 5 20000 1 4000 6 24000Pre.Qty = If bill number is 3, we have to sum Qty of bill < 3 ,if bill number is 1 previous qty is 0Pre.Amount = If bill number is 4 we have to add the Amount of all the previous bills i.e sum of < 4TotalQty = Pre.Qty + this qtyTotal Amount = Pre Amount + this Amountplease help me to get the expected output.thanks and looking forward |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-07-25 : 14:02:08
|
| [code];With CTE (ItemCode,RunningBill,Qty,Amt)AS(SELECT ItemCode,RunningBill,SUM(Qty),SUM(AMt)FROM tableGROUP BY ItemCode,RunningBill)SELECT c.ItemCode,c1.PrevQty,c1.PrevAmt,c.Qty,c.Amt,COALESCE(c1.PrevQty,0) + c.Qty AS TotalQty,COALESCE(c1.PrevAmt,0) + c.Amt AS TotalAmtFROM CTE cOUTER APPLY (SELECT SUM(Qty) AS PrevQty,SUM(Amt) AS PrevAmt FROM CTE WHERE ItemCode=c.ItemCode AND RunningBill < c.RunningBill )c1[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
dhinasql
Posting Yak Master
195 Posts |
Posted - 2011-07-26 : 04:46:01
|
quote: Originally posted by visakh16
;With CTE (ItemCode,RunningBill,Qty,Amt)AS(SELECT ItemCode,RunningBill,SUM(Qty),SUM(AMt)FROM tableGROUP BY ItemCode,RunningBill)SELECT c.ItemCode,c1.PrevQty,c1.PrevAmt,c.Qty,c.Amt,COALESCE(c1.PrevQty,0) + c.Qty AS TotalQty,COALESCE(c1.PrevAmt,0) + c.Amt AS TotalAmtFROM CTE cOUTER APPLY (SELECT SUM(Qty) AS PrevQty,SUM(Amt) AS PrevAmt FROM CTE WHERE ItemCode=c.ItemCode AND RunningBill < c.RunningBill )c1 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
Hello Visakh,Thank you for your Reply. My apologies there is some changes in column. Product_Code and SPB code is newly added Item_Code Running Bill Rate Qty Amount Product_Code SPB Code1001 1 4000 1 4000 5001 20011001 1 2000 2 4000 5001 20021001 1 1500 1 1500 5002 20021002 1 4000 1 4000 5001 20011003 1 2500 1 4000 5002 2001 1001 2 4000 2 8000 5001 20011001 2 4000 2 8000 5001 20021001 2 2500 2 5000 5002 2001 1001 3 4000 1 4000 5001 20011001 3 2500 1 2500 5002 20011001 3 2500 2 5000 5002 2001Please find the expected outputItemCode =1001 and Running Bill =1-----------------------------------------ItemCode Product Code SPB Code Previous.Qty Pre.Amnt ThisQty This_Amount TotalQty Total Amount1001 5001 2001 0 0 1 8000 1 80001001 5001 2002 0 0 2 8000 2 80001001 5002 2001 0 0 0 0 0 01001 5002 2002 0 0 1 1500 1 1500ItemCode =1001 and Running Bill =2-----------------------------------------ItemCode Product Code SPB Code Previous.Qty Pre.Amnt ThisQty This_Amount TotalQty Total Amount1001 5001 2001 1 8000 1 4000 1 40001001 5001 2002 2 8000 2 8000 2 80001001 5002 2001 0 0 2 5000 2 50001001 5002 2002 1 1500 0 0 1 1500ItemCode =1001 and Running Bill =3-----------------------------------------ItemCode Product Code SPB Code Previous.Qty Pre.Amnt ThisQty This_Amount TotalQty Total Amount1001 5001 2001 1 8000 1 8000 1 80001001 5001 2002 2 8000 0 0 2 80001001 5002 2001 2 5000 3 7500 5 125001001 5002 2002 1 1500 0 0 1 1500If The Product_Code, and SPB code is same we have to sum it and display.If the product_code and SPB code is differ we have to display it separately.Thanks and looking forward to hear from you... |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-07-26 : 12:12:20
|
| [code];With CTE (ItemCode,RunningBill,Product_Code,[SPB code],Qty,Amt)AS(SELECT t.ItemCode,t.RunningBill,f.Product_Code,f.[SPB code],SUM(t1.Qty),SUM(t1.AMt)FROM (SELECT DISTINCT ItemCode,RunningBill FROM table) tCROSS JOIN (SELECT DISTINCT Product_Code,[SPB code] FROM Table)fLEFT JOIN table t1ON t1.itemCode = t.ItemCodeAND t1.RunningBill = t.RunningBillAND t1.Product_code = f.Product_codeAND t1.[SPB Code] = f.[SPB Code]GROUP BY t.ItemCode,t.RunningBill,f.Product_Code,f.[SPB code])SELECT c.ItemCode,c.RunningBill,c.Product_Code,c.[SPB code],c1.PrevQty,c1.PrevAmt,c.Qty,c.Amt,COALESCE(c1.PrevQty,0) + c.Qty AS TotalQty,COALESCE(c1.PrevAmt,0) + c.Amt AS TotalAmtFROM CTE cOUTER APPLY (SELECT SUM(Qty) AS PrevQty,SUM(Amt) AS PrevAmt FROM CTE WHERE ItemCode=c.ItemCode AND Product_Code = c.Product_Code AND [SPB code] = [SPB code] AND RunningBill < c.RunningBill )c1[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|