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
 i need simpe solution

Author  Topic 

krishna_yess
Yak Posting Veteran

81 Posts

Posted - 2009-12-08 : 06:07:59
i have a table like this

CAT NAME TEST_1
A a 4
A b 5
A c 3
A d 4
A e 4
A f 5
B a 5
B b 4
B c 3
B d 3
B e 5
B f 5



i need the output like this

NAME TEST_1_1 TEST_1_2
a 4 5
b 5 4
c 3 3
d 4 3
e 4 5
f 5 5


basically group by name but i need result in set of columns for each NAME group

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-12-08 : 06:13:20
are you using SQL 2000 or 2005/2008 ?


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

krishna_yess
Yak Posting Veteran

81 Posts

Posted - 2009-12-08 : 06:24:22
using sql 2005, should be compatible with 2000. need to move to production server which uses 2000
Go to Top of Page

RyanRandall
Master Smack Fu Yak Hacker

1074 Posts

Posted - 2009-12-08 : 07:06:22
This?

select Name,
max(case when CAT = 'A' then TEST_1 end) as TEST_1_1,
max(case when CAT = 'B' then TEST_1 end) as TEST_1_2
from MyTable
group by Name


Ryan Randall - Yak of all trades
Solutions are easy. Understanding the problem, now, that's the hard part.
Go to Top of Page

krishna_yess
Yak Posting Veteran

81 Posts

Posted - 2009-12-08 : 07:18:58
exactly thats what i did,
but
CAT will grow like A,B ,C, D.....

need to track CAT dynamically

how to do this
Go to Top of Page

RyanRandall
Master Smack Fu Yak Hacker

1074 Posts

Posted - 2009-12-08 : 07:38:51
Then you need a dynamic pivot...

[url]http://sqlblogcasts.com/blogs/madhivanan/archive/2007/08/27/dynamic-crosstab-with-multiple-pivot-columns.aspx[/url]

Ryan Randall - Yak of all trades
Solutions are easy. Understanding the problem, now, that's the hard part.
Go to Top of Page
   

- Advertisement -