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)
 Dynamic column query

Author  Topic 

robbydogg
Starting Member

6 Posts

Posted - 2011-06-29 : 08:05:27
Hello,
I have a problem i've been trying to get around for some time now, but cannot get anywhere with it so was hoping someone here may help.
I have a table like below:

Part Car1 Car2 Car3 Car4
Squarewheel y y N y
FooFooValve N y N y
Skyhooks y y N N
TartanPaint N N y y

What i need is a query to concatenate the data like this:

Part Fits
Squarewheel Car1,Car2,Car4
FooFooValve Car3,Car4
Skyhooks Car1,Car4
TartanPaint Car3,Car4

But the problem i have is that there will be more columns added along the line (so Car 5, Car 6 may be added)

What i don't want to do is hard-code something as this would need changing everytime a new range is added.

I'm now stuck as to the best way forward - any ideas?

Thanks


Hitting more brick walls than Nigel Mansell

DonAtWork
Master Smack Fu Yak Hacker

2167 Posts

Posted - 2011-06-29 : 08:10:19
Forward thinking = Normalize the database.

http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspx
How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

For ultra basic questions, follow these links.
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp
Go to Top of Page

robbydogg
Starting Member

6 Posts

Posted - 2011-06-29 : 08:13:11
Hi,

Unfortunately as the database is the background of a larger body of systems, i am unable to alter the setup of it, i am merely able to get a view into this and therefore not set up the database as it would have been useful in the first place :(

For each problem, there is a solution. For each solution there is someone with another problem to render the original solution useless
Go to Top of Page

DonAtWork
Master Smack Fu Yak Hacker

2167 Posts

Posted - 2011-06-29 : 08:53:09
ugh. Then dynamic sql may help here. A forum search for "dynamic sql" should turn up Madhi and his wonderful write up of it.

http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspx
How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

For ultra basic questions, follow these links.
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp
Go to Top of Page
   

- Advertisement -