You have two tables tracking marketing touches and downstream conversions. Write SQL to answer the three prompts below. Assume a warehouse like Postgres/BigQuery/Snowflake; months are calendar months in UTC; when multiple touches tie on timestamp, break ties by the highest touch_id; ignore touches strictly after a company's conversion.
Schema:
Sample data (minimal, for reasoning/testing): marketing_touch | touch_id | company_id | touch_timestamp | channel | campaign | | 1 | 100 | 2025-01-05 10:00:00 | Email | E1 | | 2 | 100 | 2025-01-20 12:00:00 | Paid | P1 | | 3 | 100 | 2025-02-01 09:00:00 | Direct | D1 | | 4 | 101 | 2025-02-10 08:00:00 | Paid | P2 | | 5 | 101 | 2025-02-10 08:00:00 | Email | E2 | | 6 | 102 | 2025-02-28 23:59:59 | Referral | R1 | conversion | company_id | conversion_timestamp | | 100 | 2025-02-10 00:00:00 | | 101 | 2025-02-10 08:00:00 | | 103 | 2025-03-01 12:00:00 |
Prompts:
Edge cases to handle in your SQL: (a) multiple touches at the exact same timestamp for a company (pick the one with the highest touch_id); (b) touches after conversion (ignore for #2/#3); (c) companies present in conversion with no prior touches (exclude in #2/#3). Provide performant SQL (CTEs are fine) and briefly explain your tie-break and month-extraction logic.