SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Grouping by day on a rollover count


Grouping by day on a rollover count

Author
Message
brett.y
brett.y
SSC Journeyman
SSC Journeyman (94 reputation)SSC Journeyman (94 reputation)SSC Journeyman (94 reputation)SSC Journeyman (94 reputation)SSC Journeyman (94 reputation)SSC Journeyman (94 reputation)SSC Journeyman (94 reputation)SSC Journeyman (94 reputation)

Group: General Forum Members
Points: 94 Visits: 166
This is my code for one column but I cannot work out how to get counts of multiple columns for the 'rollover' count.

DECLARE @T TABLE
(ndx INT IDENTITY ( 1, 1 ) NOT NULL,
open_front INT
,open_rear INT
,wrap_count INT
,t_stamp DATETIME);

INSERT INTO @T VALUES (0,0,0,'2013-01-01 01:00');
INSERT INTO @T VALUES (1,10,1000,'2013-01-01 02:00');
INSERT INTO @T VALUES (1,20,2000,'2013-01-01 03:00');
INSERT INTO @T VALUES (2,100,30000,'2013-01-01 23:59');
INSERT INTO @T VALUES (2,100,31000,'2013-01-02 01:00');
INSERT INTO @T VALUES (4,100,150,'2013-01-02 01:30');
INSERT INTO @T VALUES (500,100,4000,'2013-01-02 03:00');
INSERT INTO @T VALUES (4000,100,31000,'2013-01-02 05:00');
INSERT INTO @T VALUES (7000,100,8,'2013-01-02 08:00');
INSERT INTO @T VALUES (30000,100,4000,'2013-01-02 10:00');
INSERT INTO @T VALUES (8,100,6000,'2013-01-02 21:00');

--The SELECT below adds the three values obtained((32000 - initial) + last value + (rollovers-1*32000))
SELECT ISNULL(32000.0-A.open_front + B.open_front+ISNULL(C.[Rollover_Count],-32000),0) AS [Count], A.Date
FROM --Below is the initial value
(SELECT
ROW_NUMBER() OVER ( PARTITION BY dateadd(DAY,0, datediff(day,0,t_stamp)) ORDER BY t_stamp) AS 'RowNumber',
open_front,
dateadd(DAY,0, datediff(day,0, t_stamp)) AS [Date]
FROM @t
WHERE t_stamp > '2012-12-31 23:59:59' and t_stamp < '2013-07-02 23:59:59')A -- put report dates in here
LEFT OUTER JOIN --Below is the end value
(SELECT
ROW_NUMBER() OVER ( PARTITION BY dateadd(DAY,0, datediff(day,0,t_stamp)) ORDER BY t_stamp DESC ) AS 'RowNumber',
open_front,
dateadd(DAY,0, datediff(day,0, t_stamp))AS [Date]
FROM @t
WHERE t_stamp > '2012-12-31 23:59:59' and t_stamp < '2013-07-02 23:59:59')B
ON A.Date = B.Date
LEFT OUTER JOIN --Below is the number of rollovers
(SELECT (COUNT(*)-1)*32000.0 as [Rollover_Count]
,dateadd(DAY,0, datediff(day,0, t1.t_stamp)) AS [Date]
FROM @t t1
FULL OUTER JOIN @t t2
ON t1.ndx = t2.ndx -1
WHERE t2.open_front < t1.open_front AND t2.open_front IS NOT NULL
AND t1.t_stamp > '2012-12-31 23:59:59' and t1.t_stamp < '2013-07-02 23:59:59' -- Also need report dates here
group by dateadd(DAY,0, datediff(day,0, t1.t_stamp)))C
ON B.Date = C.Date
WHERE A.Rownumber = 1 AND B.Rownumber = 1
ORDER BY A.Date


brett.y
brett.y
SSC Journeyman
SSC Journeyman (94 reputation)SSC Journeyman (94 reputation)SSC Journeyman (94 reputation)SSC Journeyman (94 reputation)SSC Journeyman (94 reputation)SSC Journeyman (94 reputation)SSC Journeyman (94 reputation)SSC Journeyman (94 reputation)

Group: General Forum Members
Points: 94 Visits: 166
Sorted it. Jans solution was correct. The difference in our results comes from the query using the differnece between the last value from the previous day and the first value for the grouped day in the total for the day being grouped on whereas my calculations on paper were done with just the values with the chosen date in the t_stamp regardless of the value from the day before!!!

Thanks Jan and sorry for the mix up.
Jan Van der Eecken
Jan Van der Eecken
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2430 Visits: 6494
No problem, Brett. Glad you managed to sort it out.

--------------------------------------------------------------------------
A little knowledge is a dangerous thing (Alexander Pope)

In order for us to help you as efficiently as possible, please read this before posting (courtesy of Jeff Moden)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search