Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2009-07-06
3
Medium Priority
?
453 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

604 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