The OrangeGoblin
The OrangeGoblin

Reputation: 794

Pivot query will only return data from the 'FromDate' and not between two dates

I have a query as follows:

DECLARE @_DateFrom DATETIME
DECLARE @_DateTo DATETIME
DECLARE @_SerialNumber NVARCHAR(MAX)

SET @_DateFrom = '2018-10-20 00:00:00'
SET @_DateTo = '2018-10-21 00:00:00'
SET @_SerialNumber = '2209'

    SELECT [Serial], 
       [Channel],
       [ReadingDate],
       [00:15],[00:30],[00:45],[01:00],[01:15],[01:30],[01:45],[02:00],[02:15],[02:30],[02:45],[03:00],
       [03:15],[03:30],[03:45],[04:00],[04:15],[04:30],[04:45],[05:00],[05:15],[05:30],[05:45],[06:00],
       [06:15],[06:30],[06:45],[07:00],[07:15],[07:30],[07:45],[08:00],[08:15],[08:30],[08:45],[09:00],
       [09:15],[09:30],[09:45],[10:00],[10:15],[10:30],[10:45],[11:00],[11:15],[11:30],[11:45],[12:00],
       [12:15],[12:30],[12:45],[13:00],[13:15],[13:30],[13:45],[14:00],[14:15],[14:30],[14:45],[15:00],
       [15:15],[15:30],[15:45],[16:00],[16:15],[16:30],[16:45],[17:00],[17:15],[17:30],[17:45],[18:00],
       [18:15],[18:30],[18:45],[19:00],[19:15],[19:30],[19:45],[20:00],[20:15],[20:30],[20:45],[21:00],
       [21:15],[21:30],[21:45],[22:00],[22:15],[22:30],[22:45],[23:00],[23:15],[23:30],[23:45],[00:00]
    FROM(
        SELECT 
            SerialNumber AS [Serial],
            ChannelName AS [Channel],
            (CASE WHEN 
                    CAST(ReadingDate AS DATE) > CAST(@_DateFrom AS DATE) 
                    THEN CAST(@_DateFrom AS DATE) 
                    ELSE CAST(ReadingDate AS DATE)
            END) AS [ReadingDate],
            CAST(ReadingDate AS TIME) AS [ReadingTime],
            ChannelValue AS [Value]
        FROM [UriData]
        WHERE ReadingDate BETWEEN @_DateFrom AND @_DateTo       
        AND SerialNumber = @_SerialNumber
        AND ChannelName IN (SELECT ChannelName FROM [Staging].[ActiveChannels]) 
        ) AS [Raw]
    PIVOT
    (
        MAX( [Value] ) FOR [ReadingTime] IN( [00:15],[00:30],[00:45],[01:00],[01:15],[01:30],[01:45],[02:00],
                                         [02:15],[02:30],[02:45],[03:00],[03:15],[03:30],[03:45],[04:00],
                                         [04:15],[04:30],[04:45],[05:00],[05:15],[05:30],[05:45],[06:00],
                                         [06:15],[06:30],[06:45],[07:00],[07:15],[07:30],[07:45],[08:00],
                                         [08:15],[08:30],[08:45],[09:00],[09:15],[09:30],[09:45],[10:00],
                                         [10:15],[10:30],[10:45],[11:00],[11:15],[11:30],[11:45],[12:00],
                                         [12:15],[12:30],[12:45],[13:00],[13:15],[13:30],[13:45],[14:00],
                                         [14:15],[14:30],[14:45],[15:00],[15:15],[15:30],[15:45],[16:00],
                                         [16:15],[16:30],[16:45],[17:00],[17:15],[17:30],[17:45],[18:00],
                                         [18:15],[18:30],[18:45],[19:00],[19:15],[19:30],[19:45],[20:00],
                                         [20:15],[20:30],[20:45],[21:00],[21:15],[21:30],[21:45],[22:00],
                                         [22:15],[22:30],[22:45],[23:00],[23:15],[23:30],[23:45],[00:00])
    ) AS pvt    
    ORDER BY ReadingDate DESC, Channel, [Serial]

The query only returns data for the @_DateFrom variable and will not return any other data beyond that i.e between two dates, so the above query will only return data for '2018-10-20 00:00:00'. I have tried adding it on SQL Fiddle but the site doesn't seem to be working again. I have a feeling it is the case statement that is causing it, but I am struggling to rectify it.

So I have a Dropbox link that has the query, the data used and the expected output https://www.dropbox.com/sh/odtn35jo6sjhqde/AAChn5Fw7OgrCIyI70XiJ-msa?dl=0

Upvotes: 0

Views: 59

Answers (3)

The OrangeGoblin
The OrangeGoblin

Reputation: 794

declare @_SerialNumber NVARCHAR(MAX) = '2209'   
DECLARE @_DateFrom DATETIME
DECLARE @_DateTo DATETIME

