首页 > 解决方案 > BigQuery 中的潜在客户和分析功能

问题描述

假设我的桌子是这样的

在此处输入图像描述

我正在尝试使用此信息修改我的表格

在此处输入图像描述

我添加了两列,其中列WhenWasLastBasicSubjectDone将让您知道学生在哪个学期完成了他最新的基础课程(按学期排序)。另一栏TotalBasicSubjectsDoneTillNow解释了学生到目前为止完成了多少次基础课程(主题)(按学期排序)?

我认为使用 Joins 和 UDF 很容易解决这个问题,但我想利用 BigQuery 中现有分析函数的强大功能并在没有连接的情况下解决它。

标签: sqlperformancejoingoogle-bigquerystate

解决方案


您可以为此使用窗口函数——假设您有一个指定排序的列。让我假设该列是semester

select t.*,
       max( case when subject = 'Basic' then semester end ) over (partition by student order by semester end) as lastbasic,
       sum( case when subject = 'Basic' then 1 else 0 end ) over (partition by student order by semester end) as numbasictillnow    
from t

推荐阅读