[Edit from #marcus-vinicius-pompeu: I rewrote the query for better identation. To bypass 'mostly code' constraint, I changed the original group by logic]
I am totally stuck while writing a query. Required output is attached. Query I have written is as below. it is working fine till column SL_30_Sec. How to add two columns ASA & AHT based on average values of each date
SELECT
Date(t.call_date) as created_date,
--
Sum(CASE WHEN t.table_name = 'one' THEN 1 ELSE 0 END) AS Offered_Calls,
Sum(CASE WHEN t.table_name = 'two' THEN 1 ELSE 0 END) AS Answered_Calls,
--
Concat(
Round(
(
Sum(CASE WHEN t.table_name = 'two' THEN 1 ELSE 0 END) /
Sum(CASE WHEN t.table_name = 'one' THEN 1 ELSE 0 END) * 100
), 2
), '%'
) AS Success_Rate,
--
Sum(CASE WHEN t.table_name = 'three' THEN 1 ELSE 0 END) AS OB_Calls,
Sum(CASE WHEN t.table_name = 'four' THEN 1 ELSE 0 END) AS OB_Calls_Received,
Sum(CASE WHEN t.table_name = 'five' THEN 1 ELSE 0 END) AS Calls_ANS_in_30_Sec,
--
Concat(
Round(
(
Sum(CASE WHEN t.table_name = 'five' THEN 1 ELSE 0 END) /
Sum(CASE WHEN t.table_name = 'one' THEN 1 ELSE 0 END) * 100
), 2
), '%'
) AS SL_30_Sec
FROM
(
SELECT call_date, 'one' AS table_name FROM table_1
WHERE call_date BETWEEN '2020-01-01' AND '2020-01-31 23:59' AND campaign_id = 'abc_inbound'
-- -----
UNION ALL
-- -----
SELECT call_date, 'two' AS table_name FROM table_1
WHERE call_date BETWEEN '2020-01-01' AND '2020-01-31 23:59' AND campaign_id = 'abc_inbound' AND term_reason IN('agent', 'caller')
-- -----
UNION ALL
-- -----
SELECT call_date, 'three' AS table_name FROM table_2
WHERE call_date BETWEEN '2020-01-01' AND '2020-01-31 23:59' AND campaign_id = 'xyz'
-- -----
UNION ALL
-- -----
SELECT call_date, 'four' AS table_name FROM table_2
WHERE call_date BETWEEN '2020-01-01' AND '2020-01-31 23:59' AND campaign_id = 'xyz' AND term_reason IN('agent', 'caller')
-- -----
UNION ALL
-- -----
SELECT call_date, 'five' AS table_name FROM table_1
WHERE call_date BETWEEN '2020-01-01' AND '2020-01-31 23:59' AND campaign_id = 'abc_inbound' AND term_reason IN('agent', 'caller', 'ABANDON') AND queue_seconds <= 30
) t
GROUP BY Date(t.call_date)
Required Output
You cannot, given the dataset...
Table_1 = date of offered calls
Table_2 = date of answered calls
Table_3 = date of outbound calls
Table_4 = date of offered outbound calls
Table_5 = date of answered outbound calls in less than 30 seconds
To compute:
ASA - you have to have the time of answer time (you should have, since the data allows for filtering those answered in 30 seconds or below)
AHT - you have to have answer time and talk & wrap time
For not outbound calls it seems we already have answer time from table_1.queue_seconds...
Links for definitions:
https://www.genroe.com/resources/glossary/call-centre-definitions-and-glossary
https://www.callcentrehelper.com/how-to-measure-average-handling-time-52403.htm
You can try this.
SELECT ((SUM(ASA) +SUM(AHT))/count(*))
FROM tableName
GROUP BY date
Related
I have a simple query that counts different types of tickets by departments.
There are 5 departments. The query currently groups by dept.
SELECT
Dept_Name, COUNT(vsrv.TicketNbr) AS TotalTicketsSubmitted,
SUM(CASE WHEN vsrv.Closed_Flag = 1 THEN 1 ELSE 0 END) AS TotalTicketsClosed,
SUM(CASE WHEN vsrv.Closed_Flag = 0 THEN 1 ELSE 0 END) AS TotalOpenTickets
FROM
v_rpt_service vsrv
LEFT OUTER JOIN
v_rpt_SurveysByTicket vsrvy ON vsrv.TicketNbr = Vsrvy.SR_Service_RecID
WHERE
Dept_Name IN ('Application', 'Support', 'Service', 'Development', 'IT')
GROUP BY
Dept_Name
What I need is to do is group the ticket counts so Group1 is just 'Application' and Group2 is the rest of the departments.
Is there a way to define the groups by the Dept_Name?
Something like this if possible...
SELECT
Dept_Name, COUNT(vsrv.TicketNbr) AS TotalTicketsSubmitted,
SUM(CASE WHEN vsrv.Closed_Flag = 1 THEN 1 ELSE 0 END) AS TotalTicketsClosed,
SUM(CASE WHEN vsrv.Closed_Flag = 0 THEN 1 ELSE 0 END) AS TotalOpenTickets
FROM
v_rpt_service vsrv
LEFT OUTER JOIN
v_rpt_SurveysByTicket vsrvy ON vsrv.TicketNbr = Vsrvy.SR_Service_RecID
WHERE
Dept_Name IN ('Application', 'Support', 'Service', 'Development', 'IT')
GROUP BY GROUPING SETS
((Dept_Name), ('Application'), ('Support', 'Service', 'Development', 'IT'))
You can use grouping for this. Something like this should work for you.
SELECT CASE WHEN Dept_Name = 'Application' then Dept_Name else 'Group2' end as DepartmentName
, COUNT(vsrv.TicketNbr) AS TotalTicketsSubmitted
, SUM(CASE WHEN vsrv.Closed_Flag = 1 THEN 1 ELSE 0 END) AS TotalTicketsClosed
, SUM(CASE WHEN vsrv.Closed_Flag = 0 THEN 1 ELSE 0 END) AS TotalOpenTickets
FROM v_rpt_service vsrv LEFT OUTER JOIN v_rpt_SurveysByTicket vsrvy ON vsrv.TicketNbr = Vsrvy.SR_Service_RecID
WHERE Dept_Name in ('Application', 'Support', 'Service', 'Development', 'IT')
GROUP BY CASE WHEN Dept_Name = 'Application' then Dept_Name else 'Group2' end
Something like this might be helpful for you. I assumed the DEPT_NAME column is from v_rpt_service. If not replace it with v_rpt_SurveysByTicket
SELECT Dept_Name, COUNT(vsrv.TicketNbr) AS TotalTicketsSubmitted,
SUM(CASE WHEN vsrv.Closed_Flag = 1 THEN 1 ELSE 0 END) AS TotalTicketsClosed,
SUM(CASE WHEN vsrv.Closed_Flag = 0 THEN 1 ELSE 0 END) AS TotalOpenTickets
FROM (
Select v_rpt_service.* -- all fields other than Dept_Name
, CASE Dept_Name
WHEN 'Application' THEN 'Group 1'
ELSE 'Group 2'
END AS Dept_Name
FROM v_rpt_service
) vsrv
LEFT OUTER JOIN v_rpt_SurveysByTicket vsrvy ON vsrv.TicketNbr = Vsrvy.SR_Service_RecID
WHERE Dept_Name in ('Application', 'Support', 'Service', 'Development', 'IT')
GROUP BY Dept_Name
I need to add the number of records in which the status is set to 'reopened'. But the 'reopened' status has several IDs.
This is the subquery that will Id the 'reopen' statuses:
SELECT (CASE WHEN s.sr_status_recid = 1 THEN 1 ELSE 0 END) AS Reopened
from v_rpt_service s
where vsrv.sr_status_recid in
(select distinct SR_Status_RecID from SR_Status where [Description] like '%Re-opened%'))
This is the main query that the above query needs to be a part:
SELECT DATEPART(WK, vsrv.date_entered) as WkNumber,
COUNT(vsrv.TicketNbr) AS OpenedIssues, --total ticket count
SUM(CASE WHEN vsrv.Closed_Flag = 1 THEN 1 ELSE 0 END) AS ClosedIssues, --sum of tickets with closed_flag = 1
(SELECT SUM(CASE WHEN s.sr_status_recid = 1 THEN 1 ELSE 0 END)
from v_rpt_service s
where vsrv.sr_status_recid in
(select distinct SR_Status_RecID from SR_Status where [Description] like '%Re-opened%')) AS ReopenedIssues,
SUM(CASE WHEN vsrvy.Surveys_Completed = 1 THEN 1 ELSE 0 END) AS SurveysCompletedWithConnectWise, -- Surveys_Completed flag in view is 1
SUM(CASE WHEN Source = 'Portal' THEN 1 ELSE 0 END) AS IssueLoggedPortal,
SUM(CASE WHEN Source = 'Email Connector' THEN 1 ELSE 0 END) AS IssueLoggedEmai
FROM v_rpt_service vsrv LEFT OUTER JOIN v_rpt_SurveysByTicket vsrvy ON vsrv.TicketNbr = Vsrvy.SR_Service_RecID
WHERE vsrv.company_name <> 'XYZ Test Company' AND vsrv.date_entered BETWEEN '01/01/2016' AND '10/07/2016'
GROUP BY DATEPART(WK, vsrv.date_entered)
ORDER BY WkNumber
How can I have a subquery that uses a CASE statement and the CASE statement is aggregated?
You can use CROSS APPLY
SELECT DATEPART(WK, vsrv.date_entered) as WkNumber,
COUNT(vsrv.TicketNbr) AS OpenedIssues, --total ticket count
SUM(CASE WHEN vsrv.Closed_Flag = 1 THEN 1 ELSE 0 END) AS ClosedIssues, --sum of tickets with closed_flag = 1
SUM(CountReopen.YesNo) AS NumberOfReopen,
SUM(CASE WHEN vsrvy.Surveys_Completed = 1 THEN 1 ELSE 0 END) AS SurveysCompletedWithConnectWise, -- Surveys_Completed flag in view is 1
SUM(CASE WHEN Source = 'Portal' THEN 1 ELSE 0 END) AS IssueLoggedPortal,
SUM(CASE WHEN Source = 'Email Connector' THEN 1 ELSE 0 END) AS IssueLoggedEmail
FROM v_rpt_service vsrv
LEFT OUTER JOIN v_rpt_SurveysByTicket vsrvy
ON vsrv.TicketNbr = Vsrvy.SR_Service_RecID
CROSS APPLY (
SELECT IIF(COUNT(*) > 0,1,0) YesNo
FROM SR_Status
where [Description] like '%Re-opened%'
AND SR_Status_ID = vsrv.sr_status_recid
) CountReopen(YesNo)
WHERE vsrv.company_name <> 'XYZ Test Company'
AND vsrv.date_entered BETWEEN '01/01/2016' AND '10/07/2016'
GROUP BY DATEPART(WK, vsrv.date_entered)
ORDER BY WkNumber
I'm coding a sp to use in an asp.net page which displays a gridview (with multiple pages) and checkboxes for the select query. However it gives me syntax error on the EXEC(#sql), can't I call it within the if after using the WITH...AS ?
SET #sql=#sql +' ' +' FROM DATASET WHERE (RowId BETWEEN (#page - 1) * #pageSize AND (#page) * #pageSize)
OR (#page IS NULL)'
if #filter='pt'
BEGIN
WITH DataSet AS
(
SELECT TOP 100 PERCENT
row_number() over(order by date desc) as 'RowId'
, convert(varchar(10), date 105) AS 'date'
, product1
, product2
, product3
, product4
, Y
, N
FROM (select date
, count(case when product='product1' then 1 else null end) as 'product1'
, count(case when product='product2' then 1 else null end) as 'product1'
, count(case when product='product3' then 1 else null end) as 'product1'
, count(case when onsale='Y' then 1 else null end) as 'Y'
, count(case when onsale='N' then 1 else null end) as 'N'
from dbo.vwSales
group by date) as o
WHERE
(date BETWEEN #from AND #to)
)
EXEC (#sql)
END
You have to have the whole SQL in the variable for the Exec -command, starting with the "with ...".
Unless there is something we're not seeing here, there is no reason to execute dynamic SQL here. Writing dynamic SQL and executing it with EXEC is generally an anti-pattern, as it opens you up to the possibility of writing some inefficient code with security problems.
Can you just run...
WITH DataSet AS
(
SELECT TOP 100 PERCENT
row_number() over(order by date desc) as 'RowId'
, convert(varchar(10), date 105) AS 'date'
, product1
, product2
, product3
, product4
, Y
, N
FROM (select date
, count(case when product='product1' then 1 else null end) as 'product1'
, count(case when product='product2' then 1 else null end) as 'product1'
, count(case when product='product3' then 1 else null end) as 'product1'
, count(case when onsale='Y' then 1 else null end) as 'Y'
, count(case when onsale='N' then 1 else null end) as 'N'
from dbo.vwSales
group by date) as o
WHERE
(date BETWEEN #from AND #to)
)
FROM DATASET WHERE (RowId BETWEEN (#page - 1) * #pageSize AND (#page) * #pageSize)
OR (#page IS NULL)
Also note that the "TOP 100 PERCENT" is unnecessary.
I am trying to get the totals of each month as of YTD (Years to date) Can someone please help me figure out how to integrate this in my query? thank you This is what I have so far.
DECLARE #Year int
set #Year = 2013
select a.first_name, a.last_name
, COUNT(CASE WHEN MONTH(b.Funded_date) = 1 THEN 1 ELSE NULL END) January
, COUNT(CASE WHEN MONTH(b.Funded_date) = 2 THEN 1 ELSE NULL END) February
, COUNT(CASE WHEN MONTH(b.Funded_date) = 3 THEN 1 ELSE NULL END) March
, COUNT(CASE WHEN MONTH(b.Funded_date) = 4 THEN 1 ELSE NULL END) April
From tContact a Join tContract b ON a.contact_id = b.contract_id
Group by a.first_name, a.last_name
This is just an example of how you could count up rows that fall under a certain month.
SELECT MONTH(b.Funded_date) AS 'MonthNum',
COUNT(*) AS 'Total'
FROM Table AS b
WHERE YEAR(b.Funded_date) = 2014
GROUP BY MONTH(b.Funded_date)
Hopefully this will help you with your query.
Thanks
What I tried to do here is create an upper bound record for each month in tContract then join that back into the query you already had. It is joined on dates that are between the beginning of the year and the current month.
DECLARE #Year int
set #Year = 2013
select Ms.thismonth, count(B.thing_You_are_Totalling) from (
select thisMonth = dateadd(month,datediff(month,0,Funded_date),0)
from tContract
where moid = 2005405
and year(Funded_date) = #Year
group by dateadd(month,datediff(month,0,Funded_date),0)
) Ms
inner join (select * from tContact a inner join tContract ON a.contact_id = tContract.contract_id) B
on B.Funded_date >=dateadd(year,datediff(year,0,B.Funded_date),0) -- beginning of year
and B.Funded_date <= Ms.thisMonth -- this month
where year(B.Funded_date) = #Year -- restrict to only this year
group by thisMonth, first_name, last_name
I don't have your full table definition so there might be some issues (maybe a SqlFiddle is in order)
Not sure if this is what you are asking for.
select a.first_name, a.last_name
, COUNT(CASE WHEN MONTH(b.Funded_date) = 1 THEN 1 ELSE NULL END) January
, COUNT(CASE WHEN MONTH(b.Funded_date) = 2 THEN 1 ELSE NULL END) February
, COUNT(CASE WHEN MONTH(b.Funded_date) = 3 THEN 1 ELSE NULL END) March
, COUNT(CASE WHEN MONTH(b.Funded_date) = 4 THEN 1 ELSE NULL END) April
, COUNT(*) TotalCount
, SUM(CASE WHEN MONTH(b.Funded_date) = 1 THEN Amount ELSE NULL END) JanuaryAmount
, SUM(CASE WHEN MONTH(b.Funded_date) = 2 THEN Amount ELSE NULL END) FebruaryAmount
, SUM(CASE WHEN MONTH(b.Funded_date) = 3 THEN Amount ELSE NULL END) MarchAmount
, SUM(CASE WHEN MONTH(b.Funded_date) = 4 THEN Amount ELSE NULL END) AprilAmount
From tContact a Join tContract b ON a.contact_id = b.contact_id
WHERE YEAR(b.Funded_date) = #Year
Group by a.first_name, a.last_name
How about this:
declare #year int = 2013
select a.first_name
, a.last_name
, month(b.Funded_date) [Month]
, datename(month, dateadd(month, month(date_of_birth_dt), - 1)) [MonthName]
, count(month(b.Funded_date)) [Total]
from tContact a
where a.[Year] = #year
group by a.first_name, a.last_name, month(b.Funded_date)
It returns the total of each Month for the year 2013. a.[Year] might not the the name of the field that you have so adjust accordingly. Also, [Month] returns numeric value for month.
Use the Count(*) As Total function. I'm sure this will help you
SELECT MONTH(b.Funded_date) AS 'MonthNum',
COUNT(*) AS 'Total'
FROM Table AS b
WHERE YEAR(b.Funded_date) = 2014
GROUP BY MONTH(b.Funded_date)
This is my data
unitCode stunum assNo subStatus
SIT111 1000 1 Yes
SIT111 1000 2 No
SIT111 3000 1 No
How do i generate only results with ONLY a 'No'
eg: Only student 3000 should come up. Since that person has not handed in ANY assignments. ?
You can use the following statement:
select
*
from
(
select
unitCode,
stunum,
sum(case when subStatus = 'Yes' then 1 else 0 end) as CountYes,
sum(case when subStatus = 'No' then 1 else 0 end) as CountNo
from
students
group by
unitCode, stunum
) as student
inner join student_details
on student.stunum = student_details.stunum
where
CountNo > 0 and CountYes = 0;
declare #T table
(
unitCode varchar(10),
stunum int,
assNo int,
subStatus varchar(3)
)
insert into #T values
('SIT111', 1000, 1, 'Yes'),
('SIT111', 1000, 2, 'No'),
('SIT111', 3000, 1, 'No')
select *
from #T as T
where T.subStatus = 'No' and
T.stunum not in (select stunum
from #T
where subStatus = 'Yes')