需要连接三个表才能获取所有行。如果没有匹配,则将其设为空。当前查询耗时较长

问题描述 投票:0回答:1

输入示例:
房子_桌子
id 日期 house_nbr house_qty
1 2025-01-18 101 50
2 2025-01-19 102 60

俱乐部_桌子
ID 日期club_nbrclub_qty
1 2025-01-18 201 30
3 2025-01-20 202 40

存储_表
ID 日期 storage_nbr storage_qty
1 2025-01-18 301 20
4 2025-01-21 302 10

输出示例:
ID 日期 house_nbr house_qtyclub_nbrclub_qty storage_nbr storage_qty 1 2025-01-18 101 50 201 30 301 20 2 2025-01-19 102 60 空 空 空 空 3 2025-01-20 空 空 202 40 空 空 4 2025-01-21 空 空 空 空 302 10

SELECT 
    COALESCE(s.id, d.id, f.id) AS id,
    COALESCE(s.date, d.date, f.date) AS date,
    s.house_nbr,
    s.house_qty,
    d.club_nbr,
    d.club_qty,
    f.storage_nbr,
    f.storage_qty
FROM house s
FULL OUTER JOIN club d
    ON s.id = d.id AND s.date = d.date
FULL OUTER JOIN storage f
    ON COALESCE(s.id, d.id) = f.id AND COALESCE(s.date, d.date) = f.date
ORDER BY id, date

enter image description here

sql google-bigquery hive
1个回答
0
投票

下面应该更快更便宜

WITH combined_data AS (
  SELECT id, date, house_nbr, house_qty, 
    NULL AS club_nbr, NULL AS club_qty, NULL AS storage_nbr, NULL AS storage_qty
  FROM house UNION ALL
  SELECT id, date, NULL, NULL, club_nbr, club_qty, NULL, NULL
  FROM club UNION ALL
  SELECT id, date, NULL, NULL, NULL, NULL, storage_nbr, storage_qty
  FROM storage
)
SELECT id, date,
  MAX(house_nbr) AS house_nbr,
  MAX(house_qty) AS house_qty,
  MAX(club_nbr) AS club_nbr,
  MAX(club_qty) AS club_qty,
  MAX(storage_nbr) AS storage_nbr,
  MAX(storage_qty) AS storage_qty
FROM combined_data
GROUP BY id, date
ORDER BY id, date;

输出与预期相同。

output

© www.soinside.com 2019 - 2024. All rights reserved.