读书人

如何把两条sql语句查询出的数据放在一

发布时间: 2012-09-19 13:43:54 作者: rapoo

怎么把两条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 

读书人网 >SQL Server

热点推荐