Solved

Create/Return Single record (multiple Columns) from Multiple records

Posted on 2009-07-06
3
449 Views
Last Modified: 2012-05-07
I know there is a way to do this, but not sure how?  I have a table (or multple tables) which contain records of related items (unlimited in the table).  However, I need to return a single record format for extracting to CSV.   I would like to do this in SQL query (currently using MS SQL Server 2005 Express Edition).

The real DB and Tables contain much more data however, if I can get this information out I should be able to include say, Address, Phone, BirthDate... with the additional columns or concatenate into the five,

An example (probably not the best) would be two tables (note the pipe is the separator for display only)

[Person]
 ID
 Name

[Friend]
 ID
 Person_ID
 Name

[Person]  - Sample Data
1 | Al Smith
2 | Bob Jones

[Friend] - Sample Data
1 | 1 | Stan Lee
2 | 1 | Mike Roberts
3 | 1 | Phil Paterson
4 | 2 | Mike Reilly
5 | 2 | Harry Johnson

I would like to return say five(max) fixed column (whether there are more or less friends)

Like
Al Smith |  Stan Lee |  Mike Robert |  Phil Paterson | BLANK | BLANK
Bob Jones | Mike Reilly | Harry Johnson | BLANK | BLANK | BLANK

Also, if there is a better way to do this I am open for options!

Thanks!
0
Comment
Question by:DoDebug
  • 2
3 Comments
 
LVL 17

Expert Comment

by:pssandhu
ID: 24785468
Helped another asker on kind of same question. The onyl difference is that it returns comman seperate values and I think that is what you are looking for.
http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/Q_24537451.html
Hope thie helps.
P.
0
 

Author Comment

by:DoDebug
ID: 24786771
pssandhu -- I will review this but, I am not looking for the CSV data to be returned to the field, I will export to CSV with BCP or other.  I am however, selecting multiple records with this information and just need a trimmed down version of the data.
Thanks.
0
 

Accepted Solution

by:
DoDebug earned 0 total points
ID: 24789215
Since I didn't hear any valid solution... I use this!   Which works fine.  As usual this site does not help.  Thanks anyway.
select
  person.id
, [1] = max(case when RowID = 1 then friend.name end)
, [2] = max(case when RowID = 2 then friend.name end)
, [3] = max(case when RowID = 3 then friend.name end)
, [4] = max(case when RowID = 4 then friend.name end)
, [5] = max(case when RowID = 5 then friend.name end)
from (
  select
    person.id
  , friend.name
  , friend.id
  , RowID = row_number() over (partition by perison.id order by friend.id)
  from friend
  ) aFriend
group by
  person.id
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Separate 2 comma delimited columns into separate rows 2 41
Access join syntax when converting to T-SQL query 4 37
RAISERROR WITH NOWAIT 2 18
MS SQL GROUP BY 5 11
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question