-- SET @pbi_datetime = '2018-01-01 10:00:10'

-- UPDATE METADATA
insert INTO `fact_metadata`
(export_time, update_time, `inserted_at`)
SELECT
    DATE_FORMAT(STR_TO_DATE(CAST(max(export_datetime) AS CHAR(19)), '%Y%m%d%H%i%s'), '%m/%d/%Y %H:%i:%s') as export_datetime,
    @pbi_datetime as update_time,
    @db_date_time as inserted_at
from bim_stg_meta
having export_datetime is not null; -- the case when there are no records in bim_stg_meta

-- {{IF_ANALYZE}}fact_metadata{{/IF_ANALYZE}}
