Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Need to pivot query results


Need to pivot query results

Author
Message
oradbguru
oradbguru
SSC Journeyman
SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)

Group: General Forum Members
Points: 95 Visits: 289
Hello, I have the following query and I need the results in 1 row going across. Here is the query:


SELECT top(5)
ap.apptDte,
ap.begTime,
lc.locName,
ev.event,
pm.fname + ' ' + pm.lname
FROM
appts ap
join loc_mstr lc on lc.locID = ap.locID
join events ev on ev.eventID = ap.eventID
join prov_mstr pm on pm.provID = ap.rend_provID
WHERE
personID = '5D06AAE9-1B8D-461B-BAAB-633C1ED7ED43'
AND appt_date > GETDATE()



The results would have 25 columns on 1 row. Is this possible?

Thank you.
Sean Lange
Sean Lange
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: 16639 Visits: 17027
oradbguru (6/25/2013)
Hello, I have the following query and I need the results in 1 row going across. Here is the query:


SELECT top(5)
ap.apptDte,
ap.begTime,
lc.locName,
ev.event,
pm.fname + ' ' + pm.lname
FROM
appts ap
join loc_mstr lc on lc.locID = ap.locID
join events ev on ev.eventID = ap.eventID
join prov_mstr pm on pm.provID = ap.rend_provID
WHERE
personID = '5D06AAE9-1B8D-461B-BAAB-633C1ED7ED43'
AND appt_date > GETDATE()



The results would have 25 columns on 1 row. Is this possible?

Thank you.


Yes it is possible. Please read the links in my signature about cross tabs. If you still need help then read the first link in my signature for best practices when posting questions.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
oradbguru
oradbguru
SSC Journeyman
SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)

Group: General Forum Members
Points: 95 Visits: 289
There is no aggregated data here. The results from this query would be a 5 rows with 5 columns. I just need to have 1 row with with 25 columns. Do you have any sample code with this?
Luis Cazares
Luis Cazares
SSCrazy Eights
SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)

Group: General Forum Members
Points: 8581 Visits: 18149
Without sample data and expected results, I'm only taking a shot in the dark.
You should really read and understand the articles mentioned by Sean as there's some sample code in them.
I can't test my solution but it might give you an idea. Even if it worked, you must complete it.


WITH CTE as(
SELECT top(5)
ap.apptDte,
ap.begTime,
lc.locName,
ev.event,
pm.fname + ' ' + pm.lname AS name,
ROW_NUMBER() OVER( ORDER BY( SELECT NULL)) rn
FROM
appts ap
join loc_mstr lc on lc.locID = ap.locID
join events ev on ev.eventID = ap.eventID
join prov_mstr pm on pm.provID = ap.rend_provID
WHERE
personID = '5D06AAE9-1B8D-461B-BAAB-633C1ED7ED43'
AND appt_date > GETDATE())
SELECT MAX( CASE WHEN rn = 1 THEN apptDte END),
MAX( CASE WHEN rn = 1 THEN begTime END),
MAX( CASE WHEN rn = 1 THEN locName END),
MAX( CASE WHEN rn = 1 THEN event END),
MAX( CASE WHEN rn = 1 THEN name END),
MAX( CASE WHEN rn = 2 THEN apptDte END),
MAX( CASE WHEN rn = 2 THEN begTime END),
MAX( CASE WHEN rn = 2 THEN locName END),
MAX( CASE WHEN rn = 2 THEN event END),
MAX( CASE WHEN rn = 2 THEN name END)
--And so on
FROM CTE




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
oradbguru
oradbguru
SSC Journeyman
SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)

Group: General Forum Members
Points: 95 Visits: 289
Louis, I think this is what I am looking for. Thank you!

David
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