Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12

Grouping by day on a rollover count Expand / Collapse
Author
Message
Posted Friday, July 5, 2013 1:18 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Today @ 3:14 AM
Points: 42, Visits: 164
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

Post #1470601
Posted Monday, July 8, 2013 7:35 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Today @ 3:14 AM
Points: 42, Visits: 164
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.
Post #1471188
Posted Monday, July 15, 2013 5:28 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Thursday, November 6, 2014 5:01 AM
Points: 2,116, Visits: 6,441
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)
Post #1473590
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse