当前位置: 代码迷 >> Sql Server >> 查询结果是sum有两个数据,小弟我想请教如何把两个数据加起来得到一个数据
  详细解决方案

查询结果是sum有两个数据,小弟我想请教如何把两个数据加起来得到一个数据

热度:79   发布时间:2016-04-27 11:08:28.0
查询结果是sum有两个数据,我想请问怎么把两个数据加起来得到一个数据
SELECT count(*) as sum from hacconf.resource,hacconf.auditor_session_author,hacref.session where starttime>=1346169600 and starttime<=1346255999 and hacref.session.appname=hacconf.resource.name and hacconf.resource.dev_id=hacconf.auditor_session_author.dev_id and hacconf.auditor_session_author.manager_id=3 and endtime > 1 
UNION SELECT count(*) as sum from hacref.session where starttime>=1346169600 and starttime<=1346255999 and endtime = 1)

我的查询结果是sum有两个数据,我想请问怎么把两个数据加起来得到一个数据

------解决方案--------------------
要用union all,不要用union,因为union会过滤到重复的值。
SQL code
select (SELECT count(*)        from hacconf.resource,hacconf.auditor_session_author,hacref.session         where starttime>=1346169600 and starttime<=1346255999         and hacref.session.appname=hacconf.resource.name         and hacconf.resource.dev_id=hacconf.auditor_session_author.dev_id         and hacconf.auditor_session_author.manager_id=3 and endtime > 1)     +         (SELECT count(*) from hacref.session         where starttime>=1346169600 and starttime<=1346255999 and endtime = 1)) as sum--orselect sum(t.[sum]) as sum from (    SELECT count(*) as sum    from hacconf.resource,hacconf.auditor_session_author,hacref.session     where starttime>=1346169600 and starttime<=1346255999     and hacref.session.appname=hacconf.resource.name     and hacconf.resource.dev_id=hacconf.auditor_session_author.dev_id     and hacconf.auditor_session_author.manager_id=3 and endtime > 1    union all    SELECT count(*) from hacref.session     where starttime>=1346169600 and starttime<=1346255999 and endtime = 1)t
  相关解决方案