sql - 如何在sql中的表中按ID填充缺失的日期
问题描述
我有表 A,其中包含日期和 EMPID,例如下面
date EMPID
8/06/19 1
8/07/19 1
8/08/19 1
8/09/19 1
8/07/19 2
8/09/19 2
8/12/19 2
我也有一个日期范围的表 B
date
...
8/05/19
8/06/19
8/07/19
8/08/19
8/09/19
8/10/19
8/11/19
8/12/19
8/13/19
...
我的表 A 缺少日期和 EMPID。
如何合并这两个表以获得下表。
Date EMPID
8/05/19 1
8/06/19 1
8/07/19 1
8/08/19 1
8/09/19 1
8/10/19 1
8/11/19 1
8/12/19 1
8/13/19 1
8/05/19 2
8/06/19 2
8/07/19 2
8/08/19 2
8/09/19 2
8/10/19 2
8/11/19 2
8/12/19 2
8/13/19 2
提前致谢。这正在 SSRS 的数据集(SQL)中使用。PS 我是 SQL 环境中编码的新手,我的背景是 ABAP
解决方案
您可以使用来自的日期来cross join
区分来自,如下所示:empid
a
b
select b.date, a.empid
from (select distinct empid from a) a
cross join b
或者,如果您正在寻找insert
“缺失”的日期a
,那么您可以使用insert ... select
带有not exists
条件的语法:
insert into a (date, empid)
select b.date, a.empid
from (select distinct empid from a) a
cross join b
where not exists (select 1 from a a1 where a1.empid = a.empid and a1.date = b.date)
推荐阅读
- numpy - skimage 旋转后的图像显示为黑色
- javascript - Vue.js + Bootstrap-vue,从 Firebase 加载表数据
- sql - 在 PostgreSQL 中删除大量行
- c# - C# Regex.Matches 和可空性问题
- javascript - 推送通知在 Android 上不起作用
- python - 将玩家放置在棋盘上并在 Python 中移动它们
- kubernetes - Kubernetes Cloud Logging 身份验证问题
- html - HTML 电子邮件签名不向下移动
- azure - 不填充 Azure 顾问 REST API 建议扩展属性?
- c# - c# - iTextSharp - 无法访问关闭的流