Alias'应该在分组时级联?

时间:2016-09-07 16:22:19

标签: google-bigquery

按照以某种方式编辑的值进行分组并重命名相同的值时,更新的值(而不是原始值)似乎会将select语句级联起来。这是预期的行为吗?

SELECT
  a+1 AS a,
  FIRST(a) AS b
FROM (SELECT 1 AS a),
     (SELECT 2 AS a)
GROUP BY
  a    

-- a | b
-- -----
-- 2 | 2
-- 3 | 3

VS

SELECT
  a+1 AS a_incremented,
  FIRST(a) AS b
FROM (SELECT 1 AS a),
     (SELECT 2 AS a)
GROUP BY
  a_incremented

-- a_incremented | b     
--       2       | 1
--       3       | 2

1 个答案:

答案 0 :(得分:0)

以下示例提供了对此

的更多见解
SELECT
  a + 1 AS a,
  FIRST(a) AS b,
  FIRST(a + 0) AS b0,
  FIRST(a + 1) AS b1,
  FIRST(a + 2) AS b2
FROM (SELECT 1 AS a),
     (SELECT 2 AS a)
GROUP BY
  a   

a   b   b0  b1  b2   
2   2   1   2   3    
3   3   2   3   4   

我为自己找到了通过这种混乱的规则 如果我在任何表达式中使用重载字段/别名(在本例中为a)(例如b0,b1,b2),它(BigQuery)使用原始字段
如果我自己使用那个重载的字段/别名(对于b),它使用别名