这是我的初始查询:
select
CASE WHEN a.TO_WG_ID is null
and a.CREATED_DATE_TIME =
(select min(b.created_date_time) from ymtn.message b
where b.thread_id = a.thread_id) THEN 1 ELSE 0
END AS "Created"
from ymtn.MESSAGE a
left join YMTN.WORKGROUP b
ON a.FROM_WG_ID=b.WORKGROUP_ID
where b.WORKGROUP_ID='1STOP_PROCESS'
它给了我一列有一堆 1' 和 0's。我想要的只是总和..只是一个数字,例如 1691。如果我在 case 语句前面放一个 SUM,我会收到此错误: Msg 130, Level 15, State 1, Line 5 Cannot perform a aggregate function on an expression包含聚合或子查询。那么我该怎么做呢?我已经使它与这个查询一起工作:
select count(CREATED_DATE_TIME) as 'Created'
from ymtn.MESSAGE a
LEFT JOIN YMTN.WORKGROUP b
ON a.FROM_WG_ID=b.WORKGROUP_ID
where CREATED_DATE_TIME=(SELECT MIN(b.CREATED_DATE_TIME) FROM YMTN.MESSAGE b WHERE
b.THREAD_ID = a.THREAD_ID)
and a.TO_WG_ID is null
and b.WORKGROUP_ID='1STOP_PROCESS'
但是,我希望它在 case 语句中工作,而不必将嵌套选择向下移动到 WHERE 子句。