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

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

2012-09-19 
怎么把两条sql语句查询出的数据放在一个查询结果里 sql2005select top 1 * from Purgdtary where ESOrders

怎么把两条sql语句查询出的数据放在一个查询结果里 sql2005
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

[解决办法]

SQL code
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
[解决办法]
SQL code
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 )tunion 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
[解决办法]
SQL code
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 

热点排行