当前位置: 代码迷 >> Sql Server >> 如何把两条sql语句查询出的数据放在一个查询结果里 sql2005
  详细解决方案

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

热度:32   发布时间:2016-04-27 11:19:08.0
怎么把两条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
  相关解决方案