我正在尝试在具有列“Id”,itemNumber“的表上构建where
子句条件,对于任何行和”date“,它可以是1或2。
我的目标是编写where子句,使得我只得到“Id's”,其中“itemNumber”为2,然后如果count大于某个值,它应该过滤整个行到今天和今天之间+ 1,否则今天今天+ 2。
我试过了,
Select Id
from table
where itemNumber=2 And ((count(itemNumber)>2 and date between ‘today’ and ‘today+1’) OR (count(itemNumber)<=2 and date between ‘today’ and ‘today+2’))
我得到错误,说你需要sql“有”。我做错了吗?
答案 0 :(得分:0)
试试这样:
SELECT id
FROM t
WHERE itemNumber = 2
GROUP BY id
HAVING (COUNT(itemNumber) > 2 AND date BETWEEN 'today' and 'today+1'))
OR (COUNT(itemNumber) <= 2 AND date BETWEEN 'today' and 'today+2'))
在对数据进行分组后将HAVING视为WHERE子句,如果要按组(或id)计算某些内容,则必须执行此操作。