使用where子句聚合sql

时间:2015-04-08 19:13:18

标签: mysql sql

我在查询时遇到问题。

SELECT adm_Consultant, count(adm_Consultant) as num
FROM Admission
WHERE  adm.adm_ReferralDate >= '01/01/2014 00:00:00' AND adm.adm_ReferralDate <= '31/12/2014 00:00:00'
AND adm.adm_PriorSurgery = 'Yes'
AND adm.adm_Consultant <> ''
GROUP BY adm_Consultant
  

错误:一般错误

这虽然有效,但是返回空值也是如此

SELECT adm_Consultant, count(adm_Consultant) as num
FROM Admission
GROUP BY adm_Consultant

我尝试了HAVING子句而不是WHERE子句,但它仍然失败了。

请帮忙。

这是我的阅读材料。

COUNT(expr)

  

返回SELECT语句检索的行中expr的非NULL值的计数。结果是BIGINT值。

https://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_count

2 个答案:

答案 0 :(得分:2)

您忘记创建别名adm

SELECT adm_Consultant, count(adm_Consultant) as num 
FROM Admission adm
WHERE  adm.adm_ReferralDate >= '01/01/2014 00:00:00' AND 
adm.adm_ReferralDate <= '31/12/2014 00:00:00'
AND adm.adm_PriorSurgery = 'Yes'
AND adm.adm_Consultant <> ''
GROUP BY adm_Consultant

答案 1 :(得分:2)

尝试使用ISO标准日期格式:

SELECT adm_Consultant, count(adm_Consultant) as num
FROM Admission adm
WHERE  adm.adm_ReferralDate >= '2014-01-01' AND adm.adm_ReferralDate <= '2014-12-31' AND
       adm.adm_PriorSurgery = 'Yes' AND
       adm.adm_Consultant <> ''
GROUP BY adm_Consultant;