0-问题描述
SQL 测试一组数据中是否存在某个值,你想根据某一组行记录里是否包含某个特定值来生成一个布尔值。试想这样一个例子,一个学生在一段时间内会参加若干场考试。假设他每 3 个月会参加 3 场考试。只要他通过了任何一场,将返回一个标志(flag)以表示考试通过。如果 3 场都没有通过,也会返回一个标志以表示考试未通过。
有如下数据:
create table v
as
select 1 student_id,
1 test_id,
2 grade_id,
1 period_id,
'2005-02-01' test_date,
0 pass_fail
union all
select 1, 2, 2, 1, '2005-03-01', 1
union all
select 1, 3, 2, 1, '2005-04-01', 0
union all
select 1, 4, 2, 2, '2005-05-01', 0
union all
select 1, 5, 2, 2, '2005-06-01', 0
union all
select 1, 6, 2, 2, '2005-07-01', 0
查看数据:
hive> select * from v;
OK
1 1 2 1 2005-02-01 0
1 2 2 1 2005-03-01 1
1 3 2 1 2005-04-01 0