SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


how to get data from a table all in one result row


how to get data from a table all in one result row

Author
Message
Denise McMillan
Denise McMillan
SSC-Addicted
SSC-Addicted (450 reputation)SSC-Addicted (450 reputation)SSC-Addicted (450 reputation)SSC-Addicted (450 reputation)SSC-Addicted (450 reputation)SSC-Addicted (450 reputation)SSC-Addicted (450 reputation)SSC-Addicted (450 reputation)

Group: General Forum Members
Points: 450 Visits: 501
I'm need help with a query.
The table has an id, a name, and a type. Sample:
ID Name Type
123 description black
123 technique breaking
456 findings nice
456 technique splitting

How can I select to get this result:
123 black breaking
456 nice splitting

Thanks very much for any help.
Luis Cazares
Luis Cazares
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16617 Visits: 19098
You can use cross tabs to accomplish this. To read more about this technique, read the following article.
http://www.sqlservercentral.com/articles/T-SQL/63681/
SELECT ID,
MAX( CASE WHEN Name = 'Description' THEN Type) AS Description,
MAX( CASE WHEN Name = 'technique ' THEN Type) AS Technique
FROM MyTable
GROUP BY ID




Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Denise McMillan
Denise McMillan
SSC-Addicted
SSC-Addicted (450 reputation)SSC-Addicted (450 reputation)SSC-Addicted (450 reputation)SSC-Addicted (450 reputation)SSC-Addicted (450 reputation)SSC-Addicted (450 reputation)SSC-Addicted (450 reputation)SSC-Addicted (450 reputation)

Group: General Forum Members
Points: 450 Visits: 501
This worked, I had to add an else and end and it worked like I needed.
SELECT ID,
MAX( CASE WHEN Name = 'Description' THEN Type else null end) AS Description,
MAX( CASE WHEN Name = 'technique ' THEN Type else null end) AS Technique
FROM MyTable
GROUP BY ID

Thanks very much.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search