?
Solved

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

Posted on 2009-07-06
3
Medium Priority
?
452 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

10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

Question has a verified solution.

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

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…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

777 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