SQL选择if count

时间:2013-04-02 13:11:17

标签: sql database count

我有两张桌子 事件(身份证明,日期) 提醒(incidentID,类型) fact.id = reminder.incidentID

上的内部联接

并且我想在事件发生时使用fact.id只有4个以上的提醒.type = 15 我认为是什么

SELECT incident.id
FROM incident
INNER JOIN reminder ON incident.id = reminder.incidentid 
HAVING COUNT (reminder.remindertype = "something") >5
GROUP BY incident.incidentcode

我得到的错误是

  

第6行:'='附近的语法不正确。

我该怎么办?

3 个答案:

答案 0 :(得分:2)

group by子句应在having子句

之前声明
select incident.id
from incident
inner join reminder
on incident.id = reminder.incidentid
group by incident.incidentcode 
having count (reminder.remindertype) >5

答案 1 :(得分:2)

COUNT不能那样工作。

尝试:

select incident.id
from incident
inner join reminder
on incident.id = reminder.incidentid
WHERE reminder.remindertype = "something"
group by incident.incidentcode 
having count (reminder.remindertype) >5

答案 2 :(得分:1)

你很近:

select incident.id
from incident inner join
     reminder
     on incident.id = reminder.incidentid
group by incident.incidentcode 
having sum(case when reminder.remindertype = "something" then 1 else 0 end) >5

您的原始语法在大多数SQL方言中都不起作用。您需要条件聚合。