【问题标题】:BigQuery: update a row with count of a subquery and inner joinBigQuery:使用子查询和内部联接的计数更新行
【发布时间】:2020-08-10 13:30:15
【问题描述】:

我有以下两张表:

Table 1: Students
name:string,
age:integer,
class:integer,
number_of_allowed_trips:integer

Table 2: Trips
allowed_age:integer,
trip_name:string,
class:integer

我正在尝试根据某些条件为每个学生分配允许的旅行次数。我的查询是:

UPDATE
  `mydataset.students` AS s
SET
  s.number_of_allowed_trips=(
  SELECT
    COUNT(*)
  FROM
    `mydataset.trips` AS t
  WHERE
    t.r.class= 9)
FROM
  `mydataset.trips` AS t
WHERE 
  r.name = 'Jack' 
  AND r.class = 9
  AND w.class = r.class

但是这个查询不起作用。我在做什么假?我怎样才能让这段代码工作?

【问题讨论】:

  • UPDATE 中允许的位置,请参见此处:stackoverflow.com/a/47373922/14076891
  • 是的,这是允许的。但是在您之前的查询中出现了错误。我可以看到你已经修复/编辑了:-)
  • 是的 :) 我的意思是,UPDATE 中允许 FROM
  • 谢谢。希望查询现在可以正常工作!
  • 对我来说它不起作用...没有行受到影响

标签: sql google-bigquery


【解决方案1】:
UPDATE
  `mydataset.students` AS s
SET
  s.number_of_allowed_trips= (
  SELECT
    COUNT(*)
  FROM
    `mydataset.trips` AS t
  WHERE
    t.class = s.class
    AND s.age= 9  )
WHERE
  s.id=s.id

这很好用!

【讨论】:

    猜你喜欢
    • 2011-08-17
    • 2015-08-01
    • 2015-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-28
    • 2013-11-03
    • 2017-12-23
    相关资源
    最近更新 更多