Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL Pivot Dates

Posted on 2014-09-24
5
Medium Priority
?
128 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 1800 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 200 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

705 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