Solved

SQL Pivot Dates

Posted on 2014-09-24
5
121 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
Comment Utility
Can you explain why you have two Monday columns?
0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 450 total points
Comment Utility
| 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
Comment Utility
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
Comment Utility
Thank you both for your replies. I will test it and respond back this afternoon.
0
 
LVL 1

Author Closing Comment

by:RecipeDan
Comment Utility
The solutions works great! Thank you
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

771 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now