I have table A which has Dates and EMPID eg below
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
I also have Table B which has a date range
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 ...
My table A has missing dates and EMPID.
How can I merge the two tables to have the following table.
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
Thanks in advance. This is being used in a dataset(SQL) in SSRS. P.S. I’m new to coding in SQL environment, My background is in ABAP
Advertisement
Answer
You can cross join
the distinct empid
coming from a
with dates coming from b
, as follows:
select b.date, a.empid from (select distinct empid from a) a cross join b
Or if you are looking to insert
“missing” dates in a
, then you can use the insert ... select
syntax with a not exists
condition:
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)