Solved

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

Posted on 2009-07-06
3
447 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

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.​
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now