我有下表
CREATE TABLE public.like_log
(
id integer,
account_id bigint,
match boolean,
insert_timestamp timestamp with time zone DEFAULT now()
)
我有以下SQL查询
select count(*) from like_log where insert_timestamp >= (NOW() - INTERVAL '30 hours' ) AND account_id = 1105399
如何构造执行以下操作的SQL查询?
首先它将对上面查询执行的行进行计数,然后,如果计数超过30,它将检查是否有任何行具有match = true。如果这样做,它将返回false,如果结果不包含包含match = true的行,则它将返回true
如果计数少于30,则返回false
答案 0 :(得分:3)
所以您想知道以下两个条件是否成立:
如何?
select ( count(*) > 30 and not bool_or(match) )
from like_log
where insert_timestamp >= (NOW() - INTERVAL '30 hours' ) and
account_id = 1105399;
答案 1 :(得分:0)
使用case
检查条件:
select
case
when t.counter <= 30 then false
else
case
when t.istrue > 0 then false
else true
end
end result
from (
select
count(*) counter,
sum(case when match = true then 1 else 0) istrue
from like_log
where insert_timestamp >= (NOW() - INTERVAL '30 hours' ) AND account_id = 1105399
) t
尚不清楚当计数等于30时会发生什么,因此我将其包括在计数小于30的情况下。