1

I want a column where I count transactions where itemtype contains either one of two specified values or location contains one of two specified values. The first two columns (itemtype and location) gives correct figures, but the third seems to count all transactions, irrespective of itemtype or location. What am I doing wrong?

SELECT 
  COUNT(CASE WHEN itemtype IN ('BARNTAL','STORSTIL') THEN 1 END) 'itemtype',
  COUNT(CASE WHEN location IN ('Lattlast','Appelhyllan') THEN 1 END) 'location',
  COUNT(CASE WHEN itemtype IN ('BARNTAL','STORSTIL') THEN 1 
             WHEN location IN ('Lattlast','Appelhyllan') THEN 1  
             ELSE 0
        END) 'total'  
FROM statistics
4
  • 2
    what is your expected output? Commented Jan 14, 2019 at 9:51
  • 2
    Hi there, which DBMS are you using (e.g. SQL Server, MySQL etc.) Commented Jan 14, 2019 at 9:51
  • Why cant you match location and itemtype in where clause Commented Jan 14, 2019 at 9:52
  • 2
    @Klas Just change the ELSE 0 to ELSE NULL or remove it as ELSE NULL is already implied. Commented Jan 14, 2019 at 9:54

4 Answers 4

1

COUNT counts not null values which means it also "counts" the 0 from the ELSE part. Remove the ELSE part altogether:

SELECT 
  COUNT(CASE WHEN itemtype IN ('BARNTAL','STORSTIL') THEN 1 END) 'itemtype',
  COUNT(CASE WHEN location IN ('Lattlast','Appelhyllan') THEN 1 END) 'location',
  COUNT(CASE WHEN itemtype IN ('BARNTAL','STORSTIL') THEN 1 
             WHEN location IN ('Lattlast','Appelhyllan') THEN 1
        END) 'total'  
FROM statistics
Sign up to request clarification or add additional context in comments.

Comments

1

You don't need to add ELSE part :

SELECT COUNT(CASE WHEN itemtype IN ('BARNTAL','STORSTIL') THEN 1 END) AS itemtype,
       COUNT(CASE WHEN location IN ('Lattlast','Appelhyllan') THEN 1 END) AS location,
       COUNT(CASE WHEN itemtype IN ('BARNTAL','STORSTIL') THEN 1 
                  WHEN location IN ('Lattlast','Appelhyllan') THEN 1  
             END) AS total
FROM statistics;

Comments

1

For last one you can use SUM instead of COUNT like below -

SELECT 
  COUNT(CASE WHEN itemtype IN ('BARNTAL','STORSTIL') THEN 1 END) 'itemtype',
  COUNT(CASE WHEN location IN ('Lattlast','Appelhyllan') THEN 1 END) 'location',
  sum(CASE WHEN itemtype IN ('BARNTAL','STORSTIL') THEN 1 
  WHEN location IN ('Lattlast','Appelhyllan') THEN 1  
 END) 'total'  

FROM statistics

1 Comment

An alternative would have been to change the ELSE 0 to ELSE NULL in the original COUNT version. Or remove it as ELSE NULL is already implied. :)
1

I think, the third condition would be as below

SELECT
COUNT(CASE WHEN itemtype IN ('BARNTAL','STORSTIL') OR location IN ('Lattlast','Appelhyllan') THEN 1 END) AS total
FROM statistics;

Comments

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.