简体   繁体   English

如何选择对应行中前 5 个列的五个值?

[英]How to select five values of a column with the top 5 in their corresponding row?

So I have two columns date and norder in my SQL table orders .所以我的 SQL 表orders有两列datenorder We have to select dates with the top 10 maximum orders.我们必须选择具有前 10 个最大订单的日期。 We can do this like我们可以这样做

SELECT date
FROM orders
ORDER BY norder DESC
LIMIT 10;

But we want to order these 10 dates such that the date is in descending order on values that have the same norder .但是我们想要对这 10 个日期进行排序,以便日期在具有相同norder值上按降序排列。 How should we do this?我们应该怎么做?

You can use window functions.您可以使用窗口函数。

select a.date from 
(select date,dense_rank() over (order by norder desc) as rank
from orders
order by rank desc) as a
where rank <= 10
order by date desc;

This would give you the dates for the top 10 maximum orders (including dates for orders where the maximums are identical).这将为您提供前 10 个最大订单的日期(包括最大值相同的订单的日期)。

This should do.这个应该可以。

SELECT date
FROM orders
ORDER BY order DESC, date DESC
LIMIT 10;

In Microsoft SQL Server:在 Microsoft SQL Server 中:

SELECT TOP(10) [date]
FROM [orders]
ORDER BY [order] DESC, [date] DESC;

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

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