当前位置: 代码迷 >> Sql Server >> .求一sql语句
  详细解决方案

.求一sql语句

热度:16   发布时间:2016-04-27 15:42:14.0
在线等..急!..求一sql语句
各位侠:

现有如下两个查询结果

select   count(*)   as   Acnt,sum(credit)   as   Acredit   from   A

select   count(*)   as   Bcnt,sum(credit)   as   Bcredit   from   B

我现在要得到  
select   Acnt+Bcnt   as   ABcnt   ,Acredit+Bcredit   as   ABcredit

求一个sql语句将其实现

说明:A,B两表结构不同,也无关联索引字段

------解决方案--------------------
select ABcnt=sum(ABcnt),ABcredit=sum(ABcredit) from
(
select count(*) as ABcnt,sum(credit) as ABcredit from A
union all
select count(*) as ABcnt,sum(credit) as ABcredit from B
)a
------解决方案--------------------
看样了不急,用join也可以,不如上边好,但可同时得到另个的4个量
select Acnt+Bcnt as ABcnt ,Acredit+Bcredit as ABcredit from
(select count(*) as Acnt,sum(credit) as Acredit from #aa)aa left join
(select count(*) as Bcnt,sum(credit) as Bcredit from #bb)bb on 1=1
  相关解决方案