从表中的条目创建列

时间:2019-03-05 15:22:11

标签: sql google-analytics pattern-matching vertica web-analytics

我在弄清楚如何在列中输入条目时遇到一些困难

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      |

很抱歉,如果我没有解释清楚!谢谢您的见解。

2 个答案:

答案 0 :(得分:1)

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

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记录。它使用条件聚合来计算每个ImpressionsClicks的{​​{1}}和Visit_ID的数量。

答案 1 :(得分:0)

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

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;

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