如何在sql server中汇总两个不同的表值 [英] how to sum the two different table value in sql server

查看:84
本文介绍了如何在sql server中汇总两个不同的表值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 选择 sum(Hocount) 来自讲义其中 type = '  H'  course = '  AFF' 





输出如下



总计

150



 选择总和(席位) 来自 stockentry 其中 其中 type = '  H'  course = '  AFF' 





输出如下



总计

100





使用上面两个sql查询组合我想在单个查询中输出如下。



总计

250



我怎样才能编写sql查询以获得单sql查询中的输出。



问候,

Narasiman P.

解决方案

只是一个想法:



尝试

 选择 sum(Hocount)+ sum(席位) as 
来自讲义h
内部 join stockentry s on h.type = s。类型 和 h.course = s.course
其中 h.type = ' H' h.course = ' AFF'


试试这个

 select sum( x.total)总计来自

选择总和(Hocount)作为来自分发的总额,其中type ='H'且course ='AFF'
union
select sum(seat )来自stockentry的总数,其中type ='H'和course ='AFF'
)x


尝试:

  SELECT 选择 sum(Hocount) as 来自讲义其中​​ type = '  H'  course = '  AFF')+ 
select 总和(席位) 来自 stockentry 其中 其中 type = ' H' course = ' AFF'


select sum(Hocount) as total from handout where type= 'H' and course = 'AFF'



Output as follows

Total
150

select sum(seats) as total from stockentry where where type= 'H' and course = 'AFF'



Output as follows

Total
100


Using combining above two sql query i want the output as follows in single query.

Total
250

for that how can i write the sql query to get the output in single sql query.

Regards,
Narasiman P.

解决方案

Just an idea:

Try

select sum(Hocount) + sum(seats) as total 
from handout h
inner join stockentry s on h.type= s.type and h.course = s.course
where h.type= 'H' and h.course = 'AFF'


Try this

select sum(x.total) as Total from
(
select sum(Hocount) as total from handout where type= 'H' and course = 'AFF'
union
select sum(seats) as total from stockentry where where type= 'H' and course = 'AFF'
)x


Try:

SELECT (select sum(Hocount) as total from handout where type= 'H' and course = 'AFF') +
       (select sum(seats) as total from stockentry where where type= 'H' and course = 'AFF')


这篇关于如何在sql server中汇总两个不同的表值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