如何对特定 SQL 列进行分组并检索这些列的计数最高的行?
How to group specific SQL columns and retrieve rows with highest counts for those columns?
我有以下数据:
col_1 | col_2 | col_3 | col_4
-----------------------------
a1 b1 c1 d1
a1 b2 c1 d1
a1 b3 c1 d1
a1 b4 c1 d2
a1 b5 c2 d2
a1 b6 c2 d2
a1 b7 c1 d3
a1 b8 c2 d3
a1 b9 c3 d3
a1 b10 c1 d2
a1 b11 c2 d3
a2 b12 c1 d1
a3 b13 c1 d1
我有兴趣能够:
- Return 行,其中
col_1
的值是唯一的
- 对于结果中的每一行,它应该 return 分组时计数最高的列的值:
col_3
、col_4
例如,我希望输出return如下:
col_1 | col_2 | col_3 | col_4
-----------------------------
a1 b1 c1 d1
a2 b12 c1 d1
a3 b13 c1 d1
注意结果中 col_1
中的每个值都是唯一的。另请注意,对于 a1
,它 return 与 c1
和 d1
一起编辑,因为它们在 a1
.
中的计数最高
如何通过 SQL 查询实现此目的?我将把它用于 Hive SQL 查询。
您可以使用聚合和 window 函数:
select col_1, col_2, col_3, col_4
from (
select
col_1,
col_2,
col_3,
col_4,
rank() over(partition by col_1 order by count(*) desc) rn
from mytable t
group by col_1, col_2, col_3, col_4
) t
where rn = 1
具有row_number()
window功能:
select t.col_1, t.col_2, t.col_3, t.col_4
from (
select col_1, min(col_2) col_2, col_3, col_4,
row_number() over (partition by col_1 order by count(*) desc) rn
from tablename
group by col_1, col_3, col_4
) t
where t.rn = 1
参见demo。
结果:
| col_1 | col_2 | col_3 | col_4 |
| ----- | ----- | ----- | ----- |
| a1 | b1 | c1 | d1 |
| a2 | b12 | c1 | d1 |
| a3 | b13 | c1 | d1 |
如果需要完整的行,可以使用 window 函数:
select t.*
from (select t.*,
rank() over (partition by col1 order by cnt desc) as seqnum
from (select t.*, count(*) over (partition by col1, col3, col4) as cnt
from t
) t
) t
where seqnum = 1;
最里面的子查询计算每个 col1/col3/col4 组合的行数。中间的子查询为每个 col1
枚举计数最高的行。最高计数的最外层过滤器。
我有以下数据:
col_1 | col_2 | col_3 | col_4
-----------------------------
a1 b1 c1 d1
a1 b2 c1 d1
a1 b3 c1 d1
a1 b4 c1 d2
a1 b5 c2 d2
a1 b6 c2 d2
a1 b7 c1 d3
a1 b8 c2 d3
a1 b9 c3 d3
a1 b10 c1 d2
a1 b11 c2 d3
a2 b12 c1 d1
a3 b13 c1 d1
我有兴趣能够:
- Return 行,其中
col_1
的值是唯一的 - 对于结果中的每一行,它应该 return 分组时计数最高的列的值:
col_3
、col_4
例如,我希望输出return如下:
col_1 | col_2 | col_3 | col_4
-----------------------------
a1 b1 c1 d1
a2 b12 c1 d1
a3 b13 c1 d1
注意结果中 col_1
中的每个值都是唯一的。另请注意,对于 a1
,它 return 与 c1
和 d1
一起编辑,因为它们在 a1
.
如何通过 SQL 查询实现此目的?我将把它用于 Hive SQL 查询。
您可以使用聚合和 window 函数:
select col_1, col_2, col_3, col_4
from (
select
col_1,
col_2,
col_3,
col_4,
rank() over(partition by col_1 order by count(*) desc) rn
from mytable t
group by col_1, col_2, col_3, col_4
) t
where rn = 1
具有row_number()
window功能:
select t.col_1, t.col_2, t.col_3, t.col_4
from (
select col_1, min(col_2) col_2, col_3, col_4,
row_number() over (partition by col_1 order by count(*) desc) rn
from tablename
group by col_1, col_3, col_4
) t
where t.rn = 1
参见demo。
结果:
| col_1 | col_2 | col_3 | col_4 |
| ----- | ----- | ----- | ----- |
| a1 | b1 | c1 | d1 |
| a2 | b12 | c1 | d1 |
| a3 | b13 | c1 | d1 |
如果需要完整的行,可以使用 window 函数:
select t.*
from (select t.*,
rank() over (partition by col1 order by cnt desc) as seqnum
from (select t.*, count(*) over (partition by col1, col3, col4) as cnt
from t
) t
) t
where seqnum = 1;
最里面的子查询计算每个 col1/col3/col4 组合的行数。中间的子查询为每个 col1
枚举计数最高的行。最高计数的最外层过滤器。