SQL Hive根据列值添加列

时间:2019-01-16 19:13:50

标签: sql hive hiveql

我有一个类似的查询

select
  number,
  class,
   unix_timestamp(date1) - unix_timestamp(date2) as time_example,
   sum(unix_timestamp(date1) - unix_timestamp(date2)) over(partition by unix_timestamp(date1) - unix_timestamp(date2) order by class) as class_time
from myTable

给出的结果例如

number        class         time_example       class_time
1             math          5                  5
1             science       5                  10
1             art           5                  15
1             math          2                  2
1             science       2                  4
1             art           2                  6
1             math          10                 10
1             science       10                 20
1             art           10                 30

我想基于类添加列,并且只有3个不同的列,因为只有3列。例如数学时间为17,这是我想要的表。

number        class         class_time
1             math          17
1             science       17
1             art           17

1 个答案:

答案 0 :(得分:1)

您可以使用group by

select number, class,
sum(unix_timestamp(date1) - unix_timestamp(date2)) as class_time
from myTable
group by number,class;