在sql中总结两个select语句

时间:2017-10-06 15:01:29

标签: sql sql-server

我希望产生这两个选择语句的总和:

select sum(amount) as hr from friday.dbo.FMDR_Friday_CSV where [Account #] like 4010

select sum(amount) as hr from friday.dbo.FMDR_Friday_CSV where [Account #] like 4060

我尝试在它们之间加入一个联合,它只用于在同一结果窗口中生成两个结果。

感谢您的帮助,我是一个绝对的初学者。

4 个答案:

答案 0 :(得分:3)

由于记录来自同一个表格,因此UNION是多余的。简单的INOR就足够了:

select sum(amount) as hr from friday.dbo.FMDR_Friday_CSV
where [Account #] in (4060, 4061)
select sum(amount) as hr from friday.dbo.FMDR_Friday_CSV
where [Account #] = 4060 OR [Account #] = 4061)

答案 1 :(得分:2)

如果您想使用多个选择(可能涉及其他表)

select ((select sum(hours) from foo1) +
        (select sum(hours) from foo2)) as totalHours

答案 2 :(得分:1)

一个简单的OR会做:

 select sum(amount) as hr from friday.dbo.FMDR_Friday_CSV 
   where [Account #] like 4010 OR [Account #] like 4060

答案 3 :(得分:1)

错误:

select sum(amount) as hr from friday.dbo.FMDR_Friday_CSV
where [Account #] like 4010 or [Account #] like 4060

您的第二个选择是使用with语句。阅读它:)