Solved

SQL Pivot - All Dates

Posted on 2014-02-26
10
377 Views
Last Modified: 2014-02-26
I currently have SQL that creates a pivot based on PERIOD, but excludes dates (INS_DATE) where no data exists.  How can I get it to show all dates whether data exists or not?

WITH BASEQUERY AS(
SELECT INS_DATE, CONVERT(VARCHAR(5),INSERT_PERIOD,108) as PERIOD, BOOKINGS
FROM SUMMARY
WHERE CATEGORY IN('WEB')
)
SELECT * FROM BASEQUERY
pivot (SUM(BOOKINGS) for PERIOD in (
[00:00],[00:30],[01:00],[01:30],[02:00],[02:30],
[03:00],[03:30],[04:00],[04:30],[05:00],[05:30],
[06:00],[06:30],[07:00],[07:30],[08:00],[08:30],
[09:00],[09:30],[10:00],[10:30],[11:00],[11:30],
[12:00],[12:30],[13:00],[13:30],[14:00],[14:30],
[15:00],[15:30],[16:00],[16:30],[17:00],[17:30],
[18:00],[18:30],[19:00],[19:30],[20:00],[20:30],
[21:00],[21:30],[22:00],[22:30],[23:00],[23:30])) AS SUMBOOKINGSPERPERIOD
ORDER BY 1 ASC

Open in new window

0
Comment
Question by:codyvance1
  • 4
  • 3
  • 3
10 Comments
 
LVL 35

Expert Comment

by:YZlat
Comment Utility
Can you post a sample of original data?
0
 

Author Comment

by:codyvance1
Comment Utility
I cannot unfortunately..
0
 
LVL 35

Expert Comment

by:YZlat
Comment Utility
which data is missing BOOKINGS or INSERT_PERIOD?
0
 
LVL 35

Expert Comment

by:YZlat
Comment Utility
What you can do is replace the null values with a string:

WITH BASEQUERY AS(
SELECT INS_DATE, CONVERT(VARCHAR(5),INSERT_PERIOD,108) as PERIOD, ISNULL(BOOKINGS, 'NULL') as BOOKINGS
FROM SUMMARY
WHERE CATEGORY IN('WEB')
)
SELECT * FROM BASEQUERY
pivot (SUM(BOOKINGS) for PERIOD in (
[00:00],[00:30],[01:00],[01:30],[02:00],[02:30],
[03:00],[03:30],[04:00],[04:30],[05:00],[05:30],
[06:00],[06:30],[07:00],[07:30],[08:00],[08:30],
[09:00],[09:30],[10:00],[10:30],[11:00],[11:30],
[12:00],[12:30],[13:00],[13:30],[14:00],[14:30],
[15:00],[15:30],[16:00],[16:30],[17:00],[17:30],
[18:00],[18:30],[19:00],[19:30],[20:00],[20:30],
[21:00],[21:30],[22:00],[22:30],[23:00],[23:30])) AS SUMBOOKINGSPERPERIOD
ORDER BY 1 ASC

Open in new window

0
 

Author Comment

by:codyvance1
Comment Utility
Here is an example, as you can see it starts at 2/12/13 since there is no data for 1/1/13-2/11/13.  I need it to show these dates, even if it shows NULL all the way across.

Data
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 48

Expert Comment

by:PortletPaul
Comment Utility
With respect to the dates that are not present is the results:

You need a table of dates first, then left join your data to that.

This table of dates could be a permanent feature or something temporary (e.g. a recursive CTE)

----
posting data: >>"I cannot unfortunately.."

but you could post "sample data" (or "representative data") that is stripped of anything private.
0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 total points
Comment Utility
here is an article discussing creation of a date table
http://www.experts-exchange.com/Database/MS-SQL-Server/A_12267-Date-Fun-Part-One-Build-your-own-SQL-calendar-table-to-perform-complex-date-expressions.html

& here is sample code using a recursive CTE
-- establish a date range
DECLARE @fromdate datetime
      , @todate datetime
SELECT
        @fromdate = '20130901' 
      , @todate   = '20131001'

;WITH
        DateCTE (theDate)
        AS (
                SELECT
                        @fromdate AS theDate
                UNION ALL
                        SELECT
                                DATEADD(DAY, 1, theDate)
                        FROM DateCTE
                        WHERE theDate < dateadd(day,-1,@todate)
                )
SELECT
        *
FROM DateCTE
left join SUMMARY on DateCTE.theDate = SUMMARY.INS_DATE
...

Open in new window

0
 
LVL 35

Expert Comment

by:YZlat
Comment Utility
Did you try my solution?
0
 
LVL 48

Expert Comment

by:PortletPaul
Comment Utility
YZlat, the question as I understand it is about missing rows.

>>"How can I get it to show all dates whether data exists or not?"
0
 

Author Closing Comment

by:codyvance1
Comment Utility
This worked perfectly, 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

Suggested Solutions

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
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.

763 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

11 Experts available now in Live!

Get 1:1 Help Now