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.
| 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|Saturdaywith 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 Satfrom(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 weekfrom MyTablewhere 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 |
 |
|
|
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... |
 |
|
|
slimt_slimt
Aged Yak Warrior
746 Posts |
Posted - 2010-08-09 : 11:21:59
|
| please post your table definition and some sample data.thanks |
 |
|
|
dabram
Starting Member
14 Posts |
Posted - 2010-08-09 : 11:55:50
|
| ID int UncheckedDateOfEvent date UncheckedEvent nvarchar(50) Unchecked2 2010-06-01 Fix Car3 2010-06-02 Walk the dog4 2010-06-03 Take cat to vet5 2010-06-04 Take dog to vet6 2010-06-05 Feed fish7 2010-06-06 Fix water leak8 2010-06-07 Do this10 2010-06-08 Do that11 2010-06-09 Do whatever12 2010-06-10 Do nothing13 2010-06-11 Workday 1114 2010-06-12 Workday 1215 2010-06-13 Workday 1316 2010-06-14 Workday 1417 2010-06-15 Workday 15The result I am looking for is:Week Sun Mon Tue Wed Thu Fri SatWeek1 Fix Car Walk the dog Take cat Take dog Feed fish Week2 Fix wa Do this Do that Do whatever |
 |
|
|
slimt_slimt
Aged Yak Warrior
746 Posts |
Posted - 2010-08-09 : 13:47:49
|
try this:--CREATE TABLEcreate table dabram(id int identity(1,1),DateOfEvent datetime,event nvarchar(50))--GET SOME DATAinsert 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 DATASELECT *,datepart(week,DateOfEvent) as wk from dabram--SOLUTION WITH PIVOTselect wk ,[1] as Sun ,[2] as Mon ,[3] as Tue ,[4] as Wed ,[5] as Thu ,[6] as Fri ,[7] as Satfrom(select [Event] ,datepart(week,DateOfEvent) as wk -- week number ,datepart(weekday,DateOfEvent) as day_of_week --day of the weekfrom 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 |
 |
|
|
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 |
 |
|
|
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, |
 |
|
|
slimt_slimt
Aged Yak Warrior
746 Posts |
Posted - 2010-08-09 : 14:10:52
|
| great! :) |
 |
|
|
|
|
|
|
|