Postgresql:如何在加入时删除重复的行?

Postgresql: How to remove duplicate rows while joining?

我有两个名为 chargesorders 的 postgresql table。我正在尝试创建一个 matview,其中包含有多少费用转化为订单及其价值的数据。两个table没有直接关系,这里是table两个

的结构
Charges

| date   | transaction_id | amount |
|--------|----------------|--------|
| 23-Apr | abcdef         | 36     |
| 23-Apr | fghijkl        | 198    |
| 24-Apr | yyyyyy         | 200    |

Orders

| date   | order_id |
|--------|----------|
| 23-Apr | abcdef   |
| 23-Apr | abcdef   |
| 24-Apr | yyyyyy   |

下面是我用于生成 matview 的查询,

CREATE MATERIALIZED VIEW sales AS
    SELECT ch.date AS date,
           (ord.id IS NOT NULL) as placed_order,
           COUNT(DISTINCT(ch.transaction_id)) AS attempts,
           SUM(ch.amount) AS amount
    FROM charges ch
    LEFT OUTER JOIN orders as ord ON ch.transaction_id = ord.order_id
    GROUP BY ch.date

问题是由视图中生成的 Amount 列引起的。由于orderstable中的重复,左外连接时返回了charges中的多行,数量基本在增加。

有没有办法在加入自身时从订单中 Distinct order_id 列?

或者有没有办法在查询时区分 order_id 和总和?我尝试了子查询和自连接,但没有成功。

您可以在 table orders 上进行子查询以过滤掉重复项:

CREATE MATERIALIZED VIEW sales AS
  SELECT ch.date AS date,
         (ord.order_id IS NOT NULL) AS placed_order,
         count(ch.transaction_id) AS attempts,
         sum(ch.amount) AS amount
  FROM charges ch
  LEFT JOIN (
    SELECT DISTINCT date, order_id FROM orders) ord ON ch.transaction_id = ord.order_id
  GROUP BY 1, 2