sql - T-SQL 数据清理 - 当元组多次出现时,如果某些属性为 NULL,则备用行
问题描述
带有样本数据的表格:
UserID | DateID | Code | Type
0815 20191211 'oef' xx -> keep that row in the result
0815 20191211 'oef' NULL -> should not be in the result set because
0916 20191212 'bin' NULL -> keep that row in the result set if there is just one occurrence for this User at that day.
在上面的示例中,类型和代码可以为 NULL。如果 Type 为 NULL,则应应用条件数据清理。第二行不应该在结果集中,因为与第一行的唯一区别是类型为 NULL。第三行在该用户当天仅存在一次,并带有该代码,因此应保留。
我无法想象一个优雅而高效的解决方案来完成这项清理任务。因此,如果有人有想法,我会很高兴。
UserID 和 DateID 上有聚集索引(如果有帮助,我可以将其更改为列存储 - MS SQL Server 2016)。我们正在谈论该表中的 100.000.000 行。
解决方案
如果我理解正确,您需要两列中的值不是的所有行NULL
。
那么你想要NULL
的值是没有基于另一列的对应行。根据我解释为您想要的内容:
select t.*
from t
where (t.code is not null and t.type is not null) or
(t.code is null and
not exists (select 1
from t t2
where t2.user = t.user and t2.dateid = t.dateid and
t2.code is not null and
(t2.type = t.type or t2.type is null and t.type is null)
)
) or
(t.type is null and
not exists (select 1
from t t2
where t2.user = t.user and t2.dateid = t.dateid and
t2.type is not null and
(t2.code = t.code or t2.code is null and t.code is null)
)
) ;
推荐阅读
- javascript - 如何解决在节点应用程序中禁用的 body-parser 模块?
- python - 我如何使用 python 3.8 下载 tensorflow
- python - 谁能帮我理解,下面的代码是如何工作的?
- python - 通过 Python 拆分命令行输出
- reactjs - React 和 .Net Core Web API:Axios API 显示状态“待定”。无法获得想要的数据
- powershell - 保存 Office 365 邮箱中的电子邮件附件
- php - :SQLSTATE [HY000]:一般错误:1364 字段
- flutter - 如何在颤振中使用 googleapis 包将视频上传到 YouTube?
- bash - 使用 crontab 备份 Git Repo 的 Shell 脚本
- reactjs - 如何在 react-firestore 中使用 LIKE?