我想在SQL Server中找到这两个选择的总和:
select count(rez1)*3 as Fit1 from Table_Name where rez1>rez2
select count(rez1)*1 as Fit2 from Table_Name where rez1=rez2
答案 0 :(得分:4)
您可以将where
子句转换为case
表达式并返回整数值,而这些值又可以求和:
SELECT SUM (CASE WHEN rez1 > rez2 THEN 3
WHEN rez1 = rez2 THEN 1
END)
FROM table_name
答案 1 :(得分:1)
一种可能的方式:
SELECT SUM(T.Fit) AS SumFit
FROM
(
select count(rez1)*3 as Fit
from Table_Name where rez1>rez2
UNION ALL
select count(rez1)*1 as Fit
from Table_Name where rez1=rez2
) AS T