簡體   English   中英

SQL Hive根據列值添加列

[英]SQL Hive add column based on column value

我有一個查詢,看起來像

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM