怎么把两条sql语句查询出的数据放在一个查询结果里 sql2005

SvenCows 2011-07-25 05:59:42
select top 1 * from Purgdtary where ESOrdersId =58 and PurgErrorTime is not null and purgState>0 order by abs(datediff(d,PurgErrorTime,getdate())) desc

select top 1 * from Purgdtary where ESOrdersId =58 and PurgEndTime is not null and purgState>0 order by abs(datediff(d,PurgEndTime,getdate())) asc
...全文
557 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
h892359495 2012-08-31
  • 打赏
  • 举报
回复
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有两个数据,我想请问怎么把两个数据起来得到一个数据
SvenCows 2011-07-25
  • 打赏
  • 举报
回复
谢谢了,都可以 稍后结帖
cd731107 2011-07-25
  • 打赏
  • 举报
回复
select *
from
(select top 1 * from Purgdtary where ESOrdersId =58 and PurgErrorTime is not null
and purgState>0 order by abs(datediff(d,PurgErrorTime,getdate())) desc
) ta,
(select top 1 * from Purgdtary where ESOrdersId =58 and PurgEndTime is not null
and purgState>0 order by abs(datediff(d,PurgEndTime,getdate())) asc
) tb
中国风 2011-07-25
  • 打赏
  • 举报
回复
select * from (select top 1 * from Purgdtary where ESOrdersId =58 and PurgErrorTime is not null and purgState>0 order by abs(datediff(d,PurgErrorTime,getdate())) desc )t
union all
select * from (select top 1 * from Purgdtary where ESOrdersId =58 and PurgEndTime is not null and purgState>0 order by abs(datediff(d,PurgEndTime,getdate())) asc)t
快溜 2011-07-25
  • 打赏
  • 举报
回复
with cte1 as
(select top 1 * from Purgdtary where ESOrdersId =58 and PurgErrorTime is not null
and purgState>0 order by abs(datediff(d,PurgErrorTime,getdate())) desc)
,cte2 as(select top 1 * from Purgdtary where ESOrdersId =58 and PurgEndTime is not null
and purgState>0 order by abs(datediff(d,PurgEndTime,getdate())) asc)

select * from cte1 union all select * from cte2

22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