我有一张看起来像这样的桌子
visit_id event_list seq_num search_term
1 search 1 sample
1 click 3 null
2 search 1 sample
3 search 1 sample
3 click 2 null
我想在一天结束时
search_term searches visits product_clicks
sample 3 3 1
请记住,只有在搜索后直接点击它。
现在,我可以进行正确的访问和搜索,直到增加产品点击次数为止。我通过单击表重新加入表,然后在visit_id
上加入。当我加入此连接时,我的搜索量猛增了很多,我不知道为什么。
这是我的代码
select a.search_term
,count(a.visit_id) AS searches
,count(distinct a.visit_id) visits
,count(case when cast(b.seq_num as int) = cast(a.seq_num as int) + 1 then a.visit_id end) prod_click
from Table a join (select visit_id
,seq_num
,event_list
from Table
where event_list = 'click'
and seq_num != 'null') b
on b.visit_id = a.visit_id
where event_list = 'search'
group by 1
编辑: 查询
select a.search_term
,count(distinct a.visit_id) as visits
,count(distinct case when a.search_term != 'null' then a.visit_id end) as searches
,count(distinct case when b.seq_num = a.seq_num + 1 then 1 end) as prod_click
from T a, T b
可以满足我的需求,但是我正在使用的表非常庞大,因此无法使用。有谁有办法使它变得更好?
答案 0 :(得分:1)
您可以在第二行代码中进行“案例分析”
count(distinct case when event_list = "click" then a.visit_id else 0) AS product_clicks,
count(distinct case when event_list = "searches" then a.visit_id else 0) AS searches,
我猜您不需要加入表格。谢谢!
答案 1 :(得分:0)
您可以尝试使用条件汇总函数来实现。
SUM
和CASE WHEN
CREATE TABLE T(
visit_id int,
event_list varchar(50),
seq_num int,
search_term varchar(50)
);
insert into t values (1,'search',1,'sample');
insert into t values (1,'click',2,'sample');
insert into t values (2,'search',1,'sample');
insert into t values (3,'search',1,'sample');
insert into t values (3,'click',2,'sample');
查询1 :
select search_term,
COUNT(distinct visit_id) searches,
SUM(CASE WHEN event_list = 'search' THEN 1 ELSE 0 END) visits,
SUM(CASE WHEN event_list = 'click' THEN 1 ELSE 0 END) product_clicks
FROM T
GROUP BY search_term
Results :
| search_term | searches | visits | product_clicks |
|-------------|----------|--------|----------------|
| sample | 3 | 3 | 2 |
编辑
您可以尝试将LEAD
函数用于下一个状态,并比较seq_num
到ROW_NUMBER
是连续的
CREATE TABLE T(
visit_id int,
event_list varchar(50),
seq_num int,
search_term varchar(50)
);
insert into t values (1,'search',1,'sample');
insert into t values (1,'click', 3,null);
insert into t values (2,'search',1,'sample');
insert into t values (3,'search',1,'sample');
insert into t values (3,'click', 2,null);
查询1 :
with cte as (
select *,
(ROW_NUMBER() OVER(order by visit_id) - seq_num) rn
FROM T
), cte1 as (
select *,LEAD(event_list) OVER(PARTITION BY visit_id,rn order by visit_id) nextEvent
from cte
)
SELECT * FROM (
SELECT search_term,
COUNT(distinct visit_id) searches,
SUM(CASE WHEN event_list = 'search' THEN 1 ELSE 0 END) visits,
COUNT(CASE WHEN event_list = 'search' and nextEvent = 'click' then 1 end) product_clicks
FROM cte1
group by search_term
)t
where search_term is not null
Results :
| search_term | searches | visits | product_clicks |
|-------------|----------|--------|----------------|
| sample | 3 | 3 | 1 |