小编典典

联合仅选择一行

sql

我有一个将两个选择子句与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

阅读 143

收藏
2021-04-07

共1个答案

小编典典

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
2021-04-07