Skip to content

Commit cde347f

Browse files
committed
漏斗分析
1 parent d8e8d51 commit cde347f

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

flink_user_kafka/flink.sql

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,5 +202,73 @@ FROM (
202202
--│ 2020-08-01 │ 4 │ 0.25 │ 0.25 │ 0 │ 0.5 │ 0.75 │
203203
--└────────────┴──────────────────┴──────┴──────┴─────┴──────┴──────┘
204204

205+
-- 漏斗分析
206+
-- 分析"2020-01-02"这天 路径为“浏览->点击->下单->支付”的转化情况。
207+
208+
CREATE TABLE test.action
209+
(
210+
`uid` Int32,
211+
`event_type` String,
212+
`time` DateTime
213+
)ENGINE = MergeTree()
214+
PARTITION BY uid
215+
ORDER BY xxHash32(uid)
216+
SAMPLE BY xxHash32(uid)
217+
SETTINGS index_granularity = 8192
218+
219+
INSERT INTO action
220+
VALUES (1,'浏览','2020-01-02 11:00:00'),
221+
(1,'点击','2020-01-02 11:10:00'),
222+
(1,'下单','2020-01-02 11:20:00'),
223+
(1,'支付','2020-01-02 11:30:00'),
224+
(2,'下单','2020-01-02 11:00:00'),
225+
(2,'支付','2020-01-02 11:10:00'),
226+
(1,'浏览','2020-01-02 11:00:00'),
227+
(3,'浏览','2020-01-02 11:20:00'),
228+
(3,'点击','2020-01-02 12:00:00'),
229+
(4,'浏览','2020-01-02 11:50:00'),
230+
(4,'点击','2020-01-02 12:00:00'),
231+
(5,'浏览','2020-01-02 11:50:00'),
232+
(5,'点击','2020-01-02 12:00:00'),
233+
(5,'下单','2020-01-02 11:10:00'),
234+
(6,'浏览','2020-01-02 11:50:00'),
235+
(6,'点击','2020-01-02 12:00:00'),
236+
(6,'下单','2020-01-02 12:10:00');
237+
238+
SELECT
239+
level_index,
240+
count(1)
241+
FROM
242+
(
243+
SELECT
244+
user_id,
245+
arrayWithConstant(level, 1) AS levels,
246+
arrayJoin(arrayEnumerate(levels)) AS level_index
247+
FROM
248+
(
249+
SELECT
250+
user_id,
251+
windowFunnel(1800)(time, event_type = '浏览', event_type = '点击', event_type = '下单', event_type = '支付') AS level
252+
FROM
253+
(
254+
SELECT
255+
time,
256+
event_type,
257+
uid AS user_id
258+
FROM test.action
259+
WHERE toDate(time) = '2020-01-02'
260+
)
261+
GROUP BY user_id
262+
)
263+
)
264+
GROUP BY level_index
265+
ORDER BY level_index ASC
266+
267+
--┌─level_index─┬─count(1)─┐
268+
--│ 1 │ 5 │
269+
--│ 2 │ 4 │
270+
--│ 3 │ 2 │
271+
--│ 4 │ 1 │
272+
--└─────────────┴──────────┘
205273

206274

0 commit comments

Comments
 (0)