小编Sea*_*ean的帖子

加入时包含 NULL 行

如何从 table_A 中选择所有行,然后连接来自其他表的结果,但包含一个 NULL 行以显示那些没有 table_A_id 的其他表的聚合值?

WITH RECURSIVE transactions_with_children AS (
  SELECT
    table_A_id,
    other_stuff,
    1 AS depth
  FROM transactions
  WHERE transactions.parent_transaction_id IS NULL
  UNION ALL
  SELECT
    table_A_id,
    other_stuff,
    depth + 1 AS depth
  FROM transactions children
    INNER JOIN transactions_with_children parents ON children.parent_transaction_id = parents.id
)

SELECT 
  table_A.id,
  view_B.aggregate_col1, view_B.aggregate_col2,
  view_C.aggregate_col1, view_C.aggregate_col2

FROM table_A
  -- Limit table_A to only records with data from transactions_with_children, but I also need to include
  -- a null row for all transactions_with_children that don't have …
Run Code Online (Sandbox Code Playgroud)

postgresql join group-by

4
推荐指数
1
解决办法
1万
查看次数

标签 统计

group-by ×1

join ×1

postgresql ×1