SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Other SQL Server Topics (2005)
 joint problem
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

taniarto
Starting Member

Indonesia
27 Posts

Posted - 06/19/2013 :  00:12:59  Show Profile  Reply with Quote
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

India
52325 Posts

Posted - 06/19/2013 :  01:15:54  Show Profile  Reply with Quote

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 


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

taniarto
Starting Member

Indonesia
27 Posts

Posted - 06/21/2013 :  00:35:51  Show Profile  Reply with Quote
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

India
52325 Posts

Posted - 06/21/2013 :  02:37:35  Show Profile  Reply with Quote

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 


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.17 seconds. Powered By: Snitz Forums 2000