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
Solved

SQL Pivot - All Dates

Posted on 2014-02-26
10
381 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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
Stored Proc - Rewrite 42 59
query optimization 6 15
Need help in debugging a UDF results 7 25
SQL View / Qtry 3 12
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.
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.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

828 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