我正在尝试使用下拉菜单使报告像 2 个单独的报告一样。这是一个名为 rtype 的参数,其值为 0-2。
数据是客户预约,我试图报告他们保持预约的能力。
选项 1 和 2 我们正在专门寻找他们的第一次约会,因此在 where 的“不存在”部分中,子查询正在寻找以前的约会。
清单 3 本质上是 list1+list2(还有更多),但我们不在乎它是否是初次约会
select sa.organization, sa.status, count(1)
from mytable as sa
where sa.service_date between ${sdate} and ${edate}
and (
(${rtype}::int = 0 and sa.activity_id in (..) /*list 1*/ )
OR(${rtype}::int = 1 and sa.activity_id in (..) /*list 2*/)
OR(${rtype}::int = 2 AND sa.activity_id not in (..) /*list 3*/)
)
and (
(${rtype}::int !=2 AND not exists (select *
from rpt_scheduled_activities as sa2
where sa2.client_id = sa.client_id
and (
(${rtype}::int = 0 and sa.activity_id in (..)/*list 1*/ )
OR
(${rtype}::int = 1 and sa.activity_id in (..) /*list 1*/ )
)
and sa2.status in ('Kept')
and sa2.service_date < sa.service_date
))
OR (${rtype}::int = 2))
group by
sa.organization,
sa.status
order by
sa.status
本质上,我想在 rtype = 2 时“跳过”不存在的子查询。
谁能指出我做错了什么?