-- create a login period for those users that don't have any yet
-- only fill the beggining date which is
-- a) if the identification has a creation date it's creation date
-- b) if the identification doesn't have a creation date it's begin_date_date_time
-- there were no users at the creation time of this query that had neither
-- creation_date or BEGIN_DATE_DATE_TIME
insert into LOGIN_PERIOD (begin_date,key_login)
select IDENT.begin_date,IDENT.id_internal from 
(select I1.CREATION_ENTITY_DATE as begin_date, I1.id_internal 
from IDENTIFICATION as I1
where I1.CREATION_ENTITY_DATE is not null
UNION
select date_format(I2.BEGIN_DATE_DATE_TIME,'%Y-%m-%e') as begin_date, I2.id_internal 
from IDENTIFICATION as I2
where I2.CREATION_ENTITY_DATE is null) as IDENT
left outer join LOGIN_PERIOD as LP on LP.KEY_LOGIN=IDENT.id_internal
where LP.ID_INTERNAL is null;
