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


Query Help


Query Help

Author
Message
yogi123
yogi123
SSC-Enthusiastic
SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)

Group: General Forum Members
Points: 130 Visits: 394
Hello Everyone

I have one task

I need to do pivoting


create table #Temp1
(
iSchoolYearCode int,
dtCalendarDay datetime,
iCalendarDatCategoryID int,
vcCalendarCategoryCode varchar(25)
)

insert into #Temp1 values (2012,'2012-08-30 00:00:00.000',15,'Quarter Start Day')
insert into #Temp1 values (2012,'2012-11-07 00:00:00.000',89,'Quarter Grade Closing')
insert into #Temp1 values (2012,'2012-11-08 00:00:00.000',15,'Quarter Start Day')
insert into #Temp1 values (2012,'2013-01-25 00:00:00.000',89,'Quarter Grade Closing')
insert into #Temp1 values (2012,'2013-01-30 00:00:00.000',15,'Quarter Start Day')
insert into #Temp1 values (2012,'2013-04-08 00:00:00.000',89,'Quarter Grade Closing')
insert into #Temp1 values (2012,'2013-04-09 00:00:00.000',15,'Quarter Start Day')
insert into #Temp1 values (2012,'2013-06-13 00:00:00.000',89,'Quarter Grade Closing')




desired output



iSchoolYearCode Quarter Start Day Quarter Grade Closing
2012 2012-08-30 00:00:00.000 2012-11-07 00:00:00.000
2012 2012-11-08 00:00:00.000 2013-01-25 00:00:00.000
2012 2013-01-30 00:00:00.000 2013-04-08 00:00:00.000
2012 2013-04-09 00:00:00.000 2013-06-13 00:00:00.000

Erin Ramsay
Erin Ramsay
Mr or Mrs. 500
Mr or Mrs. 500 (561 reputation)Mr or Mrs. 500 (561 reputation)Mr or Mrs. 500 (561 reputation)Mr or Mrs. 500 (561 reputation)Mr or Mrs. 500 (561 reputation)Mr or Mrs. 500 (561 reputation)Mr or Mrs. 500 (561 reputation)Mr or Mrs. 500 (561 reputation)

Group: General Forum Members
Points: 561 Visits: 1099
Why does this look like homework?
kapil_kk
kapil_kk
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2437 Visits: 2763
you can do this using CASE also..

_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
yogi123
yogi123
SSC-Enthusiastic
SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)

Group: General Forum Members
Points: 130 Visits: 394
kapil_kk (2/20/2013)
you can do this using CASE also..


I try this


select iSchoolYearCode,
CASE WHEN iCalendarDatCategoryID = 15 THEN dtCalendarDay
ELSE '' End,
CASE WHEN iCalendarDatCategoryID = 89 THEN dtCalendarDay
ELSE '' End
from #Temp1



but didn't work
kapil_kk
kapil_kk
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2437 Visits: 2763
try this.. hope it will work

SELECT
iSchoolYearCode,
CASE WHEN vcCalendarCategoryCode = 'Quarter Start Day' THEN dtCalendarDay ELSE 0 END AS QuarterStartDay,
CASE WHEN vcCalendarCategoryCode = 'Quarter Grade Closing' THEN dtCalendarDay ELSE 0 END AS QuarterGradeClosing

FROM #Temp1

_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
yogi123
yogi123
SSC-Enthusiastic
SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)

Group: General Forum Members
Points: 130 Visits: 394
kapil_kk (2/20/2013)
try this.. hope it will work

SELECT
iSchoolYearCode,
CASE WHEN vcCalendarCategoryCode = 'Quarter Start Day' THEN dtCalendarDay ELSE 0 END AS QuarterStartDay,
CASE WHEN vcCalendarCategoryCode = 'Quarter Grade Closing' THEN dtCalendarDay ELSE 0 END AS QuarterGradeClosing

FROM #Temp1


Kapil I already try this

Here what i got



