Google bigquery 在BigQuery中使用Where子句进行切片

Google bigquery 在BigQuery中使用Where子句进行切片,google-bigquery,subquery,analytics,Google Bigquery,Subquery,Analytics,我的查询有一个问题,试图计算术语和会话之间的分数变化,每次我尝试按如下查询所示的会话切片时,change列在所有行中都返回“null”,但如果我删除会话切片,change列将返回所需的值 ca表 name id subject session term classroom ca_cat score one 1 maths 2018/2019 1 level1 1 10 one 1 maths 2018/2019 1 leve

我的查询有一个问题,试图计算术语和会话之间的分数变化,每次我尝试按如下查询所示的会话切片时,change列在所有行中都返回“null”,但如果我删除会话切片,change列将返回所需的值

ca表

name  id  subject session   term classroom ca_cat  score
one   1    maths  2018/2019   1    level1    1       10
one   1    maths  2018/2019   1    level1    2       6
two   2    maths  2018/2019   1    level1    1       9
two   2    maths  2018/2019   1    level1    2       7
one   1    maths  2018/2019   2    level1    1       9
one   1    maths  2018/2019   2    level1    2       8
two   2    maths  2018/2019   2    level1    1       7
two   2    maths  2018/2019   2    level1    2       5        
检查表

name    id    course session   term classroom    score
one     1      maths 2018/2019   1    level1      50
two     2      maths 2018/2019   1    level1      49 
one     1      maths 2018/2019   2    level1      50
two     2      maths 2018/2019   2    level1      50  
质疑

我想要的预期输出是这样的

name, id, subject, classroom, session,    term, totalscore change
one    1    math    level1     2018/2019   1        66       null
one    1    math    level1     2018/2019   2        67        1 
two    2    math    level1     2018/2019   1        65       null
two    2    math    level1     2018/2019   2        62       -3 
分数的变化是术语之间的差异,例如第2-1和第3-2


有解决办法吗?我已经尝试了我所知道的一切,但似乎找不到它为什么会以这种方式响应,或者可能是什么问题。

下面是针对BigQuery标准SQL的

#standardSQL
SELECT *, 
  totalscore - LAG(totalscore) OVER(PARTITION BY name, id, subject, classroom, session ORDER BY term) AS change
FROM (
  SELECT name, id, subject, classroom, session, term, caone + catwo + exam AS totalscore
  FROM (
    SELECT name, id, subject, classroom, session, term, 
      MAX(IF(ca_cat = 1, ca.score, NULL)) AS caone,
      MAX(IF(ca_cat = 2, ca.score, NULL)) AS catwo,
      ANY_VALUE(ex.score) AS exam,
    FROM `project.dataset.exam` ex
    JOIN `project.dataset.catable` ca
    USING (name, id, subject, classroom, session, term) 
    GROUP BY name, id, subject, classroom, session, term 
  )
)
-- ORDER BY name, id, subject, classroom, session, term   
当应用于问题中的样本数据时-结果为

Row name    id  subject classroom   session     term    totalscore  change   
1   one     1   maths   level1      2018/2019   1       66          null     
2   one     1   maths   level1      2018/2019   2       67          1    
3   two     2   maths   level1      2018/2019   1       65          null     
4   two     2   maths   level1      2018/2019   2       62          -3     

预期产量是多少?解释逻辑!我已经用所需的输出编辑了这个问题。为什么名为
two
的项目在这里丢失了?这是有原因的吗?不,第二个应该在那里。我只是以该表为例。您的预期结果和查询与输入完全不匹配!请澄清你的问题,这样我们就不会在这里浪费时间了。谢谢。这是有效的,但是我不能用术语来过滤,如果我试图用术语列来过滤,例如,期限=1,所有的变化值都被Null代替。你的问题被回答,因为它被问到-如果它工作-考虑投票并接受答案!如果您有更多的问题或后续问题-考虑发布新的问题,我们将乐意进一步帮助:O)
Row name    id  subject classroom   session     term    totalscore  change   
1   one     1   maths   level1      2018/2019   1       66          null     
2   one     1   maths   level1      2018/2019   2       67          1    
3   two     2   maths   level1      2018/2019   1       65          null     
4   two     2   maths   level1      2018/2019   2       62          -3