Search code examples
sqlgoogle-analyticspattern-matchingverticaweb-analytics

Creating a column from an entry in a table


I'm having some difficulty figuring out how to take an entry in a column

event_name where match_id = 1

and applying that entry to every record in a new column partitioned by visit_id and pattern_id

Here's what I have:

| Visit_ID           | event_label  | product_list_name | event_name | match_id | pattern_id | SKU    |
|--------------------|--------------|-------------------|------------|----------|------------|--------|
| 154892456600012589 | California   | banner-101-s      | Search     | 1        | 1          | (null) |
| 154892456600012589 | sendData     | banner-101-s      | Impression | 2        | 1          | 10572  |
| 154892456600012589 | sendData     | banner-101-s      | Impression | 3        | 1          | 10573  |
| 154892456600012589 | sendData     | banner-101-s      | Impression | 4        | 1          | 10574  |
| 154892456600012589 | sendData     | banner-101-s      | Impression | 5        | 1          | 47589  |
| 154892456600012589 | sendData     | banner-101-s      | Impression | 6        | 1          | 84756  |
| 256493157982168884 | Nevada       | banner-109-s      | Search     | 1        | 2          | (null) |
| 256493157982168884 | sendData     | banner-109-s      | Impression | 2        | 2          | 58798  |
| 256493157982168884 | sendData     | banner-109-s      | Impression | 3        | 2          | 58799  |
| 256493157982168884 | sendData     | banner-109-s      | Impression | 4        | 2          | 10546  |
| 256493157982168884 | banner-109-s | banner-109-s      | Click      | 5        | 2          | 58798  |

I'm trying to get the following:

| Search     | Product_List_Name | SKU   | Impressions | Clicks |
|------------|-------------------|-------|-------------|--------|
| California | banner-101-s      | 10572 | 1           | 0      |
| California | banner-101-s      | 10573 | 1           | 0      |
| California | banner-101-s      | 10574 | 1           | 0      |
| California | banner-101-s      | 47589 | 1           | 0      |
| California | banner-101-s      | 84756 | 1           | 0      |
| Nevada     | banner-109-s      | 58798 | 1           | 1      |
| Nevada     | banner-109-s      | 58799 | 1           | 0      |
| Nevada     | banner-109-s      | 10546 | 1           | 0      |

Apologies if I haven't explained well! Thank you for any insights.


Solution

  • This might be close to what you are looking for:

    SELECT t2.event_label AS Search, t1.product_list_name, t1.SKU,
           COUNT(CASE WHEN t1.event_name = 'Impression' THEN 1 END) AS Impression,
           COUNT(CASE WHEN t1.event_name = 'Click' THEN 1 END) AS Click
    FROM mytable AS t1
    INNER JOIN mytable AS t2 ON t1.Visit_ID = t2.Visit_ID AND t2.match_id = 1
    WHERE t1.match_id > 1
    GROUP BY t1.Visit_ID, t1.SKU, t2.event_label, t1.product_list_name
    

    The query picks all records with match_id <> 1 matching each one of them with the corresponding match_id = 1 record. It uses conditional aggregation to count the number of Impressions and Clicks per Visit_ID and SKU.