Reputation: 83
I'm trying to group the results by time intervals (hourly) I cannot for the life of me figure this out. I need the experts to help me, can anyone see where I'm going wrong?
DECLARE @IntervalMinutes AS INT
SELECT @IntervalMinutes = 60
SELECT
CONVERT(VARCHAR(5),DATEADD(MINUTE,(DATEDIFF(MINUTE,'19000101',dbo.cmp_EEAddLines.CallTime) / @IntervalMinutes) * @IntervalMinutes, '19000101'),108) AS CallTime,
COUNT(dbo.Dial.HistoryID) AS TotalDials,
SUM(CONVERT(numeric, dbo.CRCTotal.Contact)) AS Contacts,
SUM(CONVERT(numeric, dbo.CRCTotal.FinalCRC)) AS Completes
FROM
dbo.cmp_EEAddLines LEFT OUTER JOIN
dbo.Dial ON
dbo.cmp_EEAddLines.DialID = dbo.Dial.DialID LEFT OUTER JOIN
dbo.CRCTotal ON
dbo.Dial.CRC = dbo.CRCTotal.CRC
WHERE
(dbo.cmp_EEAddLines.CallDate >= 29/03/2015)
GROUP BY
CallTime
Upvotes: 0
Views: 55
Reputation: 6018
Everyone seems to be breaking up each datetime into each part when in reality, you only need two parts. The date and the hour. Try this:
SELECT CAST(dbo.cmp_EEAddLines.CallTime AS DATE) as dates,
DATEPART(HOUR,dbo.cmp_EEAddLines.CallTime) AS hr
...
GROUP BY CAST(dbo.cmp_EEAddLines.CallTime AS DATE),
DATEPART(HOUR,dbo.cmp_EEAddLines.CallTime)
If you would like it in one column, you can try this instead. It will round each datetime value to it's nearest hour.
SELECT CAST(CONCAT(CAST(dbo.cmp_EEAddLines.CallTime AS DATE),' ',DATEPART(HOUR,dbo.cmp_EEAddLines.CallTime),':00:00.000') AS DATETIME)
....
GROUP BY CAST(CONCAT(CAST(dbo.cmp_EEAddLines.CallTime AS DATE),' ',DATEPART(HOUR,dbo.cmp_EEAddLines.CallTime),':00:00.000') AS DATETIME)
Upvotes: 1
Reputation: 40289
Here's one way to produce a list of "hits" by year/month/day/hour.
TotalDials
without any Contacts
or Completes
The code:
SELECT
datepart(yy, al.CallTime) Year
,datepart(mm, al.CallTime) Month
,datepart(dd, al.CallTime) Day
,datepart(hh, al.CallTime) Hour
,COUNT(di.HistoryID) TotalDials
,SUM(CONVERT(numeric, cr.Contact)) Contacts
,SUM(CONVERT(numeric, cr.FinalCRC)) Completes
FROM
dbo.cmp_EEAddLines al
INNER JOIN dbo.Dial di -- Left join to inner; counts and sums are identical, query will be faster
ON al.DialID = di.DialID
LEFT OUTER JOIN dbo.CRCTotal cr
ON di.CRC = cr.CRC
group by
datepart(yy, al.CallTime) -- Year
,datepart(mm, al.CallTime) -- Month
,datepart(dd, al.CallTime) -- Day
,datepart(hh, al.CallTime) -- Hour
order by
datepart(yy, al.CallTime) -- Year
,datepart(mm, al.CallTime) -- Month
,datepart(dd, al.CallTime) -- Day
,datepart(hh, al.CallTime) -- Hour
Upvotes: 0
Reputation: 1269445
You could break out each of the date parts you want:
select year(e.cmp_EEAddLines.CallTime), month(e.cmp_EEAddLines.CallTime),
day(e.cmp_EEAddLines.CallTime),
datepart(hour, cmp_EEAddLines.CallTime),
. . .
group by year(e.cmp_EEAddLines.CallTime), month(e.cmp_EEAddLines.CallTime),
day(e.cmp_EEAddLines.CallTime),
datepart(hour, cmp_EEAddLines.CallTime)
This doesn't give the final output as a datetime
, but it might work for your needs.
Note: e
is an alias for the table with the column.
Upvotes: 2