将上一个时间戳值添加到 HIVE 查询中特定列的下一个时间戳值
Add previous timestamp value to the next timestamp value of a particular column in HIVE query
我有一个要求,我每隔 15 分钟接收一次数据。在我的输入 table 中,您可以看到有一列 Total_sales,它应该是 previous timestamp value + current_timestamp value 的聚合,而不是只有 current_timestamp 个值。
HIVE 查询 应该以这样的方式给我输出结果 table。
我试过按子句分区,但它对时间戳不起作用。
你能帮帮我吗
输入table:
timestamp_
Dept_no
Product_no
Total_Sales
2019-09-01 9:00:00
Sales
Black Ink
1
2019-09-01 9:15:00
Sales
Black Ink
1
2019-09-01 9:30:00
Sales
Black Ink
1
2019-09-01 9:00:00
Stock
Gel Pen
1
2019-09-01 9:15:00
Stock
Gel Pen
1
输出Table:
timestamp_
Dept_no
Product_no
Total_Sales
2019-09-01 9:00:00
Sales
Black Ink
1
2019-09-01 9:15:00
Sales
Black Ink
2 , like(1+1)
2019-09-01 9:30:00
Sales
Black Ink
3 , like(2+1)
2019-09-01 9:00:00
Stock
Gel Pen
1
2019-09-01 9:15:00
Stock
Gel Pen
2 ,like(1+1)
使用解析 sum() over():
with mytable as(
select '2019-09-01 9:00:00' timestamp_,'Sales' Dept_no,'Black Ink' Product_no,1 Total_Sales union all
select '2019-09-01 9:15:00','Sales','Black Ink',1 union all
select '2019-09-01 9:30:00','Sales','Black Ink',1 union all
select '2019-09-01 9:00:00','Stock','Gel Pen', 1 union all
select '2019-09-01 9:15:00','Stock','Gel Pen', 1
)
select timestamp_, Dept_no, Product_no,
sum(Total_Sales) over(partition by Dept_no order by timestamp_ rows between unbounded preceding and current row) Total_Sales
from mytable;
结果:
timestamp_ dept_no product_no total_sales
2019-09-01 9:00:00 Sales Black Ink 1
2019-09-01 9:15:00 Sales Black Ink 2
2019-09-01 9:30:00 Sales Black Ink 3
2019-09-01 9:00:00 Stock Gel Pen 1
2019-09-01 9:15:00 Stock Gel Pen 2
我有一个要求,我每隔 15 分钟接收一次数据。在我的输入 table 中,您可以看到有一列 Total_sales,它应该是 previous timestamp value + current_timestamp value 的聚合,而不是只有 current_timestamp 个值。
HIVE 查询 应该以这样的方式给我输出结果 table。
我试过按子句分区,但它对时间戳不起作用。
你能帮帮我吗
输入table:
timestamp_ | Dept_no | Product_no | Total_Sales |
---|---|---|---|
2019-09-01 9:00:00 | Sales | Black Ink | 1 |
2019-09-01 9:15:00 | Sales | Black Ink | 1 |
2019-09-01 9:30:00 | Sales | Black Ink | 1 |
2019-09-01 9:00:00 | Stock | Gel Pen | 1 |
2019-09-01 9:15:00 | Stock | Gel Pen | 1 |
输出Table:
timestamp_ | Dept_no | Product_no | Total_Sales |
---|---|---|---|
2019-09-01 9:00:00 | Sales | Black Ink | 1 |
2019-09-01 9:15:00 | Sales | Black Ink | 2 , like(1+1) |
2019-09-01 9:30:00 | Sales | Black Ink | 3 , like(2+1) |
2019-09-01 9:00:00 | Stock | Gel Pen | 1 |
2019-09-01 9:15:00 | Stock | Gel Pen | 2 ,like(1+1) |
使用解析 sum() over():
with mytable as(
select '2019-09-01 9:00:00' timestamp_,'Sales' Dept_no,'Black Ink' Product_no,1 Total_Sales union all
select '2019-09-01 9:15:00','Sales','Black Ink',1 union all
select '2019-09-01 9:30:00','Sales','Black Ink',1 union all
select '2019-09-01 9:00:00','Stock','Gel Pen', 1 union all
select '2019-09-01 9:15:00','Stock','Gel Pen', 1
)
select timestamp_, Dept_no, Product_no,
sum(Total_Sales) over(partition by Dept_no order by timestamp_ rows between unbounded preceding and current row) Total_Sales
from mytable;
结果:
timestamp_ dept_no product_no total_sales
2019-09-01 9:00:00 Sales Black Ink 1
2019-09-01 9:15:00 Sales Black Ink 2
2019-09-01 9:30:00 Sales Black Ink 3
2019-09-01 9:00:00 Stock Gel Pen 1
2019-09-01 9:15:00 Stock Gel Pen 2