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 |
|
sdhusodo
Starting Member
4 Posts |
Posted - 2011-10-30 : 00:57:47
|
| I have the following three tables:InvoiceLine:InvNo ItemNo Quantity Month DateINV1005 105 10 01/31/11INV1006 106 10 01/31/11INV1001 101 10 01/31/11INV1001 102 10 01/31/11INV1002 101 10 01/31/11INV1003 101 10 02/28/11INV1004 102 10 02/28/11ReturnLine:RetNo No_ Quantity Month DateCR1001 101 2 01/31/11CR1002 101 2 01/31/11CR1003 104 2 01/31/11CR1004 103 2 02/28/11CR1005 105 2 02/28/11Item:No_ Category Brand Description100 Tennis MI MIKASA 01 101 Tennis MI MIKASA 02102 Tennis MI MIKASA 20103 Tennis MI MIKASA 03 104 Tennis AS ASANO 04105 Tennis AS ASANO 05106 Tennis AS ASANO 06I want to join these tables to get the following result:Month Date Category Brand Sales Qty Return Qty01/31/11 Tennis AS 20 201/31/11 Tennis MI 30 402/28/11 Tennis AS 0 202/28/11 Tennis MI 20 2The closest query that I could get is:SELECT SIL.[Month Date],ITM.[Category],ITM.[Brand],SUM(SIL.[Quantity]) [Sales Qty],SUM(SCL.[Quantity]) [Return Qty] FROM [InvoiceLine] SILJOIN [Item] ITMon SIL.[No_] = ITM.[No_]JOIN (SELECT [Month Date],ITM.[Category],ITM.[Brand],sum([Quantity]) [Return Qty] FROM [ReturnLine] GROUP BY [Month Date],ITM.[Category],ITM.[Brand]) SCLON SCL.[Month Date] = SIL.[Month Date]and SCL.[No_] = ITM.[No_]GROUP BY SIL.[Month Date],ITM.[Category],ITM.[Brand] and it does not give me the correct result.The sales qty are correct but the return qty is wrong, and I am missing the return for AS for 02/28/11:Month Date Category Brand Sales Qty Return Qty01/31/11 Tennis AS 20 401/31/11 Tennis MI 30 1202/28/11 Tennis MI 20 4Please help. Thank you.sdhusodo |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-30 : 01:28:07
|
| [code]SELECT r.[Month Date],i.Category,i.Brand,SUM(r.Sales) AS SalesQty,SUM(r.Return) AS ReturnQtyFROM Item iINNER JOIN (SELECT ItemNo,[Month Date],SUM(Sales) AS Sales,SUM(Return) AS return FROM (SELECT ItemNo,[Month Date],Quantity AS Sales, 0 AS Return FROM InvoiceLine UNION ALL SELECT No_,[Month Date],0,Quantity FROM ReturnLine )t GROUP BY ItemNo,[Month Date] )rON r.ItemNo = i.[No_]GROUP BY i.Category,i.Brand,r.[Month Date][/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
sdhusodo
Starting Member
4 Posts |
Posted - 2011-10-30 : 21:57:30
|
| visakh16,Thank you for the prompt answer.I have tried the code and it works like a charm.I just learned something very useful, thank you. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-31 : 04:27:34
|
welcomeGlad that i could be of help ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|