iSchoolYearCode QuarterStartDay QuarterGradeClosing
2012 2012-08-30 00:00:00.000 1900-01-01 00:00:00.000
2012 1900-01-01 00:00:00.000 2012-11-07 00:00:00.000
2012 2012-11-08 00:00:00.000 1900-01-01 00:00:00.000
2012 1900-01-01 00:00:00.000 2013-01-25 00:00:00.000
2012 2013-01-30 00:00:00.000 1900-01-01 00:00:00.000
2012 1900-01-01 00:00:00.000 2013-04-08 00:00:00.000
2012 2013-04-09 00:00:00.000 1900-01-01 00:00:00.000
2012 1900-01-01 00:00:00.000 2013-06-13 00:00:00.000


but desired output Like as below


iSchoolYearCode Quarter Start Day Quarter Grade Closing
2012 2012-08-30 00:00:00.000 2012-11-07 00:00:00.000
2012 2012-11-08 00:00:00.000 2013-01-25 00:00:00.000
2012 2013-01-30 00:00:00.000 2013-04-08 00:00:00.000
2012 2013-04-09 00:00:00.000 2013-06-13 00:00:00.000

kapil_kk
kapil_kk
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2437 Visits: 2763
ok i didnt ran that query..

lemme try again

_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
mickyT
mickyT
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1257 Visits: 3309
Hi

I think you need some way to group the dates into quarters.
I'll let someone else suggest the best method as I don't deal with dates frequently and wouldn't want to put wrong.

Once that is done you should have no problems.
This is a good article by Jeff Moden on pivots http://www.sqlservercentral.com/articles/T-SQL/63681/

Here is a method using NTILE to number the quarters.
NOTE: This assumes that you have complete data for the year. (2 dates per quarter)

;with numberQuarters AS (
select iSchoolYearCode
,NTILE(4) OVER (Partition By iSchoolYearCode ORDER BY dtCalendarDay ) QuarterNum
,dtCalendarDay
,iCalendarDatCategoryID
,vcCalendarCategoryCode
from #temp1
where iCalendarDatCategoryID in (15, 89)
)
select iSchoolYearCode,
MAX(CASE WHEN iCalendarDatCategoryID = 15 THEN dtCalendarDay ELSE NULL END) AS [Quarter Start Day],
MAX(CASE WHEN iCalendarDatCategoryID = 89 THEN dtCalendarDay ELSE NULL END) AS [Quarter Grade Closing]
from numberQuarters
group by iSchoolYearCode, QuarterNum


kapil_kk
kapil_kk
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2437 Visits: 2763
you can try this one now...

;with cte as
(
Select iSchoolYearCode
,dtCalendarDay
,iCalendarDatCategoryID
,vcCalendarCategoryCode
,ROW_NUMBER() OVER(Partition By iSchoolYearCode, iCalendarDatCategoryID Order by dtCalendarDay) RowNumber
From #Temp1
)
Select iSchoolYearCode
,Max(Case
When vcCalendarCategoryCode = 'Quarter Start Day' Then dtCalendarDay
End) As [Quarter Start]
,Max(Case
When vcCalendarCategoryCode = 'Quarter Grade Closing' Then dtCalendarDay
End) As [Quarter end]
From cte
Group By RowNumber, iSchoolYearCode

_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
kapil_kk
kapil_kk
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2437 Visits: 2763
try this too....

;WITH mycte AS (
SELECT iCalendarDatCategoryID, vcCalendarCategoryCode, iSchoolYearCode,dtCalendarDay,
ROW_NUMBER() OVER(PARTITION BY iCalendarDatCategoryID ORDER BY dtCalendarDay) AS rn
FROM #Temp1

)


SELECT iSchoolYearCode, min(dtCalendarDay) as [Quarter Start Day], max(dtCalendarDay) as [Quarter Grade Closing] FROM mycte
group by iSchoolYearCode, rn
Order by min(dtCalendarDay)

it will also work Smile

_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
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