Quantcast
Viewing latest article 4
Browse Latest Browse All 7

Help in Transpose format result

Hi Jugnu,

Please also have a try with below:

Create table #temp (ECode char(20), lastName char(20), FirstName char(20), Bdate datetime) Insert into #temp values ('12','Samad','Irhad','1967-01-10') Insert into #temp values ('34','Smith','Peter','1968-01-20') Insert into #temp values ('99','Farhan','Ji','1989-02-19') Insert into #temp values ('44','Jina','Farah','1997-03-05') Insert into #temp values ('41','David','Bhi','1997-06-05') Insert into #temp values ('53','Sam','David','1972-07-01') Insert into #temp values ('17','Kila','Sam','1972-04-01') Insert into #temp values ('19','Sarah','G','1996-05-08') Insert into #temp values ('67','Jinnah','F','1996-08-08') Insert into #temp values ('41','Rose','D','1995-09-05') Insert into #temp values ('53','Sam','T','1975-10-01') Insert into #temp values ('67','Joe','G','1997-10-08') Insert into #temp values ('53','Piya','SW','1962-12-01') Insert into #temp values ('67','Bhani','Q','1992-12-07') Insert into #temp values ('41','Mira','Dr','1987-11-05') ;WITH CTE AS ( SELECT RN, [1] AS Jan, [2] AS Feb, [3] AS Mar, [4] AS Apr, [5] AS May, [6] AS Jun, [7] AS Jul, [8] AS Aug, [9] AS Sep, [10] AS Oct, [11] AS Nov, [12] AS Dec FROM (Select RN=RANK() OVER (PARTITION BY MONTH(Bdate) ORDER BY DAY(Bdate) DESC), TRIM(lastName)+','+TRIM(FirstName)+ ' - '+ Right('0'+Cast(datepart(day,Bdate) as varchar(2)),2) VALUE, MONTH(Bdate) as Month from #temp) T PIVOT ( MAX(VALUE) FOR Month IN ( [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12] ) ) AS pvtMonth) SELECT Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec from CTE

drop table #temp

Image may be NSFW.
Clik here to view.

Best regards,

Melissa

-------------------------------------------

MSDN Community Support

Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to  MSDN Support, feel free to contact MSDNFSF@microsoft.com





Viewing latest article 4
Browse Latest Browse All 7

Trending Articles