Solved

SQL Pivot Dates

Posted on 2014-09-24
5
127 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 25

Expert Comment

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

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

Get MySQL database support online, now!

At Percona’s web store you can order your MySQL database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

622 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