Solved

SQL Pivot - All Dates

Posted on 2014-02-26
10
380 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
ID: 39890182
Can you post a sample of original data?
0
 

Author Comment

by:codyvance1
ID: 39890228
I cannot unfortunately..
0
 
LVL 35

Expert Comment

by:YZlat
ID: 39890266
which data is missing BOOKINGS or INSERT_PERIOD?
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 35

Expert Comment

by:YZlat
ID: 39890284
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
ID: 39890338
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
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39890667
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
ID: 39890676
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
ID: 39890693
Did you try my solution?
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39890698
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
ID: 39890721
This worked perfectly, thank you!!!!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sql server computed columns 11 29
MS SQL BCP Extra Lines Between Records 2 14
optimize stored procedure 6 23
SSRS Enable Remote Errors 4 22
In this article I will describe the Backup & Restore 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.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
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.

813 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