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
 General SQL Server Forums
 New to SQL Server Programming
 GROUP RECORDS IN ONE LINE

Author  Topic 

jaimealvarez
Starting Member

31 Posts

Posted - 2012-07-27 : 11:27:00
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

52326 Posts

Posted - 2012-07-27 : 11:32:52
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

31 Posts

Posted - 2012-07-27 : 11:36:14
Yes I did!! Tank you!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-07-27 : 11:38:46
welcome

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

Go to Top of Page
   

- Advertisement -