-
Notifications
You must be signed in to change notification settings - Fork 0
/
SQLCases.html
1067 lines (705 loc) · 89.5 KB
/
SQLCases.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE HTML>
<html lang="zh-hans" >
<head>
<meta charset="UTF-8">
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>SQL案例 · 大数据技术与算法Checklist</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="description" content="">
<meta name="generator" content="GitBook 3.2.3">
<meta name="author" content="powerlee">
<link rel="stylesheet" href="gitbook/style.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-search-pro/search.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-prism/prism.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-chapter-fold/chapter-fold.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-splitter/splitter.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-back-to-top-button/plugin.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-advanced-emoji/emoji-website.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-insert-logo/plugin.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-pageview-count/plugin.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-flexible-alerts/style.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-katex-new/katex.min.css">
<link rel="stylesheet" href="https://unpkg.com/gitalk/dist/gitalk.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-theme-comscore/test.css">
<link rel="stylesheet" href="styles/website.css">
<meta name="HandheldFriendly" content="true"/>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
<link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">
<link rel="next" href="Kafka.md" />
<link rel="prev" href="SQLKeypoints.html" />
</head>
<body>
<div class="book">
<div class="book-summary">
<div id="book-search-input" role="search">
<input type="text" placeholder="输入并搜索" />
</div>
<nav role="navigation">
<ul class="summary">
<li class="chapter " data-level="1.1" data-path="./">
<a href="./">
Introduction
</a>
</li>
<li class="chapter " data-level="1.2" data-path="GradientDescent.html">
<a href="GradientDescent.html">
梯度下降
</a>
</li>
<li class="chapter " data-level="1.3" data-path="LinearRegression.html">
<a href="LinearRegression.html">
线性回归
</a>
</li>
<li class="chapter " data-level="1.4" data-path="LogisticRegression.html">
<a href="LogisticRegression.html">
逻辑回归
</a>
</li>
<li class="chapter " data-level="1.5" data-path="DecisionTree.html">
<a href="DecisionTree.html">
决策树
</a>
</li>
<li class="chapter " data-level="1.6" data-path="ABTest.html">
<a href="ABTest.html">
A/B实验
</a>
</li>
<li class="chapter " data-level="1.7" data-path="TimeSeries.html">
<a href="TimeSeries.html">
时间序列
</a>
</li>
<li class="chapter " data-level="1.8" data-path="DimensionalModeling.html">
<a href="DimensionalModeling.html">
维度建模
</a>
</li>
<li class="chapter " data-level="1.9" data-path="UserBehaviorsAnalysisPlatform.html">
<a href="UserBehaviorsAnalysisPlatform.html">
用户行为分析平台
</a>
</li>
<li class="chapter " data-level="1.10" data-path="SQLKeypoints.html">
<a href="SQLKeypoints.html">
SQL要点
</a>
</li>
<li class="chapter active" data-level="1.11" data-path="SQLCases.html">
<a href="SQLCases.html">
SQL案例
</a>
</li>
<li class="chapter " data-level="1.12" data-path="Kafka.md">
<span>
Kafka
</a>
</li>
<li class="chapter " data-level="1.13" data-path="Spark.html">
<a href="Spark.html">
Spark
</a>
</li>
<li class="chapter " data-level="1.14" data-path="Flink.md">
<span>
Flink
</a>
</li>
<li class="chapter " data-level="1.15" data-path="ClickHouse.md">
<span>
ClickHouse
</a>
</li>
<li class="chapter " data-level="1.16" data-path="ProbabilityAndStatistics.html">
<a href="ProbabilityAndStatistics.html">
概率论与数理统计
</a>
</li>
<li class="chapter " data-level="1.17" data-path="PythonVirtualEnv.html">
<a href="PythonVirtualEnv.html">
Python虚拟环境
</a>
</li>
<li class="chapter " data-level="1.18" data-path="SparkLocalInstall.html">
<a href="SparkLocalInstall.html">
Spark本地安装部署
</a>
</li>
<li class="chapter " data-level="1.19" data-path="GitCheatSheet.html">
<a href="GitCheatSheet.html">
Git Cheat Sheet
</a>
</li>
<li class="divider"></li>
<li>
<a href="https://www.gitbook.com" target="blank" class="gitbook-link">
本书使用 GitBook 发布
</a>
</li>
</ul>
</nav>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header" role="navigation">
<!-- Title -->
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i>
<a href="." >SQL案例</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<div id="book-search-results">
<div class="search-noresults">
<section class="normal markdown-section">
<p>在互联网行业,会有一些常用的数据指标和常见的数据分析方法包括:事件分析,留存分析,漏斗分析,分布分析,路径分析,LTV分析等。在做数据分析,数据报表时,通常用SQL来计算这些数据指标或者实现这些数据分析方法。</p>
<p>下面模拟一个简单的用户行为事件表。基于这个事件表,用SQL来实现常见的数据指标和数据分析方法。</p>
<pre class="language-"><code class="lang-sql"><span class="token comment"># 用户行为事件表</span>
<span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> Events <span class="token punctuation">(</span>
user_id <span class="token keyword">VARCHAR</span><span class="token punctuation">(</span><span class="token number">20</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">DEFAULT</span> <span class="token string">''</span><span class="token punctuation">,</span>
event_name <span class="token keyword">VARCHAR</span><span class="token punctuation">(</span><span class="token number">20</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">DEFAULT</span> <span class="token string">''</span><span class="token punctuation">,</span>
event_time <span class="token keyword">VARCHAR</span><span class="token punctuation">(</span><span class="token number">20</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">DEFAULT</span> <span class="token string">''</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
<h4 id="事件量,用户量">事件量,用户量</h4>
<p>一段时间内的事件量。在真实的大数据分析场景,因为数据量比较大,通常一定是需要限定时间范围的。而且时间字段通常用来作为分区表的分区字段,比如:按天分区。</p>
<p>一段时间内的用户量就是总事件量按用户做去重处理。</p>
<pre class="language-"><code class="lang-sql"><span class="token comment"># 一段时间内的总事件量</span>
<span class="token keyword">SELECT</span> <span class="token function">count</span><span class="token punctuation">(</span>event_name<span class="token punctuation">)</span>
<span class="token keyword">FROM</span> Events
<span class="token keyword">WHERE</span> event_time <span class="token operator">>=</span> <span class="token string">'2022-01-01 00:00:00'</span>
<span class="token operator">AND</span> event_time <span class="token operator"><</span> <span class="token string">'2022-01-08 00:00:00'</span><span class="token punctuation">;</span>
<span class="token comment"># 一段时间内的总用户量</span>
<span class="token keyword">SELECT</span> <span class="token function">count</span><span class="token punctuation">(</span><span class="token keyword">DISTINCT</span> user_id<span class="token punctuation">)</span>
<span class="token keyword">FROM</span> Events
<span class="token keyword">WHERE</span> event_time <span class="token operator">>=</span> <span class="token string">'2022-01-01 00:00:00'</span>
<span class="token operator">AND</span> event_time <span class="token operator"><</span> <span class="token string">'2022-01-08 00:00:00'</span><span class="token punctuation">;</span>
</code></pre>
<h4 id="dau,uv,pv">DAU,UV,PV</h4>
<p>数据分析中经常需要按天展示DAU,在Web侧通常称为UV。或者按天展示PV,APP侧不太讲PV这个概念。</p>
<pre class="language-"><code class="lang-sql"><span class="token comment"># 一段时间内特定事件的按天DAU</span>
<span class="token keyword">SELECT</span> <span class="token keyword">date</span><span class="token punctuation">(</span>event_time<span class="token punctuation">)</span> <span class="token keyword">AS</span> <span class="token keyword">day</span><span class="token punctuation">,</span> <span class="token function">COUNT</span><span class="token punctuation">(</span><span class="token keyword">DISTINCT</span> user_id<span class="token punctuation">)</span>
<span class="token keyword">FROM</span> Events
<span class="token keyword">WHERE</span> event_name <span class="token operator">=</span> <span class="token string">"play"</span>
<span class="token operator">AND</span> event_time <span class="token operator">>=</span> <span class="token string">"2022-01-01 00:00:00"</span>
<span class="token operator">AND</span> event_time <span class="token operator"><</span> <span class="token string">"2022-01-08 00:00:00"</span>
<span class="token keyword">GROUP</span> <span class="token keyword">BY</span> <span class="token keyword">date</span><span class="token punctuation">(</span>event_time<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment"># 一段时间内特定事件的按天PV或者说事件量</span>
<span class="token keyword">SELECT</span> <span class="token keyword">date</span><span class="token punctuation">(</span>event_time<span class="token punctuation">)</span> <span class="token keyword">AS</span> <span class="token keyword">day</span><span class="token punctuation">,</span> <span class="token function">COUNT</span><span class="token punctuation">(</span>event_name<span class="token punctuation">)</span>
<span class="token keyword">FROM</span> Events
<span class="token keyword">WHERE</span> event_name <span class="token operator">=</span> <span class="token string">"play"</span>
<span class="token operator">AND</span> event_time <span class="token operator">>=</span> <span class="token string">"2022-01-01 00:00:00"</span>
<span class="token operator">AND</span> event_time <span class="token operator"><</span> <span class="token string">"2022-01-08 00:00:00"</span>
<span class="token keyword">GROUP</span> <span class="token keyword">BY</span> <span class="token keyword">date</span><span class="token punctuation">(</span>event_time<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
<p>以上SQL直接通过<code>GROUP BY</code>按天展示数据的一个问题是:如果某一天没有数据,那么在SQL查询的结果中就不会出现该天的数据。但因为最终的数据是要拿来做展示的,所以如果某天没有数据,也需要<strong>增加该日期的数据行,用0来补充</strong>。具体到项目中,如果用来展示数据的 BI 能够兼容缺失日期的数据当然最好。如果 BI 无法兼容,用于请求数据库或者数仓的后端服务也可以兼容该逻辑。如果后端服务也不愿意兼容,最后只能还是利用SQL实现,不过SQL实现这个需求并不简单且不直观。</p>
<p>SQL实现该需求需要分两步进行:</p>
<ol>
<li>生成按行的连续日期。根据所用数据库或者数仓的不同,应该有不同的实现方式。</li>
<li>按行的连续日期和原数据做<code>LEFT JOIN</code>。利用<code>LEFT JOIN</code>时右表某行不存在则用<code>NULL</code>替代的特性,将<code>NULL</code>替换为0即可。</li>
</ol>
<pre class="language-"><code class="lang-sql"><span class="token comment"># 给定时间范围,生成按行的连续日期</span>
<span class="token keyword">SELECT</span>
<span class="token variable">@i</span> :<span class="token operator">=</span> <span class="token variable">@i</span> <span class="token operator">+</span> <span class="token number">1</span> <span class="token keyword">AS</span> <span class="token string">'NO'</span><span class="token punctuation">,</span>
<span class="token keyword">DATE</span><span class="token punctuation">(</span>
DATE_ADD<span class="token punctuation">(</span><span class="token string">'2022-01-01'</span><span class="token punctuation">,</span><span class="token keyword">INTERVAL</span> <span class="token variable">@i</span> <span class="token keyword">DAY</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">as</span> <span class="token keyword">date</span> <span class="token comment"># 开始时间</span>
<span class="token keyword">FROM</span>
Events<span class="token punctuation">,</span> <span class="token comment"># 随便给表,需要保证表的行数比需要的天数多</span>
<span class="token punctuation">(</span><span class="token keyword">SELECT</span> <span class="token variable">@i</span> :<span class="token operator">=</span> <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span> t <span class="token comment"># 设置初始值为-1的变量i</span>
<span class="token keyword">WHERE</span>
<span class="token variable">@i</span> <span class="token operator"><</span> DATEDIFF<span class="token punctuation">(</span><span class="token string">'2022-01-10'</span><span class="token punctuation">,</span><span class="token string">'2022-01-01'</span><span class="token punctuation">)</span> <span class="token comment"># 结束时间,开始时间</span>
</code></pre>
<p>以时间范围内的按行连续日期表为基础,结合之前查询DAU的数据,进行联表查询,获得每个日期的DAU数据。观察一下数据查询结果中,2022-01-08~2022-01-10三天的DAU数据均为0。</p>
<pre class="language-"><code class="lang-sql"><span class="token keyword">SELECT</span>
t1<span class="token punctuation">.</span><span class="token keyword">day</span><span class="token punctuation">,</span> ifnull<span class="token punctuation">(</span>t2<span class="token punctuation">.</span>DAU<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token keyword">as</span> DAU
<span class="token keyword">FROM</span>
<span class="token punctuation">(</span><span class="token keyword">SELECT</span>
<span class="token variable">@i</span> :<span class="token operator">=</span> <span class="token variable">@i</span> <span class="token operator">+</span> <span class="token number">1</span> <span class="token keyword">AS</span> <span class="token keyword">NO</span><span class="token punctuation">,</span>
<span class="token keyword">DATE</span><span class="token punctuation">(</span>
DATE_ADD<span class="token punctuation">(</span> <span class="token string">'2022-01-01'</span><span class="token punctuation">,</span> <span class="token keyword">INTERVAL</span> <span class="token variable">@i</span> <span class="token keyword">DAY</span> <span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">AS</span> <span class="token keyword">day</span> <span class="token comment"># 开始时间</span>
<span class="token keyword">FROM</span>
Events<span class="token punctuation">,</span>
<span class="token punctuation">(</span> <span class="token keyword">SELECT</span> <span class="token variable">@i</span> :<span class="token operator">=</span> <span class="token operator">-</span> <span class="token number">1</span> <span class="token punctuation">)</span> t
<span class="token keyword">WHERE</span>
<span class="token variable">@i</span> <span class="token operator"><</span> DATEDIFF<span class="token punctuation">(</span> <span class="token string">'2022-01-10'</span><span class="token punctuation">,</span> <span class="token string">'2022-01-01'</span> <span class="token punctuation">)</span><span class="token punctuation">)</span> t1 <span class="token comment"># 结束时间,开始时间</span>
<span class="token keyword">LEFT</span> <span class="token keyword">JOIN</span>
<span class="token punctuation">(</span><span class="token keyword">SELECT</span>
<span class="token keyword">date</span><span class="token punctuation">(</span>event_time<span class="token punctuation">)</span> <span class="token keyword">AS</span> <span class="token keyword">day</span><span class="token punctuation">,</span> <span class="token function">COUNT</span><span class="token punctuation">(</span><span class="token keyword">DISTINCT</span> user_id<span class="token punctuation">)</span> <span class="token keyword">as</span> DAU
<span class="token keyword">FROM</span> Events
<span class="token keyword">WHERE</span>
event_time <span class="token operator">>=</span> <span class="token string">"2022-01-01 00:00:00"</span>
<span class="token operator">AND</span> event_time <span class="token operator"><</span> <span class="token string">"2022-01-08 00:00:00"</span>
<span class="token keyword">GROUP</span> <span class="token keyword">BY</span> <span class="token keyword">date</span><span class="token punctuation">(</span>event_time<span class="token punctuation">)</span><span class="token punctuation">)</span> t2
<span class="token keyword">ON</span> t1<span class="token punctuation">.</span><span class="token keyword">day</span> <span class="token operator">=</span> t2<span class="token punctuation">.</span><span class="token keyword">day</span>
</code></pre>
<table>
<thead>
<tr>
<th style="text-align:left">day</th>
<th style="text-align:left">DAU</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">2022-01-01</td>
<td style="text-align:left">3</td>
</tr>
<tr>
<td style="text-align:left">2022-01-02</td>
<td style="text-align:left">3</td>
</tr>
<tr>
<td style="text-align:left">2022-01-03</td>
<td style="text-align:left">3</td>
</tr>
<tr>
<td style="text-align:left">2022-01-04</td>
<td style="text-align:left">1</td>
</tr>
<tr>
<td style="text-align:left">2022-01-05</td>
<td style="text-align:left">2</td>
</tr>
<tr>
<td style="text-align:left">2022-01-06</td>
<td style="text-align:left">2</td>
</tr>
<tr>
<td style="text-align:left">2022-01-07</td>
<td style="text-align:left">2</td>
</tr>
<tr>
<td style="text-align:left">2022-01-08</td>
<td style="text-align:left">0</td>
</tr>
<tr>
<td style="text-align:left">2022-01-09</td>
<td style="text-align:left">0</td>
</tr>
<tr>
<td style="text-align:left">2022-01-10</td>
<td style="text-align:left">0</td>
</tr>
</tbody>
</table>
<h4 id="留存分析,留存率">留存分析,留存率</h4>
<p>A日期的活跃用户在B日期的留存率计算可以分为以下步骤:</p>
<ol>
<li>计算A日期活跃的用户ID</li>
<li>计算A日期活跃的用户哪些在B日期也活跃过</li>
<li>将以上两步的计算结果做JOIN,算出留存率</li>
</ol>
<p>对于复杂的SQL,<strong>可读性</strong>通常很差。可以利用<code>WITH AS</code>来相对提升可读性。t1用来计算起始天活跃的用户 ,t2用来计算留存天活跃的用户。用t1和t2做<code>LEFT JOIN</code>后,t1中有t2没有的会是NULL,t2有t1没有的不会出现在结果中。</p>
<pre class="language-"><code class="lang-sql"><span class="token keyword">WITH</span> t1 <span class="token keyword">AS</span> <span class="token punctuation">(</span> <span class="token comment"># 计算起始天的活跃用户</span>
<span class="token keyword">SELECT</span> <span class="token keyword">DISTINCT</span> user_id
<span class="token keyword">FROM</span> Events
<span class="token keyword">WHERE</span> <span class="token keyword">date</span><span class="token punctuation">(</span>event_time<span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token string">'2022-01-01'</span>
<span class="token punctuation">)</span><span class="token punctuation">,</span>
t2 <span class="token keyword">AS</span> <span class="token punctuation">(</span> <span class="token comment"># 计算留存天的活跃用户</span>
<span class="token keyword">SELECT</span> <span class="token keyword">DISTINCT</span> user_id
<span class="token keyword">FROM</span> Events
<span class="token keyword">WHERE</span> <span class="token keyword">date</span><span class="token punctuation">(</span>event_time<span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token string">'2022-01-02'</span>
<span class="token punctuation">)</span>
<span class="token comment"># LEFT JOIN后t2中没有留存的会是NULL,COUNT不会包含NULL</span>
<span class="token keyword">SELECT</span> <span class="token function">COUNT</span><span class="token punctuation">(</span>t2<span class="token punctuation">.</span>user_id<span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token function">COUNT</span><span class="token punctuation">(</span>t1<span class="token punctuation">.</span>user_id<span class="token punctuation">)</span> <span class="token keyword">AS</span> retention
<span class="token keyword">FROM</span> t1
<span class="token keyword">LEFT</span> <span class="token keyword">JOIN</span> t2 <span class="token keyword">ON</span> t1<span class="token punctuation">.</span>user_id <span class="token operator">=</span> t2<span class="token punctuation">.</span>user_id<span class="token punctuation">;</span>
</code></pre>
<p>以上SQL只是求得了某一日期的次日留存率。但在数据分析中,通常我们希望看到的是如下的留存曲线。留存曲线描绘了特定日期之后一段时间每天的留存数据。我们需要用SQL来实现绘制该留存曲线所需要的数据。</p>
<p><img src="pics/SQLCases/image-20221205230409609.png" alt="image-20221205230409609"></p>
<p>首先需要同时对用户ID和日期做<code>GROUP BY</code>,其次用<code>LEFT JOIN</code>求时间差。</p>
<p>以下SQL展示了如何求得时间差diff这一中间结果:</p>
<pre class="language-"><code class="lang-sql"><span class="token keyword">WITH</span> t1 <span class="token keyword">AS</span> <span class="token punctuation">(</span>
<span class="token keyword">SELECT</span> user_id<span class="token punctuation">,</span> <span class="token keyword">date</span><span class="token punctuation">(</span>event_time<span class="token punctuation">)</span> <span class="token keyword">AS</span> <span class="token keyword">day</span>
<span class="token keyword">FROM</span> Events
<span class="token keyword">WHERE</span> <span class="token keyword">date</span><span class="token punctuation">(</span>event_time<span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token string">'2022-01-01'</span>
<span class="token keyword">GROUP</span> <span class="token keyword">BY</span> user_id<span class="token punctuation">,</span> <span class="token keyword">date</span><span class="token punctuation">(</span>event_time<span class="token punctuation">)</span>
<span class="token punctuation">)</span><span class="token punctuation">,</span>
t2 <span class="token keyword">AS</span> <span class="token punctuation">(</span>
<span class="token keyword">SELECT</span> user_id<span class="token punctuation">,</span> <span class="token keyword">date</span><span class="token punctuation">(</span>event_time<span class="token punctuation">)</span> <span class="token keyword">AS</span> <span class="token keyword">day</span>
<span class="token keyword">FROM</span> Events
<span class="token keyword">WHERE</span> event_time <span class="token operator">>=</span> <span class="token string">"2022-01-01 00:00:00"</span>
<span class="token operator">AND</span> event_time <span class="token operator"><</span> <span class="token string">"2022-01-08 00:00:00"</span>
<span class="token keyword">GROUP</span> <span class="token keyword">BY</span> user_id<span class="token punctuation">,</span> <span class="token keyword">date</span><span class="token punctuation">(</span>event_time<span class="token punctuation">)</span>
<span class="token punctuation">)</span>
<span class="token keyword">SELECT</span> t1<span class="token punctuation">.</span>user_id<span class="token punctuation">,</span> t1<span class="token punctuation">.</span><span class="token keyword">day</span> <span class="token keyword">AS</span> start_day<span class="token punctuation">,</span> t2<span class="token punctuation">.</span><span class="token keyword">day</span> <span class="token keyword">AS</span> retent_day<span class="token punctuation">,</span> datediff<span class="token punctuation">(</span>t2<span class="token punctuation">.</span><span class="token keyword">day</span><span class="token punctuation">,</span> t1<span class="token punctuation">.</span><span class="token keyword">day</span><span class="token punctuation">)</span> <span class="token keyword">AS</span> diff
<span class="token keyword">FROM</span> t1
<span class="token keyword">LEFT</span> <span class="token keyword">JOIN</span> t2 <span class="token keyword">ON</span> t1<span class="token punctuation">.</span>user_id <span class="token operator">=</span> t2<span class="token punctuation">.</span>user_id<span class="token punctuation">;</span>
</code></pre>
<table>
<thead>
<tr>
<th style="text-align:left">user_id</th>
<th style="text-align:left">start_day</th>
<th style="text-align:left">retent_day</th>
<th style="text-align:left">diff</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">01</td>
<td style="text-align:left">2022-01-01</td>
<td style="text-align:left">2022-01-07</td>
<td style="text-align:left">6</td>
</tr>
<tr>
<td style="text-align:left">01</td>
<td style="text-align:left">2022-01-01</td>
<td style="text-align:left">2022-01-05</td>
<td style="text-align:left">4</td>
</tr>
<tr>
<td style="text-align:left">01</td>
<td style="text-align:left">2022-01-01</td>
<td style="text-align:left">2022-01-03</td>
<td style="text-align:left">2</td>
</tr>
<tr>
<td style="text-align:left">01</td>
<td style="text-align:left">2022-01-01</td>
<td style="text-align:left">2022-01-02</td>
<td style="text-align:left">1</td>
</tr>
<tr>
<td style="text-align:left">01</td>
<td style="text-align:left">2022-01-01</td>
<td style="text-align:left">2022-01-01</td>
<td style="text-align:left">0</td>
</tr>
<tr>
<td style="text-align:left">03</td>
<td style="text-align:left">2022-01-01</td>
<td style="text-align:left">2022-01-07</td>
<td style="text-align:left">6</td>
</tr>
<tr>
<td style="text-align:left">03</td>
<td style="text-align:left">2022-01-01</td>
<td style="text-align:left">2022-01-06</td>
<td style="text-align:left">5</td>
</tr>
<tr>
<td style="text-align:left">03</td>
<td style="text-align:left">2022-01-01</td>
<td style="text-align:left">2022-01-05</td>
<td style="text-align:left">4</td>
</tr>
<tr>
<td style="text-align:left">03</td>
<td style="text-align:left">2022-01-01</td>
<td style="text-align:left">2022-01-01</td>
<td style="text-align:left">0</td>
</tr>
<tr>
<td style="text-align:left">04</td>
<td style="text-align:left">2022-01-01</td>
<td style="text-align:left">2022-01-03</td>
<td style="text-align:left">2</td>
</tr>
<tr>
<td style="text-align:left">04</td>
<td style="text-align:left">2022-01-01</td>
<td style="text-align:left">2022-01-02</td>
<td style="text-align:left">1</td>
</tr>
<tr>
<td style="text-align:left">04</td>
<td style="text-align:left">2022-01-01</td>
<td style="text-align:left">2022-01-01</td>
<td style="text-align:left">0</td>
</tr>
</tbody>
</table>
<p>以上结果中retent_day表示该用户有留存的日期,diff就是留存日期到起始日期的差值,1就是次日留存,2就2日留存,3就是3日留存,以此类推。要求留存人数就只需要按照diff进行<code>GROUP BY</code>求<code>COUNT</code>。如果要求留存率就还要增加一步,这里就不赘述了。而且这里同样存在某天留存人数为0,则结果中不存在该天的问题,也不多说。</p>
<pre class="language-"><code class="lang-sql"><span class="token keyword">SELECT</span> datediff<span class="token punctuation">(</span>t2<span class="token punctuation">.</span><span class="token keyword">day</span><span class="token punctuation">,</span> t1<span class="token punctuation">.</span><span class="token keyword">day</span><span class="token punctuation">)</span> <span class="token keyword">AS</span> diff<span class="token punctuation">,</span> <span class="token function">COUNT</span><span class="token punctuation">(</span><span class="token operator">*</span><span class="token punctuation">)</span>
<span class="token keyword">FROM</span> t1
<span class="token keyword">LEFT</span> <span class="token keyword">JOIN</span> t2 <span class="token keyword">ON</span> t1<span class="token punctuation">.</span>user_id <span class="token operator">=</span> t2<span class="token punctuation">.</span>user_id
<span class="token keyword">GROUP</span> <span class="token keyword">BY</span> diff
<span class="token keyword">ORDER</span> <span class="token keyword">BY</span> diff<span class="token punctuation">;</span>
</code></pre>
<table>
<thead>
<tr>
<th style="text-align:left">diff</th>
<th style="text-align:left">count(*)</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">0</td>
<td style="text-align:left">3</td>
</tr>
<tr>
<td style="text-align:left">1</td>
<td style="text-align:left">2</td>
</tr>
<tr>
<td style="text-align:left">2</td>
<td style="text-align:left">2</td>
</tr>
<tr>
<td style="text-align:left">4</td>
<td style="text-align:left">2</td>
</tr>
<tr>
<td style="text-align:left">5</td>
<td style="text-align:left">1</td>
</tr>
<tr>
<td style="text-align:left">6</td>
<td style="text-align:left">2</td>
</tr>
</tbody>
</table>
<h4 id="漏斗分析,转化率">漏斗分析,转化率</h4>
<p>漏斗分析中的转化率其实和留存率有相似之处。留存率可以看成是相对于日期的转化率,而漏斗分析中的转化率可以看成是相对于具体事件的转化率。但是通常在漏斗分析时,我们会对转化的时间做出限制,比如转化必须发生在1小时之内。</p>
<p>选取Events表中的login、play、like三个事件做转化分析。用Events表和自身进行<code>JOIN</code>,筛选其中间隔小于1小时的两个不相同的事件。每个转化,同一个用户只记录一次。</p>
<pre class="language-"><code class="lang-sql"><span class="token keyword">SELECT</span> event1<span class="token punctuation">.</span>event_name <span class="token keyword">AS</span> event1<span class="token punctuation">,</span>
event2<span class="token punctuation">.</span>event_name <span class="token keyword">AS</span> event2<span class="token punctuation">,</span>
<span class="token function">COUNT</span><span class="token punctuation">(</span><span class="token keyword">DISTINCT</span> event1<span class="token punctuation">.</span>user_id<span class="token punctuation">)</span> <span class="token keyword">AS</span> num_conversions
<span class="token keyword">FROM</span> Events event1
<span class="token keyword">INNER</span> <span class="token keyword">JOIN</span> Events event2
<span class="token keyword">ON</span> event1<span class="token punctuation">.</span>event_name <span class="token operator"><></span> event2<span class="token punctuation">.</span>event_name
<span class="token operator">AND</span> event1<span class="token punctuation">.</span>user_id <span class="token operator">=</span> event2<span class="token punctuation">.</span>user_id
<span class="token operator">AND</span> TIMESTAMPDIFF<span class="token punctuation">(</span><span class="token keyword">SECOND</span><span class="token punctuation">,</span> event1<span class="token punctuation">.</span>event_time<span class="token punctuation">,</span> event2<span class="token punctuation">.</span>event_time<span class="token punctuation">)</span> <span class="token operator"><</span> <span class="token number">3600</span>
<span class="token operator">AND</span> TIMESTAMPDIFF<span class="token punctuation">(</span><span class="token keyword">SECOND</span><span class="token punctuation">,</span> event1<span class="token punctuation">.</span>event_time<span class="token punctuation">,</span> event2<span class="token punctuation">.</span>event_time<span class="token punctuation">)</span> <span class="token operator">></span> <span class="token number">0</span>
<span class="token keyword">WHERE</span> event1<span class="token punctuation">.</span>event_name <span class="token operator">IN</span> <span class="token punctuation">(</span><span class="token string">'login'</span><span class="token punctuation">,</span> <span class="token string">'play'</span><span class="token punctuation">,</span> <span class="token string">'like'</span><span class="token punctuation">)</span>
<span class="token operator">AND</span> event2<span class="token punctuation">.</span>event_name <span class="token operator">IN</span> <span class="token punctuation">(</span><span class="token string">'play'</span><span class="token punctuation">,</span> <span class="token string">'like'</span><span class="token punctuation">)</span>
<span class="token keyword">GROUP</span> <span class="token keyword">BY</span> event1<span class="token punctuation">.</span>event_name<span class="token punctuation">,</span> event2<span class="token punctuation">.</span>event_name
</code></pre>
<table>
<thead>
<tr>
<th style="text-align:left">event1</th>
<th style="text-align:left">event2</th>
<th style="text-align:left">num_conversions</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">login</td>
<td style="text-align:left">like</td>
<td style="text-align:left">3</td>
</tr>
<tr>
<td style="text-align:left">login</td>
<td style="text-align:left">play</td>
<td style="text-align:left">4</td>
</tr>
<tr>
<td style="text-align:left">play</td>
<td style="text-align:left">like</td>
<td style="text-align:left">3</td>
</tr>
</tbody>
</table>
<p>以上可以看出login到like发生了3人次的转化,login到play发生了4次转化,play到like发生了3次转化。如果最终要求出转化率,还要先把起始事件的人数求出。对以上SQL做一点改动。</p>
<pre class="language-"><code class="lang-sql"><span class="token keyword">SELECT</span>
event1<span class="token punctuation">.</span>event_name <span class="token keyword">AS</span> event1<span class="token punctuation">,</span>
event2<span class="token punctuation">.</span>event_name <span class="token keyword">AS</span> event2<span class="token punctuation">,</span>
<span class="token function">COUNT</span><span class="token punctuation">(</span><span class="token keyword">DISTINCT</span> event1<span class="token punctuation">.</span>user_id<span class="token punctuation">)</span> <span class="token keyword">AS</span> num_conversions<span class="token punctuation">,</span>
CAST<span class="token punctuation">(</span><span class="token function">COUNT</span><span class="token punctuation">(</span><span class="token keyword">DISTINCT</span> event1<span class="token punctuation">.</span>user_id<span class="token punctuation">)</span> <span class="token keyword">AS</span> <span class="token keyword">FLOAT</span><span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token punctuation">(</span><span class="token keyword">SELECT</span> <span class="token function">COUNT</span><span class="token punctuation">(</span><span class="token keyword">DISTINCT</span> user_id<span class="token punctuation">)</span> <span class="token keyword">FROM</span> Events <span class="token keyword">AS</span> e <span class="token keyword">WHERE</span> e<span class="token punctuation">.</span>event_name <span class="token operator">=</span> event1<span class="token punctuation">.</span>event_name<span class="token punctuation">)</span> <span class="token keyword">AS</span> conversion_rate
<span class="token keyword">FROM</span> Events <span class="token keyword">AS</span> event1
<span class="token keyword">INNER</span> <span class="token keyword">JOIN</span> Events <span class="token keyword">AS</span> event2
<span class="token keyword">ON</span> event1<span class="token punctuation">.</span>event_name <span class="token operator"><></span> event2<span class="token punctuation">.</span>event_name
<span class="token operator">AND</span> event1<span class="token punctuation">.</span>user_id <span class="token operator">=</span> event2<span class="token punctuation">.</span>user_id
<span class="token operator">AND</span> TIMESTAMPDIFF<span class="token punctuation">(</span><span class="token keyword">SECOND</span><span class="token punctuation">,</span> event1<span class="token punctuation">.</span>event_time<span class="token punctuation">,</span> event2<span class="token punctuation">.</span>event_time<span class="token punctuation">)</span> <span class="token operator"><</span> <span class="token number">3600</span> <span class="token operator">and</span> TIMESTAMPDIFF<span class="token punctuation">(</span><span class="token keyword">SECOND</span><span class="token punctuation">,</span> event1<span class="token punctuation">.</span>event_time<span class="token punctuation">,</span> event2<span class="token punctuation">.</span>event_time<span class="token punctuation">)</span> <span class="token operator">></span> <span class="token number">0</span>
<span class="token keyword">where</span> event1<span class="token punctuation">.</span>event_name <span class="token operator">in</span> <span class="token punctuation">(</span><span class="token string">'login'</span><span class="token punctuation">,</span> <span class="token string">'play'</span><span class="token punctuation">,</span> <span class="token string">'like'</span><span class="token punctuation">)</span> <span class="token operator">and</span> event2<span class="token punctuation">.</span>event_name <span class="token operator">in</span> <span class="token punctuation">(</span><span class="token string">'play'</span><span class="token punctuation">,</span> <span class="token string">'like'</span><span class="token punctuation">)</span>
<span class="token keyword">GROUP</span> <span class="token keyword">BY</span> event1<span class="token punctuation">.</span>event_name<span class="token punctuation">,</span> event2<span class="token punctuation">.</span>event_name
</code></pre>
<table>
<thead>
<tr>
<th style="text-align:left">event1</th>
<th style="text-align:left">event2</th>
<th style="text-align:left">num_conversions</th>
<th style="text-align:left">conversion_rate</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">login</td>
<td style="text-align:left">like</td>
<td style="text-align:left">3</td>
<td style="text-align:left">0.75</td>
</tr>
<tr>
<td style="text-align:left">login</td>
<td style="text-align:left">play</td>
<td style="text-align:left">4</td>
<td style="text-align:left">1</td>
</tr>
<tr>
<td style="text-align:left">play</td>
<td style="text-align:left">like</td>
<td style="text-align:left">3</td>
<td style="text-align:left">0.75</td>
</tr>
</tbody>
</table>
<h4 id="用户分群">用户分群</h4>
<p>在实际数据分析中,我们通常需要按照一定的用户属性或者行为特征对用户进行分群分析。一道经典的SQL面试题:求连续登录3天的用户ID,就是一个典型的用户分群案例。用户连续登录N天可以反应用户某种程度的活跃。用户连续N次做某件事情也会经常被应用于运营活动中。</p>
<p>以下就尝试用SQL求解连续登录3天的用户ID:</p>
<ol>
<li>将事件时间event_time转换为事件天event_day。</li>
<li>利用窗口函数,按用户ID分组,按照事件天event_day排序。给每一天打上一个排序后的行号rn。</li>
<li>将事件天event_day减去对应的行号rn得到一个flag_day。如果是连续登录的,则flag_day应该是同一天。</li>
<li>按照用户ID和flag_day分组,如果同一天出现大于等3,则表示该用户从start_day开始连续登录了3天及以上。</li>
</ol>
<pre class="language-"><code class="lang-sql"><span class="token keyword">WITH</span> t1 <span class="token keyword">AS</span> <span class="token punctuation">(</span> <span class="token comment"># 将事件时间event_time转换为事件天event_day</span>
<span class="token keyword">SELECT</span> user_id<span class="token punctuation">,</span> <span class="token keyword">date</span><span class="token punctuation">(</span>event_time<span class="token punctuation">)</span> <span class="token keyword">AS</span> event_day
<span class="token keyword">FROM</span> Events
<span class="token keyword">GROUP</span> <span class="token keyword">BY</span> user_id<span class="token punctuation">,</span> <span class="token keyword">date</span><span class="token punctuation">(</span>event_time<span class="token punctuation">)</span>
<span class="token punctuation">)</span><span class="token punctuation">,</span>
t2 <span class="token keyword">AS</span> <span class="token punctuation">(</span> <span class="token comment"># 利用窗口函数,按用户ID分组,按照事件天event_day排序。给每一天打上一个排序后的行号rn</span>
<span class="token keyword">SELECT</span> user_id<span class="token punctuation">,</span> event_day<span class="token punctuation">,</span> row_number<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">OVER</span> <span class="token punctuation">(</span><span class="token keyword">PARTITION</span> <span class="token keyword">BY</span> user_id <span class="token keyword">ORDER</span> <span class="token keyword">BY</span> event_day<span class="token punctuation">)</span> <span class="token keyword">AS</span> rn
<span class="token keyword">FROM</span> t1
<span class="token punctuation">)</span><span class="token punctuation">,</span>
t3 <span class="token keyword">AS</span> <span class="token punctuation">(</span> <span class="token comment"># 将事件天event_day减去对应的行号rn得到一个flag_day。如果是连续登录的,则flag_day应该是同一天</span>
<span class="token keyword">SELECT</span> <span class="token operator">*</span><span class="token punctuation">,</span> date_sub<span class="token punctuation">(</span>event_day<span class="token punctuation">,</span> <span class="token keyword">INTERVAL</span> rn <span class="token keyword">DAY</span><span class="token punctuation">)</span> <span class="token keyword">AS</span> flag_day
<span class="token keyword">FROM</span> t2
<span class="token punctuation">)</span>
<span class="token comment"># 按照用户ID和flag_day分组,如果同一天出现大于等3,则表示该用户从start_day开始连续登录了3天及以上</span>
<span class="token keyword">SELECT</span> date_add<span class="token punctuation">(</span>flag_day<span class="token punctuation">,</span> <span class="token keyword">INTERVAL</span> <span class="token number">1</span> <span class="token keyword">DAY</span><span class="token punctuation">)</span> <span class="token keyword">AS</span> start_day<span class="token punctuation">,</span> user_id
<span class="token keyword">FROM</span> t3
<span class="token keyword">GROUP</span> <span class="token keyword">BY</span> user_id<span class="token punctuation">,</span> flag_day
<span class="token keyword">HAVING</span> <span class="token function">COUNT</span><span class="token punctuation">(</span>user_id<span class="token punctuation">)</span> <span class="token operator">>=</span> <span class="token number">3</span><span class="token punctuation">;</span>
</code></pre>
<table>
<thead>
<tr>
<th style="text-align:left">start_day</th>
<th style="text-align:left">user_id</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">2022-01-01</td>
<td style="text-align:left">01</td>
</tr>
<tr>
<td style="text-align:left">2022-01-02</td>
<td style="text-align:left">02</td>
</tr>
<tr>
<td style="text-align:left">2022-01-04</td>
<td style="text-align:left">03</td>
</tr>
<tr>
<td style="text-align:left">2022-01-01</td>
<td style="text-align:left">04</td>
</tr>
</tbody>
</table>
<h3 id="附">附</h3>
<p>以上内容所需的数据库表结构和数据库表数据如下:</p>
<pre class="language-"><code class="lang-sql"><span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> Events<span class="token punctuation">(</span>
user_id <span class="token keyword">VARCHAR</span><span class="token punctuation">(</span><span class="token number">20</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">DEFAULT</span> <span class="token string">''</span><span class="token punctuation">,</span>
event_name <span class="token keyword">VARCHAR</span><span class="token punctuation">(</span><span class="token number">20</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">DEFAULT</span> <span class="token string">''</span><span class="token punctuation">,</span>
event_time <span class="token keyword">VARCHAR</span><span class="token punctuation">(</span><span class="token number">20</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">DEFAULT</span> <span class="token string">''</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">insert</span> <span class="token keyword">into</span> Events <span class="token keyword">values</span><span class="token punctuation">(</span><span class="token string">'01'</span><span class="token punctuation">,</span> <span class="token string">'login'</span><span class="token punctuation">,</span> <span class="token string">'2022-01-01 11:50:31'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">insert</span> <span class="token keyword">into</span> Events <span class="token keyword">values</span><span class="token punctuation">(</span><span class="token string">'01'</span><span class="token punctuation">,</span> <span class="token string">'click'</span><span class="token punctuation">,</span> <span class="token string">'2022-01-01 11:51:10'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">insert</span> <span class="token keyword">into</span> Events <span class="token keyword">values</span><span class="token punctuation">(</span><span class="token string">'01'</span><span class="token punctuation">,</span> <span class="token string">'view'</span><span class="token punctuation">,</span> <span class="token string">'2022-01-01 11:55:59'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">insert</span> <span class="token keyword">into</span> Events <span class="token keyword">values</span><span class="token punctuation">(</span><span class="token string">'01'</span><span class="token punctuation">,</span> <span class="token string">'play'</span><span class="token punctuation">,</span> <span class="token string">'2022-01-01 12:01:03'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">insert</span> <span class="token keyword">into</span> Events <span class="token keyword">values</span><span class="token punctuation">(</span><span class="token string">'01'</span><span class="token punctuation">,</span> <span class="token string">'like'</span><span class="token punctuation">,</span> <span class="token string">'2022-01-01 12:06:42'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">insert</span> <span class="token keyword">into</span> Events <span class="token keyword">values</span><span class="token punctuation">(</span><span class="token string">'01'</span><span class="token punctuation">,</span> <span class="token string">'login'</span><span class="token punctuation">,</span> <span class="token string">'2022-01-02 15:50:31'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">insert</span> <span class="token keyword">into</span> Events <span class="token keyword">values</span><span class="token punctuation">(</span><span class="token string">'01'</span><span class="token punctuation">,</span> <span class="token string">'play'</span><span class="token punctuation">,</span> <span class="token string">'2022-01-02 16:01:03'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">insert</span> <span class="token keyword">into</span> Events <span class="token keyword">values</span><span class="token punctuation">(</span><span class="token string">'01'</span><span class="token punctuation">,</span> <span class="token string">'like'</span><span class="token punctuation">,</span> <span class="token string">'2022-01-02 16:06:42'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">insert</span> <span class="token keyword">into</span> Events <span class="token keyword">values</span><span class="token punctuation">(</span><span class="token string">'01'</span><span class="token punctuation">,</span> <span class="token string">'login'</span><span class="token punctuation">,</span> <span class="token string">'2022-01-03 21:50:31'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">insert</span> <span class="token keyword">into</span> Events <span class="token keyword">values</span><span class="token punctuation">(</span><span class="token string">'01'</span><span class="token punctuation">,</span> <span class="token string">'play'</span><span class="token punctuation">,</span> <span class="token string">'2022-01-03 22:01:03'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">insert</span> <span class="token keyword">into</span> Events <span class="token keyword">values</span><span class="token punctuation">(</span><span class="token string">'01'</span><span class="token punctuation">,</span> <span class="token string">'login'</span><span class="token punctuation">,</span> <span class="token string">'2022-01-05 08:50:31'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">insert</span> <span class="token keyword">into</span> Events <span class="token keyword">values</span><span class="token punctuation">(</span><span class="token string">'01'</span><span class="token punctuation">,</span> <span class="token string">'view'</span><span class="token punctuation">,</span> <span class="token string">'2022-01-05 08:55:59'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">insert</span> <span class="token keyword">into</span> Events <span class="token keyword">values</span><span class="token punctuation">(</span><span class="token string">'01'</span><span class="token punctuation">,</span> <span class="token string">'play'</span><span class="token punctuation">,</span> <span class="token string">'2022-01-05 09:01:03'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">insert</span> <span class="token keyword">into</span> Events <span class="token keyword">values</span><span class="token punctuation">(</span><span class="token string">'01'</span><span class="token punctuation">,</span> <span class="token string">'like'</span><span class="token punctuation">,</span> <span class="token string">'2022-01-05 09:06:42'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">insert</span> <span class="token keyword">into</span> Events <span class="token keyword">values</span><span class="token punctuation">(</span><span class="token string">'01'</span><span class="token punctuation">,</span> <span class="token string">'logout'</span><span class="token punctuation">,</span> <span class="token string">'2022-01-05 09:51:10'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">insert</span> <span class="token keyword">into</span> Events <span class="token keyword">values</span><span class="token punctuation">(</span><span class="token string">'01'</span><span class="token punctuation">,</span> <span class="token string">'login'</span><span class="token punctuation">,</span> <span class="token string">'2022-01-07 22:50:31'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">insert</span> <span class="token keyword">into</span> Events <span class="token keyword">values</span><span class="token punctuation">(</span><span class="token string">'02'</span><span class="token punctuation">,</span> <span class="token string">'login'</span><span class="token punctuation">,</span> <span class="token string">'2022-01-02 09:10:31'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">insert</span> <span class="token keyword">into</span> Events <span class="token keyword">values</span><span class="token punctuation">(</span><span class="token string">'02'</span><span class="token punctuation">,</span> <span class="token string">'play'</span><span class="token punctuation">,</span> <span class="token string">'2022-01-02 09:15:03'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">insert</span> <span class="token keyword">into</span> Events <span class="token keyword">values</span><span class="token punctuation">(</span><span class="token string">'02'</span><span class="token punctuation">,</span> <span class="token string">'like'</span><span class="token punctuation">,</span> <span class="token string">'2022-01-02 09:20:55'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">insert</span> <span class="token keyword">into</span> Events <span class="token keyword">values</span><span class="token punctuation">(</span><span class="token string">'02'</span><span class="token punctuation">,</span> <span class="token string">'login'</span><span class="token punctuation">,</span> <span class="token string">'2022-01-03 13:15:31'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">insert</span> <span class="token keyword">into</span> Events <span class="token keyword">values</span><span class="token punctuation">(</span><span class="token string">'02'</span><span class="token punctuation">,</span> <span class="token string">'play'</span><span class="token punctuation">,</span> <span class="token string">'2022-01-03 13:20:03'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">insert</span> <span class="token keyword">into</span> Events <span class="token keyword">values</span><span class="token punctuation">(</span><span class="token string">'02'</span><span class="token punctuation">,</span> <span class="token string">'logout'</span><span class="token punctuation">,</span> <span class="token string">'2022-01-03 13:25:55'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">insert</span> <span class="token keyword">into</span> Events <span class="token keyword">values</span><span class="token punctuation">(</span><span class="token string">'02'</span><span class="token punctuation">,</span> <span class="token string">'login'</span><span class="token punctuation">,</span> <span class="token string">'2022-01-04 01:50:31'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">insert</span> <span class="token keyword">into</span> Events <span class="token keyword">values</span><span class="token punctuation">(</span><span class="token string">'02'</span><span class="token punctuation">,</span> <span class="token string">'click'</span><span class="token punctuation">,</span> <span class="token string">'2022-01-04 02:51:10'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">insert</span> <span class="token keyword">into</span> Events <span class="token keyword">values</span><span class="token punctuation">(</span><span class="token string">'02'</span><span class="token punctuation">,</span> <span class="token string">'view'</span><span class="token punctuation">,</span> <span class="token string">'2022-01-04 02:55:59'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">insert</span> <span class="token keyword">into</span> Events <span class="token keyword">values</span><span class="token punctuation">(</span><span class="token string">'02'</span><span class="token punctuation">,</span> <span class="token string">'play'</span><span class="token punctuation">,</span> <span class="token string">'2022-01-04 03:01:03'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">insert</span> <span class="token keyword">into</span> Events <span class="token keyword">values</span><span class="token punctuation">(</span><span class="token string">'02'</span><span class="token punctuation">,</span> <span class="token string">'like'</span><span class="token punctuation">,</span> <span class="token string">'2022-01-04 03:06:42'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">insert</span> <span class="token keyword">into</span> Events <span class="token keyword">values</span><span class="token punctuation">(</span><span class="token string">'02'</span><span class="token punctuation">,</span> <span class="token string">'login'</span><span class="token punctuation">,</span> <span class="token string">'2022-01-06 18:50:31'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">insert</span> <span class="token keyword">into</span> Events <span class="token keyword">values</span><span class="token punctuation">(</span><span class="token string">'02'</span><span class="token punctuation">,</span> <span class="token string">'view'</span><span class="token punctuation">,</span> <span class="token string">'2022-01-06 18:55:01'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">insert</span> <span class="token keyword">into</span> Events <span class="token keyword">values</span><span class="token punctuation">(</span><span class="token string">'02'</span><span class="token punctuation">,</span> <span class="token string">'like'</span><span class="token punctuation">,</span> <span class="token string">'2022-01-06 18:59:42'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">insert</span> <span class="token keyword">into</span> Events <span class="token keyword">values</span><span class="token punctuation">(</span><span class="token string">'03'</span><span class="token punctuation">,</span> <span class="token string">'login'</span><span class="token punctuation">,</span> <span class="token string">'2022-01-01 16:23:07'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">insert</span> <span class="token keyword">into</span> Events <span class="token keyword">values</span><span class="token punctuation">(</span><span class="token string">'03'</span><span class="token punctuation">,</span> <span class="token string">'login'</span><span class="token punctuation">,</span> <span class="token string">'2022-01-05 23:50:31'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">insert</span> <span class="token keyword">into</span> Events <span class="token keyword">values</span><span class="token punctuation">(</span><span class="token string">'03'</span><span class="token punctuation">,</span> <span class="token string">'play'</span><span class="token punctuation">,</span> <span class="token string">'2022-01-06 00:01:03'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">insert</span> <span class="token keyword">into</span> Events <span class="token keyword">values</span><span class="token punctuation">(</span><span class="token string">'03'</span><span class="token punctuation">,</span> <span class="token string">'like'</span><span class="token punctuation">,</span> <span class="token string">'2022-01-06 00:06:42'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">insert</span> <span class="token keyword">into</span> Events <span class="token keyword">values</span><span class="token punctuation">(</span><span class="token string">'03'</span><span class="token punctuation">,</span> <span class="token string">'login'</span><span class="token punctuation">,</span> <span class="token string">'2022-01-07 13:08:31'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">insert</span> <span class="token keyword">into</span> Events <span class="token keyword">values</span><span class="token punctuation">(</span><span class="token string">'03'</span><span class="token punctuation">,</span> <span class="token string">'click'</span><span class="token punctuation">,</span> <span class="token string">'2022-01-07 13:10:10'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">insert</span> <span class="token keyword">into</span> Events <span class="token keyword">values</span><span class="token punctuation">(</span><span class="token string">'03'</span><span class="token punctuation">,</span> <span class="token string">'view'</span><span class="token punctuation">,</span> <span class="token string">'2022-01-07 13:47:59'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">insert</span> <span class="token keyword">into</span> Events <span class="token keyword">values</span><span class="token punctuation">(</span><span class="token string">'04'</span><span class="token punctuation">,</span> <span class="token string">'login'</span><span class="token punctuation">,</span> <span class="token string">'2022-01-01 19:43:31'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">insert</span> <span class="token keyword">into</span> Events <span class="token keyword">values</span><span class="token punctuation">(</span><span class="token string">'04'</span><span class="token punctuation">,</span> <span class="token string">'play'</span><span class="token punctuation">,</span> <span class="token string">'2022-01-01 19:47:03'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">insert</span> <span class="token keyword">into</span> Events <span class="token keyword">values</span><span class="token punctuation">(</span><span class="token string">'04'</span><span class="token punctuation">,</span> <span class="token string">'logout'</span><span class="token punctuation">,</span> <span class="token string">'2022-01-01 19:53:55'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">insert</span> <span class="token keyword">into</span> Events <span class="token keyword">values</span><span class="token punctuation">(</span><span class="token string">'04'</span><span class="token punctuation">,</span> <span class="token string">'login'</span><span class="token punctuation">,</span> <span class="token string">'2022-01-02 18:43:31'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">insert</span> <span class="token keyword">into</span> Events <span class="token keyword">values</span><span class="token punctuation">(</span><span class="token string">'04'</span><span class="token punctuation">,</span> <span class="token string">'play'</span><span class="token punctuation">,</span> <span class="token string">'2022-01-02 18:47:03'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">insert</span> <span class="token keyword">into</span> Events <span class="token keyword">values</span><span class="token punctuation">(</span><span class="token string">'04'</span><span class="token punctuation">,</span> <span class="token string">'logout'</span><span class="token punctuation">,</span> <span class="token string">'2022-01-02 18:53:55'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">insert</span> <span class="token keyword">into</span> Events <span class="token keyword">values</span><span class="token punctuation">(</span><span class="token string">'04'</span><span class="token punctuation">,</span> <span class="token string">'login'</span><span class="token punctuation">,</span> <span class="token string">'2022-01-03 17:43:31'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">insert</span> <span class="token keyword">into</span> Events <span class="token keyword">values</span><span class="token punctuation">(</span><span class="token string">'04'</span><span class="token punctuation">,</span> <span class="token string">'play'</span><span class="token punctuation">,</span> <span class="token string">'2022-01-03 17:47:03'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">insert</span> <span class="token keyword">into</span> Events <span class="token keyword">values</span><span class="token punctuation">(</span><span class="token string">'04'</span><span class="token punctuation">,</span> <span class="token string">'logout'</span><span class="token punctuation">,</span> <span class="token string">'2022-01-03 17:53:55'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
<div id="gitalk-container"></div>
</section>
</div>
<div class="search-results">
<div class="has-results">
<h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
<ul class="search-results-list"></ul>
</div>
<div class="no-results">
<h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
</div>
</div>
</div>
</div>
</div>
</div>
<a href="SQLKeypoints.html" class="navigation navigation-prev " aria-label="Previous page: SQL要点">
<i class="fa fa-angle-left"></i>
</a>
<a href="Kafka.md" class="navigation navigation-next " aria-label="Next page: Kafka">
<i class="fa fa-angle-right"></i>
</a>
</div>
<script>
var gitbook = gitbook || [];
gitbook.push(function() {
gitbook.page.hasChanged({"page":{"title":"SQL案例","level":"1.11","depth":1,"next":{"title":"Kafka","level":"1.12","depth":1,"path":"Kafka.md","ref":"Kafka.md","articles":[]},"previous":{"title":"SQL要点","level":"1.10","depth":1,"path":"SQLKeypoints.md","ref":"SQLKeypoints.md","articles":[]},"dir":"ltr"},"config":{"plugins":["-lunr","-search","search-pro","-code","-codeline","-highlight","prism","expandable-chapters","chapter-fold","splitter","back-to-top-button","advanced-emoji","hide-element","insert-logo","custom-favicon","pageview-count","-sharing","sharing-plus","flexible-alerts","katex-new","mygitalk","theme-comscore","livereload"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"chapter-fold":{},"prism":{},"styles":{"website":"website.css","ebook":"ebook.css","pdf":"pdf.css","mobi":"ebook.css","epub":"ebook.css"},"katex-new":{},"livereload":{},"splitter":{},"search-pro":{},"sharing-plus":{"qq":false,"all":["facebook","google","twitter","instapaper","linkedin","pocket","stumbleupon"],"douban":false,"facebook":true,"weibo":false,"instapaper":false,"whatsapp":false,"hatenaBookmark":false,"twitter":true,"messenger":false,"line":false,"vk":false,"pocket":true,"google":false,"viber":false,"stumbleupon":false,"qzone":false,"linkedin":false},"hide-element":{"elements":[".gitbook-link"]},"fontsettings":{"theme":"white","family":"sans","size":2},"favicon":"pics/logo.ico","theme-comscore":{},"back-to-top-button":{},"pageview-count":{},"custom-favicon":{},"flexible-alerts":{"style":"callout","note":{"label":"Note","icon":"fa fa-info-circle","className":"info"},"tip":{"label":"Tip","icon":"fa fa-lightbulb-o","className":"tip"},"warning":{"label":"Warning","icon":"fa fa-exclamation-triangle","className":"warning"},"danger":{"label":"Attention","icon":"fa fa-ban","className":"danger"}},"mygitalk":{"flipMoveOptions":{},"clientID":"c623ce11cb38fa65e5f0","number":-1,"perPage":10,"proxy":"https://cors-anywhere.azm.workers.dev/https://github.com/login/oauth/access_token","admin":["powerAmore"],"createIssueManually":false,"distractionFreeMode":false,"repo":"DataSciChecklist","owner":"powerAmore","enableHotKey":true,"clientSecret":"09a234dcf2ca63822ea9858aa00245147624e61d","pagerDirection":"last","labels":["Gitalk"]},"advanced-emoji":{"embedEmojis":false},"sharing":{"facebook":true,"twitter":true,"all":["douban","facebook","google","twitter","weibo","qq","whatsapp"]},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"insert-logo":{"url":"pics/logo.jpeg","style":"background: none; max-height: 30px; min-height: 30px"},"expandable-chapters":{}},"theme":"default","author":"powerlee","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"大数据技术与算法Checklist","language":"zh-hans","links":{},"gitbook":"*","description":"大数据技术与算法Checklist"},"file":{"path":"SQLCases.md","mtime":"2023-02-04T03:48:00.261Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2023-02-04T03:57:06.860Z"},"basePath":".","book":{"language":""}});
});
</script>
</div>
<script src="gitbook/gitbook.js"></script>
<script src="gitbook/theme.js"></script>
<script src="gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>