.创建外部表examination_info,并且分区字段为release_time,输出2021年里,所有每次试卷得分都能到85分的人以及至少有一次用了一半时间就完成高难度试卷且分数大于80的人的id和活动号,按用户ID排序输出
时间: 2024-11-22 13:34:30 浏览: 11
要在Hive中创建一个名为`examination_info`的外部表,并将其分区基于`release_time`,首先需要确认你已经设置了正确的Hive环境和仓库路径。假设Hive表结构中有`user_id`, `activity_num`, `score`, `difficulty`, 和 `completion_time`等字段,可以按照以下步骤创建并查询所需数据:
```sql
CREATE EXTERNAL TABLE examination_info (
user_id INT,
activity_num STRING,
score INT,
difficulty STRING,
completion_time BIGINT,
release_time TIMESTAMP
)
PARTITIONED BY (release_time STRING)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY ','
STORED AS TEXTFILE;
-- 查询条件:
-- 得分能到85分
-- 至少有一次用了一半时间完成高难度试卷且分数大于80
SELECT DISTINCT user_id, activity_num
FROM examination_info
WHERE
score >= 85 AND
difficulty = 'high' AND
completion_time > (SELECT 5 * 60 * 1000 FROM UNNEST(array_timestamp(release_time)) t) -- 半小时
AND
year(from_unixtime(release_time, 'yyyy-MM-dd HH:mm:ss')) = 2021
ORDER BY user_id;
```
此查询会筛选出符合条件的用户ID和活动号,并按用户ID排序。
阅读全文