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)
 Need help with multiple rows into one row

Author  Topic 

JuSlaughter
Starting Member

1 Post

Posted - 2009-11-25 : 10:27:26
Thats a rubbish title, sorry. First post so please be gentle.

I have to write a query for our ecommerce team where they want me to return data for car insurance policies. What they want is if the driver has multiple cars, each car should appear as a column in the recordset instead of multiple rows in the recordset.

For example, I get 3 rows of data, each with its own make and model

Make Model
Ford Focus
Vauxhall Astra
Honda Civic

It needs to look like

Make1 Model1 Make2 Model2 Make3 Model3
Ford Focus Vauxhall Astra Honda Civic

If there are only 2 cars, only Make1 and Make2, etc will show, if there are 10, I need Make10, Model10.

Does that make sense?

Any help would be massively appreciated.

Cheers

Vinnie881
Master Smack Fu Yak Hacker

1231 Posts

Posted - 2009-11-25 : 13:09:51
I normally like to do this type of thing with the front end report rather than the back end database, but you can do this with a standard pivot in sql, but keep in mind unless you use dynamic sql, you will need to predetermine the maximum # of Make's you want.









Success is 10% Intelligence, 70% Determination, and 22% Stupidity.
\_/ _/ _/\_/ _/\_/ _/ _/- 881
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-11-26 : 01:50:52
Try to play with this
http://sqlblogcasts.com/blogs/madhivanan/archive/2008/08/27/dynamic-pivot-in-sql-server-2005.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -