从 table 中的条目创建列

Creating a column from an entry in a table

我在弄清楚如何在列中输入内容时遇到了一些困难

event_name where match_id = 1

并将该条目应用于按 visit_idpattern_id

分区的新列中的每条记录

这是我拥有的:

| 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  |

我正在尝试获取以下内容:

| 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      |

如果我没解释好,请见谅!感谢您的任何见解。

这可能与您要查找的内容接近:

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

查询选择所有 match_id <> 1 的记录,每条记录都与对应的 match_id = 1 记录相匹配。它使用条件聚合来计算每个Visit_IDSKU.

ImpressionsClicks的数量

您的结果似乎来自这样的查询:

SELECT event_label as Search, product_list_name, SKU,
       SUM( (event_name = 'Impression')::int ) AS Impressions,
       SUM( (event_name = 'Click')::int ) AS Clicks
FROM t
WHERE SKU IS NOT NULL
GROUP BY vent_label, product_list_name, SKU;

您可能想在 GROUP BY 中包含 pattern_id,但它不在结果集中,所以我不确定。