max(sum(Hive / SQL中的字段查询)

时间:2017-06-02 07:05:13

标签: sql hive hiveql

我有一个表,其中包含一个月内用户的大量交易。

我需要从Sum(成本)最高的每一天开始。

我尝试了MAX(SUM(成本)),但收到错误。

我该怎么做呢?

这里有一些示例数据

+-------------+------+----------+------+
|   user id   | hour |   date   | Cost |
+-------------+------+----------+------+
|      343252 |   13 | 20170101 | 21.5 |
|    32532532 |   13 | 20170101 | 22.5 |
|    35325325 |   13 | 20170101 | 30.5 |
|   325325325 |   13 | 20170101 | 10   |
|    64643643 |   12 | 20170101 | 22   |
|   643643643 |   12 | 20170101 | 31   |
|   436325234 |   13 | 20170101 | 15   |
|   213213213 |   13 | 20170101 | 12   |
| 53265436436 |   17 | 20170101 | 19   |
+-------------+------+----------+------+

预期输出:

我每天只需要一排,它显示了“最贵”小时的总费用。在这种情况下,13:00的总费用为111.5

enter image description here

2 个答案:

答案 0 :(得分:1)

select      hr
           ,dt
           ,total_cost

from       (select      dt
                       ,hr
                       ,sum(cost)   as total_cost
                       ,row_number () over
                        (
                            partition by    dt
                            order by        sum(cost) desc
                        ) as rn

            from        mytable
            group by    dt,hr
            ) t

where       rn = 1
+----+------------+------------+
| hr |     dt     | total_cost |
+----+------------+------------+
| 13 | 2017-01-01 | 111.5      |
+----+------------+------------+

答案 1 :(得分:-2)

试试这个:

select AVG(hour) as 'Hour',date as 'Date',sum(cost) as 'TotalCost' from dbo.Table_3 group by date