sh-3.2$ bq shell
Welcome to BigQuery! (Type help for more information.)
bqaccount> select 1;
Waiting on bqjob_r42d56b0f0c2a3eb9_00000164b439b383_1 ... (0s) Current status: DONE
+-----+
| f0_ |
+-----+
| 1 |
+-----+
bqaccount> select f0_ from (select 1);
Error in query string: Error processing job 'bqaccount:bqjob_r7d8d3d2925de7c0c_00000164b439def3_1': Unrecognized name: f0_ at [1:8]
是否可以从没有别名的查询中选择特定列?
答案 0 :(得分:2)
使用BigQuery旧版SQL可以引用fn_
#legacySQL
SELECT f2_, f0_, f1_
FROM (SELECT 1, 2, 3)
结果是
Row f2_ f0_ f1_
1 3 1 2
尽管
似乎在BigQuery Standard SQL中不受支持因此,以下是BigQuery标准SQL的一种解决方法
#standardSQL
SELECT
cols[OFFSET(0)],
cols[OFFSET(1)],
cols[OFFSET(2)],
cols[OFFSET(3)],
cols[OFFSET(4)]
FROM (SELECT 1, 2, 3, 'abc', CURRENT_DATE()) t,
UNNEST([
STRUCT<cols ARRAY<STRING>>(REGEXP_EXTRACT_ALL(TO_JSON_STRING(t), r'"[^"]?":"?(\w*)"?'))
])
或另一种完全相同的方法(无论您会发现哪种方式更好)
#standardSQL
SELECT
cols[OFFSET(0)],
cols[OFFSET(1)],
cols[OFFSET(2)],
cols[OFFSET(3)],
cols[OFFSET(4)]
FROM (
SELECT REGEXP_EXTRACT_ALL(TO_JSON_STRING(t), r'"[^"]?":"?(\w*)"?') cols
FROM (SELECT 1, 2, 3, 'abc', CURRENT_DATE()) t
)
有结果
Row f0_ f1_ f2_ f3_ f4_
1 1 2 3 abc 2018
答案 1 :(得分:1)
您可以将其强制转换为具有列名称的结构:
SELECT CAST(t AS STRUCT<f0_ INT64, f1_ INT64, f2_ INT64>).f0_
FROM (SELECT 1, 2, 3) AS t
答案 2 :(得分:0)
我认为第一个?在...
SELECT REGEXP_EXTRACT_ALL(TO_JSON_STRING(t),r'“ [^”]?“:”?(\ w *)“?')cols
...应为*或+