I’ll explain only main point because this article is not to share ad system. The technique is the following.SELECT SPLIT(last_c_info, ',')[6] AS ad_type, COUNT(*) FROM ( SELECT first_p_info, MAX(c_info) AS last_c_info FROM ( SELECT c_info, MIN(p_info) AS first_p_info FROM ( SELECT CONCAT_WS( ',', click_time, click_id, audience_id, -- User ID publisher_id, -- Media Owner(ex. Blogger) ID advertiser_id, -- Shop ID ad_id, ad_type ) c_info FROM clicks WHERE click_time >= '2019-12-01 00:00:00' AND click_time < '2020-01-01 00:00:00' ) c LEFT OUTER JOIN ( SELECT CONCAT_WS( ',', purchase_time, purchase_id, audience_id, advertiser_id, product_id -- Item ID ) p_info FROM purchases WHERE purchase_time >= '2019-12-01 00:00:00' AND purchase_time < '2020-01-02 00:00:00' ) p ON (c.audience_id = p.audience_id) WHERE p.purchase_time IS NOT NULL AND p.purchase_time >= c.click_time AND unix_timestamp(p.purchase_time) < unix_timestamp(c .click_time) + 24 * 3600) -- max duration is 24 hours GROUP BY c_info ) first_purchases ) last_clicks GROUP BY SPLIT(last_c_info, ',')[6];
If you make a group of columns with CONCAT_WS, you can use columns not GROUP BY key easily with SPLIT. MIN/MAX works correctly if you put group by key to the first. Of course, this technique has some demerits.CONCAT_WS( ',', click_time, click_id, audience_id, publisher_id, advertiser_id, ad_id, ad_type ) c_info CONCAT_WS( ',', purchase_time, purchase_id, audience_id, advertiser_id, product_id ) p_info
Streamlit is a …
I bought M5Stac…