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 2000 Forums
 SQL Server Development (2000)
 Dynamically generating columns

Author  Topic 

vijayh143
Starting Member

1 Post

Posted - 2012-02-13 : 06:19:02
Hi All,
I have a table structure like this:
Table structure
Pk,fk Pk,fk Pk,fk Pk,fk Pk,fk Pk Value1 Value 2
A1 A2 A3 A4 A5 1 xx yy
A1 A2 A3 A4 A5 2 xxx yyy
A1 A2 A3 A4 A5 3 a b
A1 B2 A3 A4 A5 4 aa bb
B1 A2 A3 A4 A5 5 aaa bbb
B1 B2 B3 B4 B5 6 d e
B1 B2 B3 B4 B5 7 dd ee
B1 B3 C3 B4 B5 8 ddd eee

In the above table (pk,fk) constitutes one column and first 6 columns make composite kesy to the table.

Now i have to write a query which gives output like this:

A1 A2 A3 A4 A5 xx yy xxx yyy a b
A1 B2 A3 A4 A5 aa bb
B1 A2 A3 A4 A5 aaa bbb
B1 B2 B3 B4 B5 d e dd ee
B1 B2 C3 B4 B5 ddd eee

Please help me!!



visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-02-13 : 10:11:16
[code]
SELECT Col1,Col2,Col3,Col4,Col5,
STUFF((SELECT ',' + Value1 + ' ' + Value2 FROM table
WHERE Col1=t.Col1
AND Col2= t.Col2
AND Col3 = t.Col3
AND Col4 = t.Col4
AND Col5 = t.Col5
ORDER BY Col6
FOR XML PATH('')),1,1,'')
FROM (SELECT DISTINCT Col1,Col2,Col3,Col4,Col5 FROM Table)t
[/code]

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

Go to Top of Page
   

- Advertisement -