I'm writing code on SQL Server 2014 to pull records for a single day. I will then be using Report Builder to create the report.
I'm not sure if my code is right. The times of the shifts are in 'yyyy-mm-dd hh:mm:sss' format starting at 7:45am to 6:15pm. I need to count total calls (and other fields) for that time period per day. I will also need to give a parameter so the viewer can select the date range for the report.
I created a function to convert total seconds to HH:MM:SS format. The code for the function is:
CREATE FUNCTION dbo.UDF_SecondsToHMS(@Seconds int) RETURNS table AS
RETURN (
SELECT CONVERT(VARCHAR(20), @Seconds / 3600) + ':'
+ RIGHT('00' + CONVERT(VARCHAR(2), (@Seconds % 3600) / 60) ,2) + ':'
+ RIGHT('00' + CONVERT(VARCHAR(2), @Seconds%60), 2) AS [HMS] );
The code I have tried is:
SELECT
SUM(CallsAnswered) CallsAnswered
,SUM(TotalCalls) TotalCalls
,(SUM(TimeToAnswerTotal) / 600) [ASA] --> How do I call the function here too?
----------------------- (I called the function here)
,(SELECT
(SELECT HMS
FROM [UDF_SecondsToHMS](SUM (DATEDIFF(second, AgentLoginTime, AgentLogoutTime)))) [HMS]
FROM [AgentStats]
WHERE DATEADD(dd, 0, DATEDIFF(dd, 0, StartDate)) = CAST('2017-03-08' AS DATETIME)
) [Actual Shift Hrs.]
----------------------- (End of the function here. I had help calling this function here.)
,(SUM(ACDDuration) / 600) [Avg. ACD Time] --> How do I call the function here too?
FROM [CallsByPeriodStats]
WHERE DateOfCalls >= @Report_Date --typically the date shows as '2017-02-22 00:00:000'
AND DateOfCalls < @Report_Date + 1 day of the month --typically it should be '2017-02-23 00:00:000'
--but what if @Report_Date is last day of month?
GROUP BY CAST[StartDate as DATE]
- How do I call the function I created to convert seconds to hh:mm:ss in the 2 places I indicated above?
- If I give a date range, how will it take it if the date is the last date of the month?
Something like this ?
Your function is the equivalent of
convert(varchar(8),dateadd(second,@seconds,0),114)
rextester demo: http://rextester.com/BCLY8438
Your function currently returns a table. I'm not sure if that is required but you will probably need a function that just returns either a string representing the duration in HMS or a Time value representing the duration in HMS
Create another function something like this..
This will just give you text back, so you won't be able to any calculation directly on it, this will be fine if you just need it showing 'as-is' on a report.
To use the function just replace anything that currently is returning seconds with the function and the original expression passed as the parameter e.g.
would become
To select only data from date, simply cast the two datetime values to date types when you compare them. Basically this give you only the date portion of the datetime so you can compare it directly with the passed in date.
becomes