102 lines
3.8 KiB
SQL
102 lines
3.8 KiB
SQL
/*
|
|
* CardLoadUnload LOAD_UNLOAD extraction for the bytebar tachograph schema.
|
|
*/
|
|
with OrgTree as (
|
|
select org.I_90021_OID
|
|
from dbo.GetOrganisationTree(null, :organisationId, 0, null) org
|
|
where :organisationId is not null
|
|
)
|
|
,
|
|
Base as (
|
|
select
|
|
lu.ID,
|
|
lu.Timestamp as occurred_at,
|
|
lu.OperationType,
|
|
lu.Odo,
|
|
lu.ID_FileLog,
|
|
c.ID as card_id,
|
|
c.ID_Driver as driver_id,
|
|
cn.AlphaCode as driver_card_nation,
|
|
c.CardNumber as driver_card_number,
|
|
gnss.Latitude as latitude,
|
|
gnss.Longitude as longitude,
|
|
v.ID as vehicle_registration_id,
|
|
v.ID_VehicleIdentification as vehicle_identification_id,
|
|
vi.VIN as vehicle_vin,
|
|
vehicleNation.AlphaCode as vehicle_registration_nation,
|
|
v.VRN as vehicle_registration_number,
|
|
coalesce(fl.DownloadDate, fl.OriginalDownloadDate, fl.TStamp, fl.CreationDate) as received_partner_at,
|
|
coalesce(fl.ID, lu.ID_FileLog, lu.ID) as source_package_id_raw,
|
|
coalesce(fl.ID_Card, lu.ID_Card) as source_package_entity_id_raw,
|
|
coalesce(fl.DownloadFrom, lu.Timestamp) as source_package_period_from,
|
|
coalesce(fl.DownloadTo, lu.Timestamp) as source_package_period_to,
|
|
coalesce(fl.CreationDate, fl.TStamp) as source_package_imported_at
|
|
from dbo.CardLoadUnload lu
|
|
join dbo.Card c on c.ID = lu.ID_Card
|
|
left join dbo.Nation cn on cn.ID = c.ID_Nation
|
|
left join dbo.GnssPlace gnss on gnss.ID = lu.ID_GnssPlace
|
|
left join dbo.FileLog fl on fl.ID = lu.ID_FileLog
|
|
outer apply (
|
|
select top 1 used.ID_Vehicle
|
|
from dbo.CardVehiclesUsed used
|
|
where used.ID_Card = lu.ID_Card
|
|
and (used.FirstUse is null or used.FirstUse <= lu.Timestamp)
|
|
and (used.LastUse is null or used.LastUse >= lu.Timestamp)
|
|
order by
|
|
used.FirstUse desc,
|
|
used.ID desc
|
|
) cvu
|
|
left join dbo.Vehicle v on v.ID = cvu.ID_Vehicle
|
|
left join dbo.VehicleIdentification vi on vi.ID = v.ID_VehicleIdentification
|
|
left join dbo.Nation vehicleNation on vehicleNation.ID = v.ID_Nation
|
|
where (:occurredFrom is null or lu.Timestamp >= :occurredFrom)
|
|
and (:occurredTo is null or lu.Timestamp < :occurredTo)
|
|
and (
|
|
:organisationId is null
|
|
or exists (
|
|
select 1
|
|
from dbo.Driver_I_90021 rel
|
|
join OrgTree on OrgTree.I_90021_OID = rel.ID_I_90021
|
|
where rel.ID_Driver = c.ID_Driver
|
|
and rel.GILT_BIS is null
|
|
)
|
|
)
|
|
)
|
|
select
|
|
cast(base.ID as varchar(128)) as source_row_id,
|
|
concat('TACHOGRAPH:CARD_LOAD_UNLOAD:', base.ID) as external_source_event_id,
|
|
|
|
base.occurred_at,
|
|
base.received_partner_at,
|
|
cast(base.Odo as bigint) * 1000 as odometer_m,
|
|
base.latitude,
|
|
base.longitude,
|
|
case base.OperationType
|
|
when 1 then 'LOAD'
|
|
when 2 then 'UNLOAD'
|
|
else 'LOAD_UNLOAD'
|
|
end as operation,
|
|
case base.OperationType
|
|
when 1 then 'LOAD'
|
|
when 2 then 'UNLOAD'
|
|
else 'LOAD_UNLOAD'
|
|
end as event_type,
|
|
|
|
cast(base.driver_id as varchar(128)) as driver_source_entity_id,
|
|
base.driver_card_nation,
|
|
base.driver_card_number,
|
|
|
|
cast(base.vehicle_identification_id as varchar(128)) as vehicle_source_entity_id,
|
|
base.vehicle_vin,
|
|
cast(base.vehicle_registration_id as varchar(128)) as vehicle_registration_source_entity_id,
|
|
base.vehicle_registration_nation,
|
|
base.vehicle_registration_number,
|
|
|
|
'DRIVER_CARD' as source_package_kind,
|
|
cast(base.source_package_id_raw as varchar(128)) as source_package_id,
|
|
cast(base.source_package_entity_id_raw as varchar(128)) as source_package_entity_id,
|
|
base.source_package_period_from,
|
|
base.source_package_period_to,
|
|
base.source_package_imported_at
|
|
from Base base
|