[英]Iterating over a column in Big query
I am using Big query to extract results from employee learning table.我正在使用大查询从员工学习表中提取结果。 I need to figure out if an employee has completed a set of courses for a department.
我需要弄清楚员工是否完成了部门的一组课程。 For example if an employee complete all of these three courses say, course 100, course 200 and course 300, they can be classified as as compliant else They are non-compliant.
例如,如果一名员工完成了所有这三门课程,比如课程 100、课程 200 和课程 300,他们可以被归类为合规,否则他们是不合规的。 I have created a dummy example of how my data is structured, unfortunately due to organization policy I cant share more information.
我创建了一个虚拟示例来说明我的数据是如何构建的,不幸的是,由于组织政策,我无法分享更多信息。
Employee course
1 100
1 101
1 200
1 300
1 300
1 400
2 100
2 200
3 100
3 200
3 300
4 75
4 85
4 95
4 105
4 115
4 125
5 200
5 200
5 100
5 100
5 100
5 300
5 300
6 100
7 100
8 300
8 200
8 100
8 101
8 102
9 100
9 200
9 300
My initial thoughts are to create columns with 1 and 0 say using case statement if course id 100 then 1 else 0 and try to sum to three new created columns by at employee level.我最初的想法是创建包含 1 和 0 的列,例如使用 case 语句 if course id 100 then 1 else 0 并尝试在员工级别汇总三个新创建的列。 any suggestion would be welcome.
欢迎提出任何建议。
Use below simple approach使用下面的简单方法
select Employee, array_length(array_agg(distinct course)) = 3 isCompliant
from your_table
where course in (100, 200, 300)
group by Employee
if applied to sample data in your question - output is如果应用于您问题中的示例数据 - output 是
The better option is更好的选择是
select Employee,
ifnull(array_length(split(string_agg(distinct if(course in (100, 200, 300), '' || course, null)))), 0) = 3 isCompliant,
from your_table
group by Employee
with [what appears like better] output:o)与[看起来更好] output:o)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.