[英]MySQL count with case return wrong count
我有以下問題:
我有mysql表:
continent,
country,
city,
zip,
street,
status (int),
reason (varchar, can be empty or some text),
... (some other columns, not required for query)
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
我認為問題出在GROUP BY,但不確定-它不會返回正確的數據。
SQLFiddle (檢查Paris / Street2-它顯示3 DISPUTED,並且應該顯示1 BLOCKED和2 DISPUTED)
感謝您的意見。
我認為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.