查询#1
SELECT
@IN_EMPCOUNT = COUNT (a.CH_EMPCODE)
FROM
BSC_REVIEW_EMP_MASTER a
INNER JOIN
dbo.cmn_authorization b ON (CASE b.vc_bu WHEN '%' THEN a.BUSSCD ELSE b.vc_bu END) = a.BUSSCD
WHERE
IN_REVIEW_NO = @in_review_no
AND b.ch_empcode = @ch_empcode
AND b.in_rolecode IN (136, 137, 138)
查询#2
SELECT
@IN_EMPCOUNT1 = COUNT (a.CH_EMPCODE)
FROM
BSC_REVIEW_EMP_MASTER a
INNER JOIN
dbo.cmn_authorization b ON (CASE b.vc_bu WHEN '%' THEN a.BUSSCD ELSE b.vc_bu END) = a.BUSSCD
WHERE
IN_REVIEW_NO = @in_review_no
AND b.ch_empcode = @ch_empcode
AND b.in_rolecode IN (138, 139, 140)
查询#3
SELECT
@IN_EMPCOUNT2 = COUNT (a.CH_EMPCODE)
FROM
BSC_REVIEW_EMP_MASTER a
INNER JOIN
dbo.cmn_authorization b ON (CASE b.vc_bu WHEN '%' THEN a.BUSSCD ELSE b.vc_bu END) = a.BUSSCD
WHERE
IN_REVIEW_NO = @in_review_no
AND b.ch_empcode = @ch_empcode
AND b.in_rolecode IN (141, 142, 145)
结果:
Select @in_empcount, @in_empcount1, @in_empcount2
这是我正在处理的示例查询。我可以做一些事情,比如不必为每个where子句for (b.in_rolecode)
重复相同的select语句,结果会相同吗?
答案 0 :(得分:0)
这就是您所要求的 - 包括IN
条款中的所有值:
select name
from tabl1
where id in ( 1,2,3,4,8,9 )
IN子句就像OR子句一样工作,因此不需要将这些查询分开。
编辑 - 根据您的最新编辑,您希望使用CASE和COUNT(您也可以使用SUM,但它的工作方式相同):
SELECT
COUNT (CASE WHEN b.in_rolecode IN (136,137,138) THEN 1 END) in_empcount,
COUNT (CASE WHEN b.in_rolecode IN (138,139,140) THEN 1 END) in_empcount1,
COUNT (CASE WHEN b.in_rolecode IN (141,142,145) THEN 1 END) in_empcount2
FROM BSC_REVIEW_EMP_MASTER a
INNER JOIN dbo.cmn_authorization b
ON (CASE b.vc_bu WHEN '%' THEN a.BUSSCD ELSE b.vc_bu END) = a.BUSSCD
WHERE IN_REVIEW_NO = @in_review_no
AND b.ch_empcode =@ch_empcode