Charlie Ansell
Charlie Ansell

Reputation: 461

SQL Server CASE statement with two conditions

I have a sample table which has the helpdesk call centre data. In this table call logs and related stuffs are being stored. This table has a call_no and a date_logged column.

I am trying to use one case where I can get the total number of calls over a given time period.

Below is the sample code.

SELECT
    COUNT(CASE WHEN CALLS.DATE_LOGGED BETWEEN '2018/04/06' AND '2018/04/07' 
               THEN calls.CALL_NO END) AS "CALLS LOGGED YESTERDAY"
FROM 
    LOGGED_CALLS CALLS

This is giving me the total number of calls that are in my system, I do not want to use a where clause outside of this condition.

Let us assume that it is the 7th of April again and I want to get the calls logged from the day before

Upvotes: 1

Views: 1725

Answers (1)

Byrel Mitchell
Byrel Mitchell

Reputation: 129

Use SUM instead of COUNT:

SELECT
  SUM(CASE WHEN CALLS.DATE_LOGGED BETWEEN '2018/04/06' AND '2018/04/07' 
              THEN 1 ELSE 0 END) AS "CALLS LOGGED YESTERDAY"

FROM LOGGED_CALLS CALLS

Upvotes: 6

Related Questions