使用groupby特定列,根据一列的最大值查找其他列值

时间:2016-06-28 06:07:25

标签: sql hive

我有以下数据

+-------+---------+--------+
| Count |  Mindif | Device |
+-------+---------+--------+
|    45 |       3 |    A   |
|    78 |       4 |    A   |
|    52 |       5 |    A   |
|    24 |       6 |    A   |
|    22 |       1 |    B   |
|    22 |       2 |    B   |
|    34 |       3 |    B   |
|    37 |       4 |    B   |
|    52 |       5 |    B   |
|    34 |       6 |    B   |
|    13 |       1 |    C   |
|    30 |       2 |    C   |
|    57 |       3 |    C   |
|   111 |       4 |    C   |
|    35 |       5 |    C   |
+-------+---------+--------+

想要根据计数的最大值找到Mindif和设备。 输出就像

+-------+---------+--------+
| Count |  Mindif | Device |
+-------+---------+--------+
|    78 |       4 |    A   |
|    52 |       5 |    B   |
|   111 |       4 |    C   |
+-------+---------+--------+

2 个答案:

答案 0 :(得分:0)

您可以使用以下查询:

SELECT t1.Count, t1.Mindif, t1.Device
FROM mytable AS t1
JOIN (
   SELECT Device, MAX(Count) AS Count
   FROM mytable 
   GROUP BY Device
) AS t2 ON t1.Device = t2.Device AND t1.Count = t2.Count

查询使用派生表,该表返回每Count的最大Device值。加入原始表格,我们可以得到理想的结果。

答案 1 :(得分:0)

使用窗口函数

SELECT Count, Mindif, Device
FROM
 (SELECT Count, Mindif, Device,
         rank() over (order by Count desc) as r 
  FROM table) S 
WHERE S.r = 1;

OR

简单加入MAX

SELECT a.* FROM table a 
LEFT SEMI JOIN  
(SELECT MAX(Count)Cnt 
 FROM table)b on (a.Count  = b.Cnt)