我需要找出给定时间段内不同患者数量之间的差异。
桌子是镶木地板的黑斑羚。
是否有更好的方式编写此查询。两个选择查询都使用同一张表。
在给定的时间范围内,只需对患者计数一次
WITH
curr_prod AS
(
SELECT product
,COUNT(DISTINCT(patient_id)) AS rawPatientCount
FROM tbl
WHERE time_seq_id IN (12,11,10)
GROUP BY product
)
,prev_prod AS
(
SELECT product
,COUNT(DISTINCT(patient_id)) AS rawPatientCount
FROM tbl
WHERE time_seq_id in (7,8,9)
GROUP BY product
)
SELECT COALESCE(curr_prod.product,prev_prod.product) AS product,
,curr_prod.rawPatientCount-prev_prod.rawPatientcount AS change_pat_cnt
FROM curr_prod
FULL OUTER JOIN prev_prod ON curr_prod.product=prev_prod.product