sql - 在 SQL 中查找丢失/删除的行
问题描述
我有一个表,它存储来自源的完整提取。我想跟踪完整提取中缺少的 id 并将其存储在目标表(所需输出)格式中。你能提供关于sql的建议吗?
源表
id source_extract_date
1 1/1/2001
2 1/1/2001
3 1/1/2001
4 1/1/2001
1 2/1/2001
3 2/1/2001
4 2/1/2001
1 3/1/2001
3 3/1/2001
4 3/1/2001
1 4/1/2001
3 4/1/2001
2 4/1/2001
1 5/1/2001
3 5/1/2001
2 5/1/2001
目标表(期望的输出)
id effective_from is_deleted
1 1/1/2001 false
2 1/1/2001 false
3 1/1/2001 false
4 1/1/2001 false
2 2/1/2001 true
4 4/1/2001 true
2 4/1/2001 false
解决方案
SELECT possibles.*, case when st.id is null then 'True' Else 'False' END As IsDeleted
FROM (
select ids.id, dates.sources_extract_date
from
(select distinct id from [source_table]) ids
cross join
(select distinct source_extract_date from [source_table]) dates
) possibles
LEFT JOIN [source_table] st ON st.id = possibles.id and st.source_extract_date = possibles.source_extract_date
推荐阅读
- ios - pangestures 像 iOS 中的邮件应用程序一样交互
- url - 如何在身份验证提供程序的重定向 uri 中包含哈希片段
- ios - 无效的应用商店图标
- keycloak - kcadm 添加新的协议映射器
- c# - 辅助副本在 Service Fabric 中是否必不可少
- ocr - 识别身份证中的字段
- selenium-webdriver - 在 Selenium 中使用 xpath 选择列表中的多个元素
- c++ - 在不同架构中使用 Boost 进行序列化和反序列化
- typescript - TypeScript [ts] 元素隐式具有“任何”类型,因为类型“{}”没有索引签名
- angular - Angular dev bulild 从 URL 中删除/删除散列之前的参数