简体   繁体   English

选择分组列的最小值

[英]Selecting lowest value of grouped column

A         B        C
+--------+-------+----+
|    310 | 09001 | 18 |
|  80614 | 09001 | 18 |
| 169009 | 09001 | 19 |
| 201695 | 09001 | 19 |
| 282089 | 09001 | 19 |
| 320438 | 09001 | 19 |
| 131733 | 09001 | 19 |
| 387427 | 09001 | 19 |
| 404201 | 09001 | 19 |
| 525449 | 09001 | 19 |
| 605542 | 09001 | 19 |
| 710740 | 09001 | 18 |
| 746380 | 09001 | 19 |
| 867492 | 09001 | 19 |
| 864637 | 09005 | 18 |
| 710741 | 09005 | 17 |
| 746375 | 09005 | 18 |
| 556470 | 09005 | 18 |
| 604258 | 09005 | 18 |
| 401597 | 09005 | 18 |
| 141331 | 09005 | 18 |
| 336054 | 09005 | 18 |
| 387423 | 09005 | 18 |
| 203706 | 09005 | 18 |
| 278651 | 09005 | 18 |
| 126352 | 09005 | 18 |
|    312 | 09005 | 17 |
|  74627 | 09005 | 17 |
+--------+-------+----+

Wasnt sure how to title the question, but here's the scenario. 不知道如何为问题加上标题,但这是方案。 Im looking to display 1 row for each distinct value in column B, Which is the highest value in column C, and the lowest in Column A. 我希望为B列中的每个不同值显示1行,C列中的最大值是A列中的最小值。

For example, for 09001 It would be 169009 09001 19, and for 09005 it would display 126352 09005 18 例如,对于09001,它将是169009 09001 19,对于09005,它将显示126352 09005 18

So basically max of column C grouped by column B, and lowest value in column A of those rows. 因此,基本上C列的最大值按B列分组,而这些行的A列最低值。

Thanks in advance 提前致谢

SELECT MIN(A) AS MaxA, B, MAX(C) AS MinC FROM Table GROUP BY B 从表GROUP BY B中选择MIN(A)AS MaxA,B,MAX(C)AS MinC

gives

310 09001   19
312 09005   18

not

131733 09001 19
141331 09005 18

which is what I need 这就是我所需要的

select min(A), B, C from my_table where ( C) in ( select max(C) from my_table where B in (09001,09005) group by B) and B in (09001,09005) group by B, C

results in 结果是

310 09001 18 310 09001 18

126352 09005 18 126352 09005 18

131733 09001 19 131733 09001 19

but there should only be one result for 09001 但是09001应该只有一个结果


Select min A,B from my_table where C in (select max(c) from my table where A in (09001, 09005)group by A) and fips in (09001, 09005) group by A

ended up working, although it only shows A and B, but all I need is A for this example. 最终工作了,尽管它只显示了A和B,但是对于此示例,我只需要A。 Im sure there are other ways to get C to show as well. 我肯定还有其他方法可以使C显示出来。

Thanks for the help guys 谢谢你们的帮助

I think you've already solved your own problem. 我认为您已经解决了自己的问题。

SELECT MIN(A) AS MaxA, B, MAX(C) AS MinC
FROM Table
GROUP BY B

Unless I made some sort of silly error there. 除非我在那里犯了某种愚蠢的错误。

EDIT: I understand the requirements better now. 编辑:我现在更好地理解要求。 Give this a shot. 试一下。

WITH T1 AS (
SELECT B, MAX(C) AS MaxC
FROM Table
GROUP BY B
), T2 AS (
SELECT MIN(A) AS MinA, MaxC
FROM T1
GROUP BY MaxC)

SELECT T2.MinA AS A, T1.B, T1.MaxC AS C
FROM T1
INNER JOIN T2
ON T1.MaxC = T2.MaxC

could you need min over the max for B 您是否需要超过B的最大值的最小值

  select  min(A), B, C
  from my_table 
  where ( B, C ) in (
    select B, max(C)
    from my_table 
    group by B
  )
 group by B,C

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

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