If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Microsoft SQL Server > Is the following possible through SQL?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-19-12, 02:40
nst2 nst2 is offline
Registered User
 
Join Date: Sep 2005
Posts: 23
Is the following possible through SQL?

Hi everyone,

on a car renting company program, I have 3 tables, Persons, Cars, PersonRentsCar

Persons - Cars is obviously a m:n relation.

What I want to get is a Grid containing only once every name and in separate cells the cars he/she has already rented, something like:
Person1 Car1 Car3 Car34
Person3 Car2 Car3 Car23
Person17 Car 2 Car7 Car34 Car56
...

I think that this is impossible through SQL. But, is there maybe a way through a function to do that? Or does it need an extern programme, which will get and sort the data in a List or Array?

I am almost convinced that I need a separate small utility to do the job, but I wanted to ask here, in case someone has a better idea.

Thanks a lot in advance.

Last edited by nst2; 01-19-12 at 03:22.
Reply With Quote
  #2 (permalink)  
Old 01-19-12, 05:27
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 01-19-12, 09:24
nst2 nst2 is offline
Registered User
 
Join Date: Sep 2005
Posts: 23
Smile

Thanks a lot for your reply.
Reply With Quote
  #4 (permalink)  
Old 01-21-12, 03:41
nst2 nst2 is offline
Registered User
 
Join Date: Sep 2005
Posts: 23
I read the tutorials, did the examples, also tried pivot in Excel too, but still couldn't manage to have the desired result.

As I read, PIVOT is being used in order to get numerical equations first, like MAX, SUM etc.

But what I need is from a view like the following:

Person17 Car34
Person1 Car1
Person1 Car3
Person3 Car3
Person1 Car34
Person3 Car2
Person17 Car2
Person3 Car23
Person17 Car7
Person17 Car56

to get a table like that:

Person1 Car1 Car3 Car34
Person3 Car2 Car3 Car23
Person17 Car2 Car7 Car34 Car56

without any numerical functions
Reply With Quote
  #5 (permalink)  
Old 01-21-12, 05:32
Wim Wim is offline
Registered User
 
Join Date: Nov 2004
Posts: 1,280
Try this:
Code:
SELECT dt1.person,
	(SELECT LTRIM(RTRIM(car)) + ' '
	FROM PersonRentsCar as dt2
	WHERE dt2.Person = dt1.Person
	ORDER BY car
	FOR XML PATH('')
	) AS Cars
FROM PersonRentsCar as dt1
GROUP BY dt1.Person ;
__________________
With kind regards . . . . . SQL Server 2000/2005/2008/2008 R2 Earned beers: 16
Wim
Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald Knuth
Grabel's Law: 2 is not equal to 3 -- not even for very large values of 2.
Pat Phelan's Law: 2 very definitely CAN equal 3 -- in at least two programming languages

Last edited by Wim; 01-21-12 at 05:41.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On