【问题标题】:SQL Hive add column based on column valueSQL Hive 根据列值添加列
【发布时间】:2019-01-16 19:13:49
【问题描述】:

我有一个看起来像这样的查询

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

【问题讨论】:

  • 提示:GROUP BY.

标签: sql hive hiveql


【解决方案1】:

你可以使用group by来做到这一点

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

【讨论】:

    猜你喜欢
    • 2021-07-16
    • 1970-01-01
    • 1970-01-01
    • 2019-03-25
    • 2021-08-29
    • 1970-01-01
    • 1970-01-01
    • 2021-11-29
    • 2018-11-07
    相关资源
    最近更新 更多