调试 SQL 查询结果中缺失的数据

当您的查询返回缺失行或列的数据时,该怎么办。

使用 Metabase 学习 SQL

免费下载 Metabase,或注册 Metabase Cloud 免费试用版

您的数据在哪里缺失?

缺失行

开始之前,请确保您了解源表或嵌套查询的架构

  1. 检查您的源表或嵌套查询是否存在缺失行。
  2. 查看下表,了解您是否因连接类型而缺失行。
  3. 检查 ON 子句中的连接条件。例如

    -- The join condition below will filter out
    -- all transactions from the Orders table
    -- where the product category is 'Gizmo'.
    
    SELECT
        *
    FROM
        orders o
        JOIN products p ON o.product_id = p.id
            AND p.category <> 'Gizmo';
    
  4. 检查您的 WHERE 子句是否与您的 JOIN 子句交互。例如

    -- The WHERE clause below will filter out
    -- all transactions from the Orders table
    -- where the product category is 'Gizmo'.
    
    SELECT
        *
    FROM
        orders o
        JOIN products p ON o.product_id = p.id
                       AND p.category = 'Gizmo'
    WHERE
        p.category <> 'Gizmo'
    
  5. 如果您想为查询结果添加行以填充空白、零或 NULL 的数据,请参阅如何填充缺失报告日期的数据

连接如何筛选掉不匹配的行

连接类型 如果连接条件不满足
A INNER JOIN B 从 A 和 B 中都筛选掉行。
A LEFT JOIN B 从 B 中筛选掉行。
B LEFT JOIN A 从 A 中筛选掉行。
A OUTER JOIN B 从 A 和 B 中都筛选掉行。
A FULL JOIN B 没有行被筛选掉。

解释

JOIN 子句中表的顺序会影响查询返回的行。

例如,当您编写 LEFT JOIN 时,查询中位于 LEFT JOIN 子句*之前*的表是“左侧”表。如果“右侧”表(即位于 LEFT JOIN 子句*之后*的表)中的行不满足 ON 子句中的连接条件,则这些行将被筛选掉。

查询的执行顺序可能会以您意想不到的方式组合您的连接条件和 WHERE 子句。

延伸阅读

如何填充缺失报告日期的数据

如果您的源表或嵌套查询只存储发生过事件的日期的行,您将得到缺失报告日期的结果。

例如,示例数据库中的 Orders 表仅存储有订单创建的日期的行。它不存储没有任何订单活动的日期的行。

-- The query below calculates the total sales
-- for each day that had at least one order.

-- For example, note that there is no row
-- in the query results for May 5, 2016.


SELECT
    DATE_TRUNC('day', o.created_at)::date AS "order_created_date",
    SUM(p.price) AS "total_sales"
FROM
    orders o
    JOIN products p ON o.product_id = p.id
WHERE
    o.created_at BETWEEN'2016-05-01'::date
    AND '2016-05-30'::date
GROUP BY
    "order_created_date"
ORDER BY
    "order_created_date" ASC;

如果您想要像下表那样的结果,您需要从包含所有您需要的日期(或任何其他序列)的表或列开始您的 JOIN。询问您的数据库管理员是否有可用于此目的的表。

+--------------------+-------------+
| report_date        | total_sales |
+--------------------+-------------+
| May 4, 2016        | 98.78       |
+--------------------+-------------+
| May 5, 2016        | 0.00        |
+--------------------+-------------+
| May 6, 2016        | 87.29       |
+--------------------+-------------+
| May 7, 2016        | 0.00        |
+--------------------+-------------+
| May 8, 2016        | 81.61       |
+--------------------+-------------+

如果您的 SQL 方言支持 GENERATE_SERIES 函数,您可以创建一个临时列来存储您的报告日期。

-- The query below calculates the total sales
-- for every day in the report period,
-- including days with 0 orders.

-- The date_series CTE generates one row
-- per date that you want in your final result.

WITH date_series AS (
    SELECT
        *
    FROM
        GENERATE_SERIES('2016-05-01'::date, '2020-05-30'::date, '1 day'::interval) report_date
)

-- The fact_orders CTE generates the total sales
-- for each date that had an order.

, fact_orders AS (
    SELECT
        DATE_TRUNC('day', o.created_at)::date AS "order_created_date",
        SUM(p.price) AS "total_sales"
    FROM
        orders o
        JOIN products p ON o.product_id = p.id
    GROUP BY
        "order_created_date"
    ORDER BY
        "order_created_date" ASC
)

-- The main query joins the two CTEs together
-- and uses the COALESCE function to fill in the dates
-- where there were no orders (i.e. a total sales value of 0).

SELECT
    d.report_date,
    o.order_created_date,
    COALESCE(o.total_sales, 0) AS total_sales
FROM
    date_series d
    LEFT JOIN fact_orders o ON d.date = o.order_created_date
;

缺失列

  1. 如果您正在连接数据,请检查您的 SELECT 语句是否包含您想要的列。
    • 您是否使用了正确的表别名?
    • 您的 FROM 子句中是否缺少表?
  2. 按照调试 SQL 逻辑下的步骤 1 检查您的源表或查询结果是否存在缺失列。
  3. 了解更多关于查询结果出乎意料的常见原因

您有其他问题吗?

您还在困惑吗?

搜索或询问 Metabase 社区

© . All rights reserved.