は、所望の出力を与えるTO_JSON_STRING
機能を使用していますか?
WITH `project.dataset.table` AS (
SELECT 1 AS id, 'Y' AS code1, 3 AS code2, 'A' AS code3, 'IA' AS data1 UNION ALL
SELECT 2, 'Y', 2, 'B', 'IB' UNION ALL
SELECT 3, 'N', 5, 'C', 'IC'
)
SELECT REPLACE(TO_JSON_STRING(t), '"', '') AS json
FROM `project.dataset.table` AS t;
+-----------------------------------------+
| json |
+-----------------------------------------+
| {id:1,code1:Y,code2:3,code3:A,data1:IA} |
| {id:2,code1:Y,code2:2,code3:B,data1:IB} |
| {id:3,code1:N,code2:5,code3:C,data1:IC} |
+-----------------------------------------+
編集:これは、正確な所望の出力を与える
WITH `project.dataset.table` AS (
SELECT 1 AS id, 'Y' AS code1, 3 AS code2, 'A' AS code3, 'IA' AS data1 UNION ALL
SELECT 2, 'Y', 2, 'B', 'IB' UNION ALL
SELECT 3, 'N', 5, 'C', 'IC'
)
SELECT TO_JSON_STRING(t) AS json
FROM `project.dataset.table` AS t;
+---------------------------------------------------------+
| json |
+---------------------------------------------------------+
| {"id":1,"code1":"Y","code2":3,"code3":"A","data1":"IA"} |
| {"id":2,"code1":"Y","code2":2,"code3":"B","data1":"IB"} |
| {"id":3,"code1":"N","code2":5,"code3":"C","data1":"IC"} |
+---------------------------------------------------------+
あなたが引用符を取り除くしたい場合は、あなたもそれを行うことができます:ここではあなたのデータを使用した例です。私はあなたが同じ方法でそれらをフォーマットしたくないように聞こえるので、id
とdata
を参照することで大丈夫だと仮定しています。
WITH `project.dataset.table` AS (
SELECT 1 AS id, 'Y' AS code1, 3 AS code2, 'A' AS code3, 'IA' AS data1 UNION ALL
SELECT 2, 'Y', 2, 'B', 'IB' UNION ALL
SELECT 3, 'N', 5, 'C', 'IC'
)
SELECT
REGEXP_REPLACE(
FORMAT(
'%d %s %s',
id,
REGEXP_REPLACE(
TO_JSON_STRING(
(SELECT AS STRUCT t.* EXCEPT (id, data1))
),
'["{}]', ''),
data1
),
r'[ ,]', ', '
) AS output
FROM `project.dataset.table` AS t;
+----------------------------------+
| output |
+----------------------------------+
| 1, code1:Y, code2:3, code3:A, IA |
| 2, code1:Y, code2:2, code3:B, IB |
| 3, code1:N, code2:5, code3:C, IC |
+----------------------------------+
あなたは「私は列名をハードコードできません」ということを明確にすることはできますか? –