简体   繁体   English

如何选择两列的最大值并按工位分组

[英]how to select max of two columns and group it by station

I have a simple table like this 我有一个像这样的简单桌子

 station    num     tim
   1        150    10
   1       200     222
   1       100     5000
   1       200     555
   2       100     500
   2       120     200
   3        1      2

The output desired is like this 所需的输出是这样的

station num     tim
   1    200     555
   2    120     200
   3    1       2

i wrote this code but for station=1 and num=200 return two rows 我写了这段代码,但对于station = 1和num = 200返回两行

(SELECT a.station , a.num ,a.tim
FROM test.dbo.tst a
JOIN (
SELECT station, MAX(num) num
FROM test.dbo.tst
GROUP BY station
) b ON a.station = b.station and a.num=b.num ) order by station 

One possible approach is to use ROW_NUMBER() to number rows grouped by station and ordered by num and tim descending and then select the rows with number equal to 1. 一种可能的方法是使用ROW_NUMBER()对按station分组并按numtim降序排序的行编号,然后选择数字等于1的行。

Input: 输入:

CREATE TABLE #Stations (
   station int,
   num int,
   tim int
)
INSERT INTO #Stations
   (station, num, tim)
VALUES
   (1, 150, 10),
   (1, 200, 222),
   (1, 100, 5000),
   (1, 200, 555),
   (2, 100, 500),
   (2, 120, 200),
   (3, 1,   2)

Statement: 声明:

;WITH cte AS (   
   SELECT 
      station, 
      num, 
      tim,
      ROW_NUMBER() OVER (PARTITION BY station ORDER BY num DESC, tim DESC) AS Rn
   FROM #Stations
)
SELECT 
   station, 
   num, 
   tim
FROM cte
WHERE Rn = 1

Output: 输出:

station num tim
1       200 555
2       120 200
3       1   2

Update 1 更新1

The recommended method is the one mentioned by @Zhorov, but i will leave my answer to give all possible solutions 推荐的方法是@Zhorov提到的方法,但是我会留下我的答案以给出所有可能的解决方案


Initial answer 初步答案

Try using a similar query: 尝试使用类似的查询:

SELECT T.station, T.maxnum, MAX(T.tim) as maxtim 
FROM (SELECT station, tim, MAX(num) as maxnum
      GROUP BY station, tim ) T
GROUP BY station, maxnum

OR 要么

WITH CTE_1 as (SELECT station, tim, MAX(num) as maxnum
               GROUP BY station, tim)
SELECT station, maxnum, Max(tim)
FROM CTE 
GROUP BY station, tim

You could use the ROW_NUMBER partition function, something along these lines should do the trick: 您可以使用ROW_NUMBER分区函数,以下ROW_NUMBER行应该可以解决问题:

SELECT  [Station], [Num], [Tim]
FROM
(
    SELECT  Station,
            Num,
            Tim,
            ROW_NUMBER() OVER(PARTITION BY Station ORDER BY Num DESC, Tim DESC) [N]
    FROM    tst
) SQ
WHERE    [N] = 1

This works on the basis of taking each group of rows, as grouped by the Station column (this is what the PARTITION BY part of the ROW_NUMBER() statement refers to), then ordering them first by Num and then by Tim and assigning each row in the result set a row number. 这是基于对每行进行分组(按Station列分组)的(这就是ROW_NUMBER()语句所指的PARTITION BY部分),然后先按Num然后按Tim对其进行排序并分配每行在结果中设置一个行号。 The outer query then takes only the first row of each group, ie for each value of Station it takes the one with the highest value for Num and Tim . 然后,外部查询仅采用每个组的第一行,即对于Station每个值,它采用NumTim最大值。

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

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