我在确定如何在列中输入条目时遇到了一些困难
event_name where match_id = 1
并将该条目应用于由visit_id
和pattern_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
记录相匹配。它使用条件聚合来计算每Impressions
和Clicks
的Visit_ID
和SKU
的数量。
您的结果似乎来自这样的查询:
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
,但它不在结果集中,所以我不确定。