0

我有一个带有两个选择子句的查询与 UNION 结合。我只想选择顶部的第一行。我怎么能用 Union 做到这一点?

Select Fault,OccurredOn From ATMStatus Where Ticket=189703 // This Will retrieve single record as the primary key is applied
Union 
Select Fault,OccurredOn From ATMStatusHistory Where Resolved=0 AND Ticket=189703 Order By OccurredOn Desc
4

2 回答 2

2
select top 1 * from 
(
                 Select Fault,OccurredOn 
                 From ATMStatus
                 Where Ticket=189703
                 Union 
                 Select Fault,OccurredOn
                 From ATMStatusHistory
                 Where Resolved=0 AND Ticket=189703
) x
Order By OccurredOn Desc
于 2012-08-07T12:14:54.973 回答
0

这将返回 2 行:

select 1 as id
union
select 2 as id

这将返回 1 行:

select top 1 * from (
  select 1 as id
  union
  select 2 as id
) as x
order by id
于 2012-08-07T12:15:38.040 回答