我正在寻找HiveQL中窗口的第一行和最后一行。我知道有几种方法可以做到这一点:
SELECT customer,
FIRST_VALUE(product) over (W),
FIRST_VALUE(time) over (W),
LAST_VALUE(product) over (W),
LAST_VALUE(time) over (W)
FROM table
WINDOW W AS (PARTITION BY customer ORDER BY COST)
计算每行的
WITH table_wRN AS
(
SELECT *,
row_number() over (partition by customer order by cost ASC) rn_B,
row_number() over (partition by customer order by cost DESC) rn_E
FROM table
),
table_first_last AS
(
SELECT *
FROM table_wRN
WHERE (rn_E=1 OR rn_B=1)
)
SELECT table_first.customer,
table_first.product, table_first.time,
table_last.product, table_last.time
FROM table_first_last as table_first WHERE table_first_last.rn_B=1
JOIN table_first_last as table_last WHERE table_first_last.rn_E=1
ON table_first.customer = table_last.customer
我的问题:有人知道这两个中哪个更有效吗?
直觉上,我认为第一个应该更快,因为不需要子查询或CTE。
- 实验上,我觉得第二个更快,但这可能是因为我在许多列上运行first_value。
FIRST_VALUE(product,time) OVER (W) AS product_first, time_first
一旦您将列数相乘,则可能会出现一个更快的问题。就是说,看看执行计划。我希望所有使用相同窗口框架规范的窗口函数都将使用相同的“窗口”处理,并对每个值进行微调。
Hive对复杂数据类型(例如字符串和数组)没有很好的支持。在这样做的数据库中,提供一个复杂的类型很容易。