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
 Transact-SQL (2005)
 Mapp row or group into column

Author  Topic 

voyager838
Yak Posting Veteran

90 Posts

Posted - 2010-04-01 : 12:28:23
How do i map this table


P | K | A | B
=====================
1 | x | 3 | 5
2 | x | 9 | 19
3 | x | 4 | 1
1 | y | 1 | 0
2 | y |-1 | -2


Into this


P | x_A | x_B | y_A | y_B
============================
1 | 3 | 5 | 1 | 0
2 | 9 | 19 | -1 | -2
3 | 4 | 1 | null| null


Perhaps it somekind of an row to columns operations,
But i dont know how.

P here is the periode and K is the group.
And last A and B is different types vid of course different values

Thanks

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-04-01 : 12:35:00
[code]SELECT P,
MAX(CASE WHEN K='x' THEN A ELSE NULL END) AS x_A,
MAX(CASE WHEN K='x' THEN B ELSE NULL END) AS x_B,
MAX(CASE WHEN K='y' THEN A ELSE NULL END) AS y_A,
MAX(CASE WHEN K='y' THEN B ELSE NULL END) AS y_B
FROM Table
GROUP BY P
[/code]

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

Go to Top of Page

voyager838
Yak Posting Veteran

90 Posts

Posted - 2010-04-01 : 12:48:03
Oh no, of course!! I should had been figured it out. :)

Thanks visakh16!
You are great.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-04-01 : 12:48:46
welcome

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

Go to Top of Page
   

- Advertisement -