0

How to total count?

SELECT 
    COUNT(CASE WHEN SHP.id = 1 then 1 ELSE NULL END) as "New", 
    COUNT(CASE WHEN SHP.id = 2 then 5 ELSE NULL END) as "Accepted"
from SHP 

RESULT:

NEW  Accepted
 1       5

But I need a total count result: 6

3
  • 1
    COUNT() counts non-null values. SUM() sums values. Commented May 3, 2017 at 7:53
  • Add some sample table data, and its expected result. Commented May 3, 2017 at 7:53
  • I edit my answer Commented May 3, 2017 at 8:03

4 Answers 4

1

I'd do something like this;

SELECT 
    COUNT(CASE WHEN id = 1 THEN 1 END) as New, 
    COUNT(CASE WHEN id = 2 THEN 5 END) as Accepted,
    COUNT(CASE WHEN id = 1 THEN 1 
               WHEN id = 2 THEN 5 END) as Total
FROM SHP 

This is exactly what the CASE statement should be used for, the logic is very simple. This will avoid having to perform multiple calculations on the same fields.

As a note, the value in your THEN statement isn't used in this instance at all, it's just doing a COUNT of the number rather than performing a SUM. I've also removed the ELSE NULL because this is what the CASE will do by default anyway.

If your intention was to SUM the values then do this;

SELECT 
    SUM(CASE WHEN id = 1 THEN 1 END) as New, 
    SUM(CASE WHEN id = 2 THEN 5 END) as Accepted,
    SUM(CASE WHEN id = 1 THEN 1 
             WHEN id = 2 THEN 5 END) as Total
FROM SHP 

Example

Assuming you have only two values in your database, 1 and 2, we can create test data like this;

CREATE TABLE #SHP (id int)
INSERT INTO #SHP (id)
VALUES (1),(2)

And use this query;

SELECT 
    SUM(CASE WHEN id = 1 then 1 END) as New, 
    SUM(CASE WHEN id = 2 then 5 END) as Accepted,
    SUM(CASE WHEN id = 1 THEN 1 
             WHEN id = 2 THEN 5 END) as Total
FROM #SHP 

Gives this result;

New     Accepted    Total
1       5           6
Sign up to request clarification or add additional context in comments.

2 Comments

result total 2 but I need 6, 1+5 = 6
SELECT SUM(CASE WHEN id = 1 then 1 END) as New, SUM(CASE WHEN id = 2 then 5 END) as Accepted, SUM(CASE WHEN id = 1 THEN 1 WHEN id = 2 THEN 5 END) as Total FROM #SHP its ok thank you
0

Try this:

SELECT 
    COUNT(CASE WHEN SHP.id = 1 then 1 ELSE NULL END) + 
    COUNT(CASE WHEN SHP.id = 2 then 5 ELSE NULL END) as "Total"
from SHP 

1 Comment

result total 2 but I need 6, 1+5 = 6
0

You could wrap your query into a subquery and do something like this:

SELECT SUM(New) as New, Sum(Accepted) as Accepted, Sum(New + Accepted) as Total FROM
(SELECT 
    COUNT(CASE WHEN SHP.id = 1 then 1 ELSE NULL END) as "New", 
    COUNT(CASE WHEN SHP.id = 2 then 5 ELSE NULL END) as "Accepted"
from SHP) as SubQuery

That's if you don't want to duplicate doing the counts and just adding the two together.

1 Comment

What doesn't work about it? Perhaps you need to use SUM instead of COUNT?
0

try this

with s1 as(
SELECT 
    COUNT(CASE WHEN SHP.id = 1 then 1 ELSE 0 END) as "New" 
from SHP 
),s2 as
(
SELECT  
    COUNT(CASE WHEN SHP.id = 2 then 5 ELSE 0 END) as "Accepted"
from SHP 
)
select sum("New"+  "Accepted") from s1,s2

1 Comment

result total 2 but I need 6, 1+5 = 6

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.