INSERT INTO `summary`.`lead_channels` (
`clientId`,
`sessionId`,
`visitNumber`,
`trafficSource_source`,
`trafficSource_medium`,
`trafficSource_campaign`,
`trafficSource_keyword`,
`trafficSource_adContent`,
`trafficSource_adCost`,
`trafficSource_attributedAdCost`,
`date_session`,
`newVisits`,
`device_deviceCategory`,
`lead_channel`,
`leadid`,
`lead_source`,
`lead_status`,
`lead_date`,
`DKT_City`,
`DKT_Source`,
`DKT_Channel`,
`DKT_Campaign`,
`DKT_Content`,
`DKT_keyword`,
`DKT_Call_Page`,
`clinic`
) SELECT
s.clientId,
s.sessionId,
s.visitNumber,
s.trafficSource.source AS trafficSource_source,
s.trafficSource.medium AS trafficSource_medium,
s.trafficSource.campaign AS trafficSource_campaign,
s.trafficSource.keyword AS trafficSource_keyword,
s.trafficSource.adContent AS trafficSource_adContent,
s.trafficSource.adCost AS trafficSource_adCost,
s.trafficSource.attributedAdCost AS trafficSource_attributedAdCost,
s.date AS date_session,
s.newVisits,
s.device.deviceCategory AS device_deviceCategory,
if (
s.trafficSource.source = b.DKT_Source AND
s.trafficSource.medium = b.DKT_Channel AND
s.trafficSource.campaign = b.DKT_Campaign AND
s.date < b.Date , 1, 0
) AS lead_channel,
b.ID AS leadid,
b.source AS lead_source,
b.status AS lead_status,
b.Date AS lead_date,
b.DKT_City,
b.DKT_Source,
b.DKT_Channel,
b.DKT_Campaign,
b.DKT_Content,
b.DKT_looking_for AS DKT_keyword,
b.DKT_Call_Page,
b.clinic AS clinic
FROM `Bitrix24`.`lead` AS b
LEFT JOIN `OWOXBI_Streaming_Krasnodar`.`owoxbi_sessions_*` AS s
ON b.DKT_client_cid = s.clientId
WHERE s.clientId IS NOT NULL
`clientId`,
`sessionId`,
`visitNumber`,
`trafficSource_source`,
`trafficSource_medium`,
`trafficSource_campaign`,
`trafficSource_keyword`,
`trafficSource_adContent`,
`trafficSource_adCost`,
`trafficSource_attributedAdCost`,
`date_session`,
`newVisits`,
`device_deviceCategory`,
`lead_channel`,
`leadid`,
`lead_source`,
`lead_status`,
`lead_date`,
`DKT_City`,
`DKT_Source`,
`DKT_Channel`,
`DKT_Campaign`,
`DKT_Content`,
`DKT_keyword`,
`DKT_Call_Page`,
`clinic`
) SELECT
s.clientId,
s.sessionId,
s.visitNumber,
s.trafficSource.source AS trafficSource_source,
s.trafficSource.medium AS trafficSource_medium,
s.trafficSource.campaign AS trafficSource_campaign,
s.trafficSource.keyword AS trafficSource_keyword,
s.trafficSource.adContent AS trafficSource_adContent,
s.trafficSource.adCost AS trafficSource_adCost,
s.trafficSource.attributedAdCost AS trafficSource_attributedAdCost,
s.date AS date_session,
s.newVisits,
s.device.deviceCategory AS device_deviceCategory,
if (
s.trafficSource.source = b.DKT_Source AND
s.trafficSource.medium = b.DKT_Channel AND
s.trafficSource.campaign = b.DKT_Campaign AND
s.date < b.Date , 1, 0
) AS lead_channel,
b.ID AS leadid,
b.source AS lead_source,
b.status AS lead_status,
b.Date AS lead_date,
b.DKT_City,
b.DKT_Source,
b.DKT_Channel,
b.DKT_Campaign,
b.DKT_Content,
b.DKT_looking_for AS DKT_keyword,
b.DKT_Call_Page,
b.clinic AS clinic
FROM `Bitrix24`.`lead` AS b
LEFT JOIN `OWOXBI_Streaming_Krasnodar`.`owoxbi_sessions_*` AS s
ON b.DKT_client_cid = s.clientId
WHERE s.clientId IS NOT NULL