SET @_DateFrom = '2018-10-01 00:15:00'
SET @_DateTo = '2018-10-28 00:15:00'


    SELECT [Serial], 
       [Channel],
       [ReadingDate],
       [00:15],[00:30],[00:45],[01:00],[01:15],[01:30],[01:45],[02:00],[02:15],[02:30],[02:45],[03:00],
       [03:15],[03:30],[03:45],[04:00],[04:15],[04:30],[04:45],[05:00],[05:15],[05:30],[05:45],[06:00],
       [06:15],[06:30],[06:45],[07:00],[07:15],[07:30],[07:45],[08:00],[08:15],[08:30],[08:45],[09:00],
       [09:15],[09:30],[09:45],[10:00],[10:15],[10:30],[10:45],[11:00],[11:15],[11:30],[11:45],[12:00],
       [12:15],[12:30],[12:45],[13:00],[13:15],[13:30],[13:45],[14:00],[14:15],[14:30],[14:45],[15:00],
       [15:15],[15:30],[15:45],[16:00],[16:15],[16:30],[16:45],[17:00],[17:15],[17:30],[17:45],[18:00],
       [18:15],[18:30],[18:45],[19:00],[19:15],[19:30],[19:45],[20:00],[20:15],[20:30],[20:45],[21:00],
       [21:15],[21:30],[21:45],[22:00],[22:15],[22:30],[22:45],[23:00],[23:15],[23:30],[23:45],[00:00]
    FROM(
        SELECT 
            SerialNumber AS [Serial],
            ChannelName AS [Channel],
            (CASE WHEN 
                    CAST(ReadingDate AS TIME) = '00:00:00'  
                    THEN CAST(DATEADD(DAY, -1, ReadingDate) AS DATE)  
                    ELSE CAST(ReadingDate AS DATE)
            END) AS [ReadingDate],
            CAST(ReadingDate AS TIME) AS [ReadingTime],
            ChannelValue AS [Value]
        FROM [Staging].[UriData]
        WHERE ReadingDate BETWEEN @_DateFrom AND @_DateTo
        AND SerialNumber = @_SerialNumber   
        and ChannelName = 'v1'
        ) AS [Raw]

    PIVOT
    (
        MAX( [Value] ) FOR [ReadingTime] IN([00:15],[00:30],[00:45],[01:00],[01:15],[01:30],[01:45],[02:00],
                                         [02:15],[02:30],[02:45],[03:00],[03:15],[03:30],[03:45],[04:00],
                                         [04:15],[04:30],[04:45],[05:00],[05:15],[05:30],[05:45],[06:00],
                                         [06:15],[06:30],[06:45],[07:00],[07:15],[07:30],[07:45],[08:00],
                                         [08:15],[08:30],[08:45],[09:00],[09:15],[09:30],[09:45],[10:00],
                                         [10:15],[10:30],[10:45],[11:00],[11:15],[11:30],[11:45],[12:00],
                                         [12:15],[12:30],[12:45],[13:00],[13:15],[13:30],[13:45],[14:00],
                                         [14:15],[14:30],[14:45],[15:00],[15:15],[15:30],[15:45],[16:00],
                                         [16:15],[16:30],[16:45],[17:00],[17:15],[17:30],[17:45],[18:00],
                                         [18:15],[18:30],[18:45],[19:00],[19:15],[19:30],[19:45],[20:00],
                                         [20:15],[20:30],[20:45],[21:00],[21:15],[21:30],[21:45],[22:00],
                                         [22:15],[22:30],[22:45],[23:00],[23:15],[23:30],[23:45],[00:00])

    ) AS pvt    
    ORDER BY ReadingDate DESC, Channel, [Serial]

Upvotes: 0

iSR5
iSR5

Reputation: 3498

I'm gussing that ReadingDate is a DATETIME column. if true. Then, your issue is in this line :

SET @_DateFrom = '2018-10-20 00:00:00'
SET @_DateTo = '2018-10-21 00:00:00'

you're starting from the beginning of 2018-10-20 12AM O'Clock , and ending at 2018-10-21 at midnight (this is a 24 hours). if you want to include the 2018-10-21 in your results, you must do something like this :

SET @_DateFrom = '2018-10-20 00:00:00'
SET @_DateTo = '2018-10-21 23:59:59'

So, you start at 2018-10-20 12AM O'Clock, and end at 2018-10-21 23:59:59 PM (Which is closer to 12AM O'Clock). (This is 48 hours, more or less).

This will include both dates records.

Upvotes: 0

Gordon Linoff
Gordon Linoff

Reputation: 1270463

Based on this logic:

       (CASE WHEN 
                CAST(ReadingDate AS DATE) > CAST(@_DateFrom AS DATE) 
                THEN CAST(@_DateFrom AS DATE) 
                ELSE CAST(ReadingDate AS DATE)
        END) AS [ReadingDate],

If ReadingDate is ever larger than @_DateFrom, then it is set to @_DateFrom. That would seem to be the culprit.

Upvotes: 1

Related Questions