Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Need to pivot query results Expand / Collapse
Author
Message
Posted Tuesday, June 25, 2013 1:27 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Tuesday, July 29, 2014 8:07 PM
Points: 79, Visits: 216
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.
Post #1467345
Posted Tuesday, June 25, 2013 1:30 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Friday, August 29, 2014 2:32 PM
Points: 13,302, Visits: 12,168
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)
Post #1467347
Posted Tuesday, June 25, 2013 3:02 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Tuesday, July 29, 2014 8:07 PM
Points: 79, Visits: 216
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?
Post #1467391
Posted Tuesday, June 25, 2013 4:16 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 7:07 PM
Points: 3,559, Visits: 7,679
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.
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?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1467402
Posted Tuesday, June 25, 2013 4:46 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Tuesday, July 29, 2014 8:07 PM
Points: 79, Visits: 216
Louis, I think this is what I am looking for. Thank you!

David
Post #1467406
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse