January 16, 2013 at 8:45 am
I need to Pivot this resultset:
Jul-201271S-L247
Aug-201281S-L237
Sep-201291S-L220
Oct-2012101S-L254
Nov-2012111S-L228
Dec-2012121S-L229
Jul-201272ROL192
Aug-201282ROL151
Sep-201292ROL158
Oct-2012102ROL193
Nov-2012112ROL172
Dec-2012122ROL208
To
Jul-2012Aug-2012Sep-2012Oct-2012Nov-2012Dec-2012
1S-L247237220254228229
2ROL192151158193172208
Here is my code:
SELECTLeft(DATENAME(m,real_date_time),3) + '-' +Cast(YEAR(real_date_time) as varchar(4)) 'Mon-Year' ,
Month(Real_Date_Time ) Mo,
Location,
COUNT(distinct PAT_NUM ) Patients
FROM CHRT_Tran
where[SITE]=1
andADMIN_STATUS='admin'
and Real_Date_Time >='7/1/2012'
and Real_Date_Time <'1/1/2013'
and Location IN ('1S-L','2ROL')
group by
Left(DATENAME(m,real_date_time),3) + '-' +Cast(YEAR(real_date_time) as varchar(4)),
Month(Real_Date_Time ),
Location
order by 3,2
Never done Pivot before.
January 16, 2013 at 9:55 am
Take a look at the link in my signature about cross tabs. That article explains how to use pivot in addition to cross tabs.
_______________________________________________________________
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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
January 16, 2013 at 11:46 am
OK. Got it. Thanks a lot!
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply