首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

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

2012-09-16 
查询结果是sum有两个数据,我想请问怎么把两个数据加起来得到一个数据SELECT count(*) as sum from hacconf

查询结果是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 

热点排行