gpt4 book ai didi

sql - 大查询 : JOIN ON with repeated/array STRUCT field in Standard SQL?

转载 作者:行者123 更新时间:2023-12-05 02:15:47 25 4
gpt4 key购买 nike

我基本上有两个表,OrdersItems .由于这些表是从 Google Cloud Datastore 备份文件导入的,因此引用不是通过简单的 ID 字段进行的,而是通过 <STRUCT> 进行的。对于一对一关系,它的 id字段表示我要匹配的实际唯一 ID。对于一对多关系(重复),模式使用 <STRUCT> 的数组.

我可以使用 LEFT OUTER JOIN 查询一对一关系,我也知道如何连接非重复结构和重复字符串或 int,但我无法使用重复结构

一个订单有一个一个项目:

#standardSQL
WITH Orders AS (
SELECT 1 AS __oid__, STRUCT(STRUCT(2 AS id, "default" AS ns) AS key) AS item UNION ALL
SELECT 2 AS __oid__, STRUCT(STRUCT(4 AS id, "default" AS ns) AS key) AS item UNION ALL
SELECT 3 AS __oid__, STRUCT(STRUCT(6 AS id, "default" AS ns) AS key) AS item
),
Items AS (
SELECT STRUCT(1 AS id, "default" AS ns) AS key, "#1.1" AS title UNION ALL
SELECT STRUCT(2 AS id, "default" AS ns) AS key, "#1.2" AS title UNION ALL
SELECT STRUCT(3 AS id, "default" AS ns) AS key, "#1.3" AS title UNION ALL
SELECT STRUCT(4 AS id, "default" AS ns) AS key, "#1.4" AS title UNION ALL
SELECT STRUCT(5 AS id, "default" AS ns) AS key, "#1.5" AS title UNION ALL
SELECT STRUCT(6 AS id, "default" AS ns) AS key, "#1.6" AS title
)

SELECT
__oid__
,Order_item AS item
FROM Orders

LEFT OUTER JOIN(
SELECT
key
,title
FROM Items
) Order_item
ON Order_item.key.id = item.key.id

结果(按预期工作):

+-----+---------+--------------+-------------+------------+
| Row | __oid__ | item.key.id | item.key.ns | item.title |
+-----+---------+--------------+-------------+------------+
| 1 | 1 | 2 | default | #1.2 |
+-----+---------+--------------+-------------+------------+
| 2 | 2 | 4 | default | #1.4 |
+-----+---------+--------------+-------------+------------+
| 3 | 3 | 6 | default | #1.6 |
+-----+---------+--------------+-------------+------------+

类似的查询,但这次是一个包含许多项的订单:

#standardSQL
WITH Orders AS (
SELECT 1 AS __oid__, ARRAY[STRUCT(STRUCT(1 AS id, "default" AS ns) AS key), STRUCT(STRUCT(2 AS id, "default" AS ns) AS key)] AS items UNION ALL
SELECT 2 AS __oid__, ARRAY[STRUCT(STRUCT(3 AS id, "default" AS ns) AS key), STRUCT(STRUCT(4 AS id, "default" AS ns) AS key)] AS items UNION ALL
SELECT 3 AS __oid__, ARRAY[STRUCT(STRUCT(5 AS id, "default" AS ns) AS key), STRUCT(STRUCT(6 AS id, "default" AS ns) AS key)] AS items
),
Items AS (
SELECT STRUCT(1 AS id, "default" AS ns) AS key, "#1.1" AS title UNION ALL
SELECT STRUCT(2 AS id, "default" AS ns) AS key, "#1.2" AS title UNION ALL
SELECT STRUCT(3 AS id, "default" AS ns) AS key, "#1.3" AS title UNION ALL
SELECT STRUCT(4 AS id, "default" AS ns) AS key, "#1.4" AS title UNION ALL
SELECT STRUCT(5 AS id, "default" AS ns) AS key, "#1.5" AS title UNION ALL
SELECT STRUCT(6 AS id, "default" AS ns) AS key, "#1.6" AS title
)

SELECT
__oid__
,Order_items AS items
FROM Orders

LEFT OUTER JOIN(
SELECT
key
,title
FROM Items
) Order_items
ON Order_items.key.id IN (SELECT item.key.id FROM UNNEST(items) AS item)

错误:连接谓词不支持 IN 子查询。

我其实预料到这个结果:

+-----+---------+--------------+-------------+------------+
| Row | __oid__ | item.key.id | item.key.ns | item.title |
+-----+---------+--------------+-------------+------------+
| 1 | 1 | 1 | default | #1.1 |
| | | 2 | default | #1.2 |
+-----+---------+--------------+-------------+------------+
| 2 | 2 | 3 | default | #1.3 |
| | | 4 | default | #1.4 |
+-----+---------+--------------+-------------+------------+
| 3 | 3 | 5 | default | #1.5 |
| | | 6 | default | #1.6 |
+-----+---------+--------------+-------------+------------+

如何更改第二个查询以获得预期结果?

最佳答案

另一种选择是使用 CROSS JOIN 而不是 LEFT JOIN

#standardSQL
WITH Orders AS (
SELECT 1 AS __oid__, ARRAY[STRUCT(STRUCT(1 AS id, "default" AS ns) AS key), STRUCT(STRUCT(2 AS id, "default" AS ns) AS key)] AS items UNION ALL
SELECT 2 AS __oid__, ARRAY[STRUCT(STRUCT(3 AS id, "default" AS ns) AS key), STRUCT(STRUCT(4 AS id, "default" AS ns) AS key)] AS items UNION ALL
SELECT 3 AS __oid__, ARRAY[STRUCT(STRUCT(5 AS id, "default" AS ns) AS key), STRUCT(STRUCT(6 AS id, "default" AS ns) AS key)] AS items
),
Items AS (
SELECT STRUCT(1 AS id, "default" AS ns) AS key, "#1.1" AS title UNION ALL
SELECT STRUCT(2 AS id, "default" AS ns) AS key, "#1.2" AS title UNION ALL
SELECT STRUCT(3 AS id, "default" AS ns) AS key, "#1.3" AS title UNION ALL
SELECT STRUCT(4 AS id, "default" AS ns) AS key, "#1.4" AS title UNION ALL
SELECT STRUCT(5 AS id, "default" AS ns) AS key, "#1.5" AS title UNION ALL
SELECT STRUCT(6 AS id, "default" AS ns) AS key, "#1.6" AS title
)

SELECT
__oid__
,ARRAY_AGG(Order_items) AS items
FROM Orders

CROSS JOIN(
SELECT
key
,title
FROM Items
) Order_items
WHERE Order_items.key.id IN (SELECT item.key.id FROM UNNEST(items) AS item)
GROUP BY __oid__

关于sql - 大查询 : JOIN ON with repeated/array STRUCT field in Standard SQL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51136595/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com