【发布时间】:2021-08-12 18:49:43
【问题描述】:
我正在尝试创建一个表格,该表格根据设定日期前 X 天显示不同的百分位值。
我使用的表格是这样的;
| id | score | date | actual_score | survey_date |
|---|---|---|---|---|
| A | 3 | 04-05-2021 | 4 | 05-05-2021 |
| A | 4 | 03-05-2021 | 5 | 05-05-2021 |
我当前的查询如下;
SELECT
id,
PERCENTILE(score,0.25) AS p25_30_day_prior,
PERCENTILE(score,0.50) AS p50_30_day_prior,
PERCENTILE(score,0.75) AS p75_30_day_prior,
MAX(actual_score) AS actual_score
FROM Table A
WHERE date < survey_date and date >= date_sub(survey_date, 30)
GROUP BY id
ORDER BY id;
这很好用,但是我希望在 2014 年 1 月 7 日以及调查日期前 30 天有多个百分位数 0.25、0.50 和 0.75。目前我使用 where 条件来获取正确的日期范围。我如何在如下表中包含多个百分位数?
| id | p25_30_day_prior | p25_14_day_prior | p25_7_day_prior | p25_1_day_prior |
|---|---|---|---|---|
| A | 3 | 4 | 3.5 | 5 |
我假设我需要使用临时表并加入?
【问题讨论】:
标签: sql hive hiveql percentile