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 |
taniarto
Starting Member
27 Posts |
Posted - 2013-06-19 : 00:12:59
|
Dear All, I Have Table A (containing header information) and Table B ( containing detail information)for example :Table AID Date Total001 01/01/13 10.000002 01/01/13 20.000Table BID Item Qty Subtotal001 A 2 5000001 B 2 5000002 A 2 20000I try to make left outer joint from table A and Table B it result :ID Date Total item qty subtotal001 01/01/13 10.000 A 2 5000001 01/01/13 10.000 B 2 5000I want to make the result like this :ID Date Total item qty subtotal001 01/01/13 10.000 A 2 5000001 01/01/13 0 B 2 5000(the header total only show once)thanks,Taniarto |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-06-19 : 01:15:54
|
[code]SELECT a.ID,a.[Date],CASE WHEN Seq=1 THEN a.Total ELSE 0.00 END AS Total,b.Item,b.Qty,b.SubtotalFROM TableA aINNER JOIN ( SELECT ROW_NUMBER() OVER (PARTITION BY ID ORDER BY Item) AS Seq, * FROM tableB )bON b.ID = a.ID [/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
taniarto
Starting Member
27 Posts |
Posted - 2013-06-21 : 00:35:51
|
Thanks it's work..Another question :I want to make the detail only show the total value ( only 1 row) what should I do ?Before :ID Date Total id qty subtotal001 01/01/13 10.000 A 2 5000001 01/01/13 0 B 2 5000the result :ID Date Total id qty subtotal001 01/01/13 10.000 A 4 10000 the record only show 1 row and the A id representing the summary of detail.thanks |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-06-21 : 02:37:35
|
[code]SELECT a.ID,a.[Date],a.Total,b.Item,b.Qty,b.SubtotalFROM TableA aINNER JOIN ( SELECT MIN(Item) AS Item,SUM(Qty) AS Qty,SUM(SubTotal) AS SubTotal FROM tableB GROUP BY ID )bON b.ID = a.ID [/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
|
|
|
|
|