T-SQL UNION 查询返回同一表中具有最高和最低评级的项目

发布于 2024-09-19 04:12:47 字数 112 浏览 3 评论 0原文

我想在 T-SQL 中编写一个存储过程,以返回 Articles 表中评级最高的前 5 篇文章和评级最低的 5 篇文章,由“评级”列确定。

我正在考虑在两个选择上使用联合,但我不确定如何编写它。

I want write a stored proc in T-SQL to return the top 5 most highly rated and the bottom 5 most lowly rated articles from an Articles table, determined by the 'rating' column.

I was thinking of using a union on two selects but I'm not sure how to write it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

诗酒趁年少 2024-09-26 04:12:48
select * from (select top 5 *, 'Bottom Five' as Ranking from Call order by id ) a
union all
select * from (select top 5 *, 'Top Five' as Ranking from Call order by id desc ) b
select * from (select top 5 *, 'Bottom Five' as Ranking from Call order by id ) a
union all
select * from (select top 5 *, 'Top Five' as Ranking from Call order by id desc ) b
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文