简体   繁体   English

MySQL计数带大小写返回错误计数

[英]MySQL count with case return wrong count

I have following problem: 我有以下问题:

I have mysql table with: 我有mysql表:

continent,  
country,  
city,  
zip,  
street,  
status (int),  
reason (varchar, can be empty or some text),  
... (some other columns, not required for query)  

I want to count occurence of each status by country,city,zip,street but with mapping of status int to text: 我想按国家,城市,邮编,街道来计数每种状态的出现但要将状态int映射到文本:

SELECT country,city,zip,street, 
   CASE WHEN (status = '2' AND reason <> '') THEN "BLOCKED" 
        WHEN (status = '2' AND reason='') THEN "DISPUTED"  
        WHEN status = '3' THEN 'EXPIRED' 
        WHEN status = '1' THEN 'ACTIVE' ELSE 'UNKNOWN' 
        END as status, 
    count(*) AS count
FROM people
where continent='Europe'
GROUP BY country,city,zip,street,status

I think that problem is in GROUP BY but not sure - it don't return correct data. 我认为问题出在GROUP BY,但不确定-它不会返回正确的数据。

SQLFiddle (Check Paris/Street2 - it shows 3 DISPUTED and should show 1 BLOCKED and 2 DISPUTED) SQLFiddle (检查Paris / Street2-它显示3 DISPUTED,并且应该显示1 BLOCKED和2 DISPUTED)

Thanks for opinions. 感谢您的意见。

I think EngineerCoder meant to write : 我认为EngineerCoder 打算写

select country,city,zip,street, status, count(*) 
from
(
SELECT country,city,zip,street, 
   CASE WHEN (status = '2' AND reason <> '') THEN "BLOCKED" 
        WHEN (status = '2' AND reason='') THEN "DISPUTED"  
        WHEN status = '3' THEN 'EXPIRED' 
        WHEN status = '1' THEN 'ACTIVE' ELSE 'UNKNOWN' 
   END as status, 
FROM people
where continent='Europe'
) AS ilv
GROUP BY country,city,zip,street,status

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM