Solved

SQL Pivot Dates

Posted on 2014-09-24
5
126 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 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL: get ride of blank rows 11 23
How to get the range for the percentage from user input. 4 26
Report 8 27
Section based report in SSRS 14 35
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.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

751 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