Solved

SQL Pivot Dates

Posted on 2014-09-24
5
125 Views
Last Modified: 2014-09-25
I have data that looks like this

DayNum      DayofWeek      Monthname
1      Tuesday      April
2      Wednesday      April
3      Thursday      April
4      Friday      April
5      Saturday      April
6      Sunday      April
7      Monday      April
8      Tuesday      April
9      Wednesday      April
10      Thursday      April
11      Friday      April
12      Saturday      April
13      Sunday      April
14      Monday      April
15      Tuesday      April
16      Wednesday      April
17      Thursday      April
18      Friday      April
19      Saturday      April
20      Sunday      April
21      Monday      April
22      Tuesday      April
23      Wednesday      April
24      Thursday      April
25      Friday      April
26      Saturday      April
27      Sunday      April
28      Monday      April
29      Tuesday      April
30      Wednesday      April

I want to get formatted in rows like this:

           Sunday            Monday     Tuesday     Wednesday      Thursday       Friday       Saturday     Sunday      Monday
April                                                     1                     2                       3                  4                   5                 6                 7

I tried SQL Pivot but it does not give me the correct results

select *
from 
(
    select DayNum, DayOfWeek, Monthname
    from dim_Date where monthname='April' and yearnum='2014'
) x
pivot
(
    Count(DayNum)
    for DayofWeek in ([Sunday], [Monday], [Tuesday], [Wednesday], [Thursday], [Friday], [Saturday])
) p

Open in new window

0
Comment
Question by:RecipeDan
5 Comments
 
LVL 24

Expert Comment

by:chaau
ID: 40343225
Can you explain why you have two Monday columns?
0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 450 total points
ID: 40343226
| MONTHNAME | SUNDAY | MONDAY | TUESDAY | WEDNESDAY | THURSDAY | FRIDAY | SATURDAY |
|-----------|--------|--------|---------|-----------|----------|--------|----------|
|     April | (null) | (null) |       1 |         2 |        3 |      4 |        5 |
|     April |      6 |      7 |       8 |         9 |       10 |     11 |       12 |
|     April |     13 |     14 |      15 |        16 |       17 |     18 |       19 |
|     April |     20 |     21 |      22 |        23 |       24 |     25 |       26 |
|     April |     27 |     28 |      29 |        30 |   (null) | (null) |   (null) |

Open in new window

That was produced from this query:
select
       d.Monthname
     , MAX(case when DayofWeek = 'Sunday' then DayNum end) as Sunday
     , MAX(case when DayofWeek = 'Monday' then DayNum end) as Monday
     , MAX(case when DayofWeek = 'Tuesday' then DayNum end) as Tuesday
     , MAX(case when DayofWeek = 'Wednesday' then DayNum end) as Wednesday
     , MAX(case when DayofWeek = 'Thursday' then DayNum end) as Thursday
     , MAX(case when DayofWeek = 'Friday' then DayNum end) as Friday
     , MAX(case when DayofWeek = 'Saturday' then DayNum end) as Saturday
from YourTable d
inner join (
            select
                   Monthname
                 , case
                      when DayofWeek = 'Sunday' then 0
                      when DayofWeek = 'Monday' then 1
                      when DayofWeek = 'Tuesday' then 2
                      when DayofWeek = 'Wednesday' then 3
                      when DayofWeek = 'Thursday' then 4
                      when DayofWeek = 'Friday' then 4
                      when DayofWeek = 'Saturday' then 6
                   end as offset
            from YourTable
            where DayNum = 1
            ) d1 on d.Monthname = d1.Monthname
group by
        d.Monthname
      , (DayNum - 1 + d1.offset) /7
;

Open in new window

Set-up details:
CREATE TABLE YourTable
	([DayNum] int, [DayofWeek] varchar(9), [Monthname] varchar(5))
;
	
INSERT INTO YourTable
	([DayNum], [DayofWeek], [Monthname])
VALUES
	(1, 'Tuesday', 'April'),
	(2, 'Wednesday', 'April'),
	(3, 'Thursday', 'April'),
	(4, 'Friday', 'April'),
	(5, 'Saturday', 'April'),
	(6, 'Sunday', 'April'),
	(7, 'Monday', 'April'),
	(8, 'Tuesday', 'April'),
	(9, 'Wednesday', 'April'),
	(10, 'Thursday', 'April'),
	(11, 'Friday', 'April'),
	(12, 'Saturday', 'April'),
	(13, 'Sunday', 'April'),
	(14, 'Monday', 'April'),
	(15, 'Tuesday', 'April'),
	(16, 'Wednesday', 'April'),
	(17, 'Thursday', 'April'),
	(18, 'Friday', 'April'),
	(19, 'Saturday', 'April'),
	(20, 'Sunday', 'April'),
	(21, 'Monday', 'April'),
	(22, 'Tuesday', 'April'),
	(23, 'Wednesday', 'April'),
	(24, 'Thursday', 'April'),
	(25, 'Friday', 'April'),
	(26, 'Saturday', 'April'),
	(27, 'Sunday', 'April'),
	(28, 'Monday', 'April'),
	(29, 'Tuesday', 'April'),
	(30, 'Wednesday', 'April')
;

http://sqlfiddle.com/#!3/0b357/14

Open in new window

0
 
LVL 35

Assisted Solution

by:David Todd
David Todd earned 50 total points
ID: 40343431
Hi,

Labelling date columns is always a pain.

I do this:
Current day is labelled 0, yesterday is labelled 1 (one day ago), the day before is 2 (two days ago)
This means I can always produce a running pivot table - currently out to 200 days.

So, my question is, what are you using as a presentation layer? Crystal reports? SQL Reports? Then this is easy. Convert the column label as I've produced to an int, and create two labels, both with something like dateadd( day, -(converttoint( columnlabel), getdate()) and format one as day of month and the other as the day of week.

HTH
  David
0
 
LVL 1

Author Comment

by:RecipeDan
ID: 40344082
Thank you both for your replies. I will test it and respond back this afternoon.
0
 
LVL 1

Author Closing Comment

by:RecipeDan
ID: 40344929
The solutions works great! Thank you
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question