Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Filter results by Sum variables

Author  Topic 

SQLN00b11
Starting Member

6 Posts

Posted - 2011-03-10 : 11:16:31
Hi Folks,

I have created the query below for a report that I need to produce for call failure stats. The query works fine and I can get the report outputting correctly. However I am getting a lot of stats for calls that have not failied i.e. U, C X or M have 0 values.

I need to see stats where calls that have sum value greater than 1 for Reason codes U,C,X and M. But I do not know how to implement this. Can someone amend my query to allow this please. Much appreciated.


DECLARE @Starttime datetime
DECLARE @Endtime datetime

Set @Starttime = '2011-03-08 00:00:00'
Set @Endtime = '2011-03-09 00:00:00'

SELECT Diallednumber as RoutingNumber, COUNT(*) AS TotalCalls,
SUM(CASE WHEN Reason = 'U' THEN 1 ELSE 0 END) AS NU,
SUM(CASE WHEN Reason = 'C' THEN 1 ELSE 0 END) AS Congestion,
SUM(CASE WHEN Reason = 'X' THEN 1 ELSE 0 END) AS Unknown,
SUM(CASE WHEN Reason = 'M' THEN 1 ELSE 0 END) AS UnsupportedMedia

FROM CallT
Where starttime between @starttime and @endtime
and (DialledNumber like '98%' or RoutingNumber like '+44206%')
and isinbound = '0'


GROUP BY originaldiallednumber
order by NU desc

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2011-03-10 : 11:38:37
Your query will basically count all the U,C,M,X associated with all Diallednumber. If you only want Dialednumbers that have failed, then add a HAVING clause
HAVING
SUM(CASE WHEN Reason = 'U' THEN 1 ELSE 0 END) >0
or SUM(CASE WHEN Reason = 'C' THEN 1 ELSE 0 END) >0
etc.

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

SQLN00b11
Starting Member

6 Posts

Posted - 2011-03-11 : 05:56:32
Hi Jim I have tried adding the having clause just before the sum statements but I must not be doing it correctly as i am getting errors. Could someone please show me how to incorporate the Having clause fully into my code so I only get results where greater than 0.

Thanks,

Sal
Go to Top of Page
   

- Advertisement -