gpt4 book ai didi

sql - 从每组中获取具有最高值的行?

转载 作者:行者123 更新时间:2023-12-04 22:41:30 25 4
gpt4 key购买 nike

我有这样的数据:

Table
Group: "A" , Color: "Blue", Count: "400"
Group: "A" , Color: "Green", Count: "3"
Group: "A" , Color: "Yellow", Count: "6"
Group: "A" , Color: "Red", Count: "1"
Group: "B" , Color: "Purple", Count: "243"
Group: "B" , Color: "Green", Count: "2"
Group: "B" , Color: "Yellow", Count: "7"

我如何查询此数据以获得每个组最流行(按计数)的颜色。所以结果看起来像这样:

Result
Group: "A", Color: "Blue"
Group: "B", Color: "Purple"

最佳答案

SELECT Group, Color
FROM MyTable t
INNER JOIN (SELECT Group, Max(CAST(Count as int)) as Ct
FROM MyTable
GROUP BY Group) Sub
ON sub.group = t.group
AND sub.ct = t.count

顺便说一句,不要将字段命名为 GROUPCOUNT。它们是关键字,会导致头痛和心碎。

关于sql - 从每组中获取具有最高值的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9180283/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com