繁体   English   中英

SQL Server在子查询中按最大行排序

[英]SQL Server order by with max rows in subquery

以下查询适用于PostgreSQL,我想知道为什么它不适用于SQL Server 2016。

select * from (values (1),(2)) as a(a)
UNION
(
  select * from (values (1),(2)) as a(a)
  order by 1 desc
  offset 0 rows fetch first 1 rows only
)
order by 1 desc offset 0 rows fetch first 1 rows only

谁能向我解释为什么不支持订购?

有趣的是,以下是另一种表达方式,它像一种魅力

select * from (values (1),(2)) as a(a)
where a.a in(
  select * from (values (1),(2)) as a(a)
  order by 1 desc
    offset 0 rows fetch first 1 rows only
)
order by 1 desc offset 0 rows fetch first 1 rows only

这是错误吗?

联合后的查询需要作为选择显示,如下所示:

select * from (values (1),(2)) as a(a)
UNION
select * from
(
select * from (values (1),(2)) as a(a)
order by 1 desc
offset 0 rows fetch first 1 rows only
) b
order by 1 desc offset 0 rows fetch first 1 rows only

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM