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)
 Please help me to write a SQL query!!!

Author  Topic 

SQLnewbie2004
Starting Member

2 Posts

Posted - 2004-10-13 : 12:13:18
Hi, can anyone please help me to write a SQL query to create multiple columns for each vehicle type and year made based on each unique name and location?

Here is a table to illustrate my problem:

Name Location Vehicle Year
John Toronto Honda 1990
John Toronto Lexus 2000
Ken Hamilton Ford 1989
Christ London Benz 2001
Christ London Mazda 2003
Christ London Toyota 1994
Tom Toronto GM 1996
Tom Barrie Ford 2002

I want to match the Name and the Location, and then partition the multiple vehicles and years into columns as shown below:

Name Location Vehicle1 Year1 Vehicle2 Year2 Vehicle3 Year3
John Toronto Honda 1990 Lexus 2000
Ken Hamilton Ford 1989
Christ London Benz 2001 Mazda 2003 Toyota 1994
Tom Toronto GM 1996
Tom Barrie Ford 2002

In general, how can I partition multiple columns (ie. vehicle types and years) relating to a specified grouping of rows (ie. name and location) into one extended row? One more problem is that the number of vehicles along with their year made may vary. In my original table, I have Christ who has 3 vehicles along with 3 year made. But, say I could have Sam from Brampton who has n vehicles along with their year made. In this case I want to partition n vehicle columns and N year columns for Sam in Brampton like this:

Name Location Vehicle1 Year1 Vehicle2 Year2 ... VehicleN YearN
Sam Brampton Toyota 1999 Honda 2000 Volks 2003

The problem is how can I determine the number of vehicles along with their year made in the list of records in order to create that many columns for each unique name and location?

Any suggestion will be greatly appreciated!Sorry about the spacing problem in the tables.

Thanks for the help!!

ditch
Master Smack Fu Yak Hacker

1466 Posts

Posted - 2004-10-13 : 12:22:07
If this is for a report - then your reporting tool can handle this quite easily.
eg - In reporting Services you can use a Matrix as opposed to a table.

If it has to be done in SQL - then you can find many posts on cross tabs here - Just search the the site a bit.


Duane.
Go to Top of Page

SQLnewbie2004
Starting Member

2 Posts

Posted - 2004-10-13 : 12:27:23
Sorry, this is strictly for the SQL Server 2000. I really need some guidelines on how to approach this problem.
Go to Top of Page

ditch
Master Smack Fu Yak Hacker

1466 Posts

Posted - 2004-10-13 : 12:37:29
Well - Like I said - Just search the site a bit.

http://www.sqlteam.com/item.asp?ItemID=2955



Duane.
Go to Top of Page
   

- Advertisement -