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
 General SQL Server Forums
 New to SQL Server Programming
 GROUP RECORDS IN ONE LINE
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

jaimealvarez
Starting Member

29 Posts

Posted - 07/27/2012 :  11:27:00  Show Profile  Reply with Quote
I've been working on this thing for so long that can't think straight anymore.

I have this sql:


SELECT

D1.mrmatter
, D1.ttk
, D1.D1Rate
, NULL AS 'D2Rate'
, NULL AS 'D3Rate'

From D1

UNION ALL

SELECT

D2.mrmatter
, D2.ttk
, NULL AS 'D1Rate'
, D2.D2Rate
, NULL AS 'D3Rate'

FROM D2

UNION ALL

SELECT

D3.mrmatter
, D3.ttk
, NULL AS 'D1Rate'
, NULL AS 'D2Rate'
, D3.D3Rate

FROM D3



Is there any way to group them in one line by mrmatter and ttk... this are the only two records that can be present in more than one table, but not necessarily are present in all the tables. I hope that I'm explaining myself, but if not please let me know, like I said... it has been a while


visakh16
Very Important crosS Applying yaK Herder

India
47189 Posts

Posted - 07/27/2012 :  11:32:52  Show Profile  Reply with Quote
you mean this?

SELECT mrmatter,
ttk,
SUM(D1Rate) AS D1Rate,
SUM(D2Rate) AS D2Rate,
SUM(D3Rate) AS D3Rate 
FROM
(SELECT

D1.mrmatter
, D1.ttk
, D1.D1Rate
, NULL AS 'D2Rate'
, NULL AS 'D3Rate'

From D1

UNION ALL

SELECT 

D2.mrmatter
, D2.ttk
, NULL AS 'D1Rate'
, D2.D2Rate
, NULL AS 'D3Rate'

FROM D2

UNION ALL

SELECT

D3.mrmatter
, D3.ttk
, NULL AS 'D1Rate'
, NULL AS 'D2Rate'
, D3.D3Rate

FROM D3
)t
GROUP BY mrmatter,ttk


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

jaimealvarez
Starting Member

29 Posts

Posted - 07/27/2012 :  11:36:14  Show Profile  Reply with Quote
Yes I did!! Tank you!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47189 Posts

Posted - 07/27/2012 :  11:38:46  Show Profile  Reply with Quote
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

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.08 seconds. Powered By: Snitz Forums 2000