Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- select datepart(Year, dev_time) as Year,
- datepart(Month, dev_time) as Month,
- datepart(Day, dev_time) as Day,
- datepart(Hour, dev_time) as Hour,
- count(tdm_msg) as Total_ACTIVITES
- from TCKT_ACT
- where tdm_msg = ‘4162′ and dev_time >= DATEADD(day, - 1, GETDATE())
- group by datepart(Year, dev_time) ,
- datepart(Month, dev_time) ,
- datepart(Day, dev_time),
- datepart(Hour, dev_time)
- order by datepart(Year, dev_time) asc,
- datepart(Month, dev_time) asc,
- datepart(Day, dev_time) asc,
- datepart(Hour, dev_time) asc
- DECLARE @MinDate DATETIME;
- SET @MinDate = CONVERT(varchar, GETDATE(), 101);
- WITH times AS (
- SELECT @MinDate as dt, 1 as depth
- UNION ALL
- SELECT DATEADD(hh, depth, @MinDate), 1 + depth as depth
- FROM times
- WHERE DATEADD(hh, depth, @MinDate) <= GETDATE())
- SELECT DATEPART(YEAR, t.dt) as [Year],
- DATEPART(MONTH, t.dt) as [Month],
- DATEPART(DAY, t.dt) as [Day],
- DATEPART(HOUR, t.dt) as [Hour],
- COUNT(tdm_msg) as Total_ACTIVITES
- FROM times t
- LEFT JOIN (SELECT * FROM TCKT_ACT WHERE tdm_msg = '4162' and dev_time >= @MinDate) a
- ON DATEPART(HOUR, t.dt) = DATEPART(HOUR, a.dev_time)
- AND MONTH(t.dt) = MONTH(a.dev_time)
- AND DAY(t.dt) = DAY(a.dev_time)
- AND YEAR(t.dt) = YEAR(a.dev_time)
- GROUP BY DATEPART(YEAR, t.dt) ,
- DATEPART(MONTH, t.dt) ,
- DATEPART(DAY, t.dt),
- DATEPART(HOUR, t.dt)
- ORDER BY DATEPART(YEAR, t.dt) asc,
- DATEPART(MONTH, t.dt) asc,
- DATEPART(DAY, t.dt) asc,
- DATEPART(HOUR, t.dt) asc
- OPTION (MAXRECURSION 0); /* Just in case you want a longer timespan later on... */
- if exists (select * from dbo.sysobjects where name = 'fn_daterange') drop function fn_daterange;
- go
- create function fn_daterange
- (
- @MinDate as datetime,
- @MaxDate as datetime,
- @intval as datetime
- )
- returns table
- --**************************************************************************
- -- Procedure: fn_daterange()
- -- Author: Ron Savage
- -- Date: 12/16/2008
- --
- -- Description:
- -- This function takes a starting and ending date and an interval, then
- -- returns a table of all the dates in that range at the specified interval.
- --
- -- Change History:
- -- Date Init. Description
- -- 12/16/2008 RS Created.
- -- **************************************************************************
- as
- return
- WITH times (startdate, enddate, intervl) AS
- (
- SELECT @MinDate as startdate, @MinDate + @intval - .0000001 as enddate, @intval as intervl
- UNION ALL
- SELECT startdate + intervl as startdate, enddate + intervl as enddate, intervl as intervl
- FROM times
- WHERE startdate + intervl <= @MaxDate
- )
- select startdate, enddate from times;
- go
- startdate enddate intervl
- ----------------------- ----------------------- -----------------------
- 2008-12-14 10:00:00.000 2008-12-14 10:59:59.997 1900-01-01 01:00:00.000
- 2008-12-14 11:00:00.000 2008-12-14 11:59:59.997 1900-01-01 01:00:00.000
- 2008-12-14 12:00:00.000 2008-12-14 12:59:59.997 1900-01-01 01:00:00.000
- 2008-12-14 13:00:00.000 2008-12-14 13:59:59.997 1900-01-01 01:00:00.000
- 2008-12-14 14:00:00.000 2008-12-14 14:59:59.997 1900-01-01 01:00:00.000
- 2008-12-14 15:00:00.000 2008-12-14 15:59:59.997 1900-01-01 01:00:00.000
- 2008-12-14 16:00:00.000 2008-12-14 16:59:59.997 1900-01-01 01:00:00.000
- 2008-12-14 17:00:00.000 2008-12-14 17:59:59.997 1900-01-01 01:00:00.000
- 2008-12-14 18:00:00.000 2008-12-14 18:59:59.997 1900-01-01 01:00:00.000
- 2008-12-14 19:00:00.000 2008-12-14 19:59:59.997 1900-01-01 01:00:00.000
- 2008-12-14 20:00:00.000 2008-12-14 20:59:59.997 1900-01-01 01:00:00.000
- eventdate eventnote
- ----------------------- --------------------
- 2008-12-14 10:01:00.000 oo! an event!
- 2008-12-14 10:01:00.000 oo! an event!
- 2008-12-14 10:01:00.000 oo! an event!
- 2008-12-14 10:01:00.000 oo! an event!
- 2008-12-14 10:23:00.000 oo! an event!
- 2008-12-14 10:23:00.000 oo! an event!
- 2008-12-14 10:23:00.000 oo! an event!
- 2008-12-14 11:23:00.000 oo! an event!
- 2008-12-14 11:23:00.000 oo! an event!
- 2008-12-14 11:23:00.000 oo! an event!
- 2008-12-14 11:23:00.000 oo! an event!
- 2008-12-14 11:23:00.000 oo! an event!
- 2008-12-14 14:23:00.000 oo! an event!
- 2008-12-14 14:23:00.000 oo! an event!
- 2008-12-14 14:23:00.000 oo! an event!
- 2008-12-14 19:23:00.000 oo! an event!
- 2008-12-14 19:23:00.000 oo! an event!
- 2008-12-14 19:23:00.000 oo! an event!
- 2008-12-14 19:23:00.000 oo! an event!
- 2008-12-14 19:00:00.000 oo! an event!
- 2008-12-14 19:00:00.000 oo! an event!
- 2008-12-14 19:00:00.000 oo! an event!
- 22 Row(s) affected
- select
- dr.startdate,
- dr.enddate,
- count(me.eventdate) as eventcount
- from
- fn_daterange('12/14/2008 10:00:00', '12/14/2008 20:00:00', '01:00:00' ) dr
- LEFT OUTER JOIN myevents me
- on ( me.eventdate between dr.startdate and dr.enddate)
- group by
- dr.startdate,
- dr.enddate
- startdate enddate eventcount
- ----------------------- ----------------------- ----------
- 2008-12-14 10:00:00.000 2008-12-14 10:59:59.993 7
- 2008-12-14 11:00:00.000 2008-12-14 11:59:59.993 5
- 2008-12-14 12:00:00.000 2008-12-14 12:59:59.993 0
- 2008-12-14 13:00:00.000 2008-12-14 13:59:59.993 0
- 2008-12-14 14:00:00.000 2008-12-14 14:59:59.993 3
- 2008-12-14 15:00:00.000 2008-12-14 15:59:59.993 0
- 2008-12-14 16:00:00.000 2008-12-14 16:59:59.993 0
- 2008-12-14 17:00:00.000 2008-12-14 17:59:59.993 0
- 2008-12-14 18:00:00.000 2008-12-14 18:59:59.993 0
- 2008-12-14 19:00:00.000 2008-12-14 19:59:59.993 7
- 2008-12-14 20:00:00.000 2008-12-14 20:59:59.993 0
- 11 Row(s) affected
Add Comment
Please, Sign In to add comment