如何在 table Sql 中查找缺失数据
How to find missing data in table Sql
这与 How to find missing data rows using SQL? and How to find missing rows (dates) in a mysql table? 类似,但有点复杂,所以我碰壁了。
我有一个数据 table 带有注明的主键:
country_id (PK)
product_id (PK)
history_date (PK)
amount
我有一个包含所有产品的产品 table、一个国家 table 和一个包含所有有效日期的日历 table。
我想查找所有缺少产品的国家、日期和产品,并且有以下问题:
我只关心某个国家/地区至少有一种产品的条目的日期(即,如果该国当天没有任何东西,我不需要找到它) - 所以,根据定义,有一个条目在我关心的每个国家和日期的历史 table。
我知道这将涉及一些连接,也许是交叉连接,但我在查找缺失数据方面遇到了真正的障碍。
我试过了(很确定它行不通):
SELECT h.history_date, h.product_id, h.country_id, h.amount
FROM products p
LEFT JOIN history h ON (p.product_id = h.product_id)
WHERE h.product_id IS NULL
没有快乐。
我也试过这个:
WITH allData AS (SELECT h1.country_id, p.product_id, h1.history_date
FROM products p
CROSS JOIN (SELECT DISTINCT country_id, history_date FROM history) h1)
SELECT f.history_date, f.product_id, f.country_id
FROM allData f
LEFT OUTER JOIN history h ON (f.country_id = h.country_id AND f.history_date = h.history_date AND f.product_id = h.product_id)
WHERE h.product_id IS NULL
AND h.country_id IS NOT NULL
AND h.history_date IS NOT null
也没有运气。 CTE 确实让我在每个日期的每个产品都有数据,但其余的 returns 什么都没有。
I only care about dates for which there are entries for a country for
at least one product (i.e. if the country has NOTHING on that day, I
don't need to find it)
所以我们关心这个组合:
from (select distinct country_id, history_date from history) country_date
cross join products p
那么只需要检查是否存在即可:
select *
from (select distinct country_id, history_date from history) country_date
cross join products p
where not exists (select null
from history h
where country_date.country_id = h.country_id
and country_date.history_date = h.history_date
and p.product_id = h.product_id
)
这与 How to find missing data rows using SQL? and How to find missing rows (dates) in a mysql table? 类似,但有点复杂,所以我碰壁了。
我有一个数据 table 带有注明的主键:
country_id (PK)
product_id (PK)
history_date (PK)
amount
我有一个包含所有产品的产品 table、一个国家 table 和一个包含所有有效日期的日历 table。
我想查找所有缺少产品的国家、日期和产品,并且有以下问题: 我只关心某个国家/地区至少有一种产品的条目的日期(即,如果该国当天没有任何东西,我不需要找到它) - 所以,根据定义,有一个条目在我关心的每个国家和日期的历史 table。
我知道这将涉及一些连接,也许是交叉连接,但我在查找缺失数据方面遇到了真正的障碍。
我试过了(很确定它行不通):
SELECT h.history_date, h.product_id, h.country_id, h.amount
FROM products p
LEFT JOIN history h ON (p.product_id = h.product_id)
WHERE h.product_id IS NULL
没有快乐。
我也试过这个:
WITH allData AS (SELECT h1.country_id, p.product_id, h1.history_date
FROM products p
CROSS JOIN (SELECT DISTINCT country_id, history_date FROM history) h1)
SELECT f.history_date, f.product_id, f.country_id
FROM allData f
LEFT OUTER JOIN history h ON (f.country_id = h.country_id AND f.history_date = h.history_date AND f.product_id = h.product_id)
WHERE h.product_id IS NULL
AND h.country_id IS NOT NULL
AND h.history_date IS NOT null
也没有运气。 CTE 确实让我在每个日期的每个产品都有数据,但其余的 returns 什么都没有。
I only care about dates for which there are entries for a country for at least one product (i.e. if the country has NOTHING on that day, I don't need to find it)
所以我们关心这个组合:
from (select distinct country_id, history_date from history) country_date
cross join products p
那么只需要检查是否存在即可:
select *
from (select distinct country_id, history_date from history) country_date
cross join products p
where not exists (select null
from history h
where country_date.country_id = h.country_id
and country_date.history_date = h.history_date
and p.product_id = h.product_id
)