在Hive中计算rise涨幅字段大于0的股票代码和最大连续天数
时间: 2024-04-30 09:17:09 浏览: 206
hive数据分区时分区字段不可为中文。.doc
可以使用Hive的窗口函数和自连接实现此计算。首先针对涨幅大于0的行进行排名,然后进行自连接查找相邻的涨幅大于0的行,最后统计每个连续上涨区间的长度。具体的Hive SQL语句如下:
```
WITH t1 AS (
SELECT
stock_code,
rise,
ROW_NUMBER() OVER (PARTITION BY stock_code ORDER BY date) AS rn
FROM
stock_data
WHERE
rise > 0
),
t2 AS (
SELECT
t1a.stock_code,
t1a.rn,
SUM(CASE WHEN t1b.rise <= 0 THEN 1 ELSE 0 END) OVER (PARTITION BY t1a.stock_code ORDER BY t1b.rn) AS grp
FROM
t1 t1a
LEFT JOIN t1 t1b ON t1a.stock_code = t1b.stock_code AND t1a.rn < t1b.rn
)
SELECT
stock_code,
MAX(rn - grp) AS max_days
FROM
t2
GROUP BY
stock_code;
```
阅读全文