如何一起找到两个选择的总和

时间:2015-09-19 09:43:21

标签: sql sql-server sql-server-2008 select

我想在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

2 个答案:

答案 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