Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Pivot weekdays

Author  Topic 

dabram
Starting Member

14 Posts

Posted - 2010-08-09 : 09:00:36
Hello,

I have table that I selected data for 1 month. The table has two columns, (DateOfEvent and Event). I need to convert the data to a temporary table to show up as:

Col Names - Sunday|Monday|Tuesday|Wednesday|Thursday|Friday|Saturday

with the event under the weekday.

Could someone please point me in the right direction.

Best regards,

slimt_slimt
Aged Yak Warrior

746 Posts

Posted - 2010-08-09 : 09:43:41
try this:

select
NameOfEvent
,[1] as Sun
,[2] as Mon
,[3] as Tue
,[4] as Wed
,[5] as Thu
,[6] as Fri
,[7] as Sat
from
(select
NameOfEvent -- each distinct event will be shown in a row
,count(id) as nof_events --for each event you will count frequencies
,day(MyDate) as day_of_week --day of the week

from MyTable
where
MyDate between '2010/08/01' and '2010/08/09'
group by day(MyDate), NameOfEvent

) as p pivot
( sum(nof_events) for day_of_week in ([1],[2],[3],[4],[5],[6],[7])
) as pivottbl
Go to Top of Page

dabram
Starting Member

14 Posts

Posted - 2010-08-09 : 11:06:43
I tried the code and it works, but not quite the way I wanted it to, my fault.

Right now it counts the frequencies; however, I need it to show what the NameOfEvent is under the column that it belongs in. What I really need then is the first column showing what week it is.

So the table will have the columns "Week","Sun","Mon" etc... Instead of "Event","Sun","Mon" etc...
Then row 1 will show Week 1 and under Column "Sun" show event "Do This", Column "Mon" show event "Feed Fish" etc...
Then row 2 will show Week 2 and under Column "Sun" show event "Fix car", Solumn "Mon" show event "Do whatevever" etc...
Go to Top of Page

slimt_slimt
Aged Yak Warrior

746 Posts

Posted - 2010-08-09 : 11:21:59
please post your table definition and some sample data.

thanks
Go to Top of Page

dabram
Starting Member

14 Posts

Posted - 2010-08-09 : 11:55:50
ID int Unchecked
DateOfEvent date Unchecked
Event nvarchar(50) Unchecked

2 2010-06-01 Fix Car
3 2010-06-02 Walk the dog
4 2010-06-03 Take cat to vet
5 2010-06-04 Take dog to vet
6 2010-06-05 Feed fish
7 2010-06-06 Fix water leak
8 2010-06-07 Do this
10 2010-06-08 Do that
11 2010-06-09 Do whatever
12 2010-06-10 Do nothing
13 2010-06-11 Workday 11
14 2010-06-12 Workday 12
15 2010-06-13 Workday 13
16 2010-06-14 Workday 14
17 2010-06-15 Workday 15

The result I am looking for is:
Week Sun Mon Tue Wed Thu Fri Sat
Week1 Fix Car Walk the dog Take cat Take dog Feed fish
Week2 Fix wa Do this Do that Do whatever
Go to Top of Page

slimt_slimt
Aged Yak Warrior

746 Posts

Posted - 2010-08-09 : 13:47:49
try this:

--CREATE TABLE
create table dabram
(id int identity(1,1)
,DateOfEvent datetime
,event nvarchar(50)
)

--GET SOME DATA
insert into dabram
select '2010-06-01','Fix Car'
union all select '2010-06-02','Walk the dog'
union all select '2010-06-03','Take cat to vet'
union all select '2010-06-04','Take dog to vet'
union all select '2010-06-05','Feed fish'
union all select '2010-06-06','Fix water leak'
union all select '2010-06-07','Do this'
union all select '2010-06-08','Do that'
union all select '2010-06-09','Do whatever'
union all select '2010-06-10','Do nothing'
union all select '2010-06-11','Workday 11'
union all select '2010-06-12','Workday 12'
union all select '2010-06-13','Workday 13'
union all select '2010-06-14','Workday 14'
union all select '2010-06-15','Workday 15'
--(15 row(s) affected)

--ORIGINAL DATA
SELECT *,datepart(week,DateOfEvent) as wk from dabram

--SOLUTION WITH PIVOT
select
wk
,[1] as Sun
,[2] as Mon
,[3] as Tue
,[4] as Wed
,[5] as Thu
,[6] as Fri
,[7] as Sat
from
(select
[Event]
,datepart(week,DateOfEvent) as wk -- week number
,datepart(weekday,DateOfEvent) as day_of_week --day of the week

from dabram

) as p pivot
--use either min(event) or max(event) for distincting the string
( min([event]) for day_of_week in ([1],[2],[3],[4],[5],[6],[7])
) as pivottbl
Go to Top of Page

slimt_slimt
Aged Yak Warrior

746 Posts

Posted - 2010-08-09 : 13:55:24
p.s.: what kind of SQL-{enter_abbreviation} job do you have that you work on sunday and "do nothing" on thursday and "do whatever" on wednesday? :D
Go to Top of Page

dabram
Starting Member

14 Posts

Posted - 2010-08-09 : 14:06:28
Your a Genius! Result is exactly what I needed.

I did manage to get the week number, but that's as far as I got. Now that I see the code, it all makes sense, but when I try doing the select within a select I get it all messed up for some reason. I guess it's gonna take alot of practice or some really good books on how to piece it all together.

Thanks again,

Best regards,
Go to Top of Page

slimt_slimt
Aged Yak Warrior

746 Posts

Posted - 2010-08-09 : 14:10:52
great! :)
Go to Top of Page
   

- Advertisement -