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.

 All Forums
 SQL Server 2005 Forums
 Other SQL Server Topics (2005)
 joint problem

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 A

ID Date Total
001 01/01/13 10.000
002 01/01/13 20.000

Table B

ID Item Qty Subtotal
001 A 2 5000
001 B 2 5000
002 A 2 20000

I try to make left outer joint from table A and Table B it result :

ID Date Total item qty subtotal
001 01/01/13 10.000 A 2 5000
001 01/01/13 10.000 B 2 5000

I want to make the result like this :

ID Date Total item qty subtotal
001 01/01/13 10.000 A 2 5000
001 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.Subtotal
FROM TableA a
INNER JOIN (
SELECT ROW_NUMBER() OVER (PARTITION BY ID ORDER BY Item) AS Seq, *
FROM tableB
)b
ON b.ID = a.ID
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

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 subtotal
001 01/01/13 10.000 A 2 5000
001 01/01/13 0 B 2 5000

the result :
ID Date Total id qty subtotal
001 01/01/13 10.000 A 4 10000

the record only show 1 row and the A id representing the summary of detail.

thanks

Go to Top of Page

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.Subtotal
FROM TableA a
INNER JOIN (
SELECT MIN(Item) AS Item,SUM(Qty) AS Qty,SUM(SubTotal) AS SubTotal
FROM tableB
GROUP BY ID
)b
ON b.ID = a.ID
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -