-
Notifications
You must be signed in to change notification settings - Fork 0
/
atom.xml
564 lines (379 loc) · 138 KB
/
atom.xml
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
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>周卫东的博客</title>
<link href="/atom.xml" rel="self"/>
<link href="http://zhouweidong.ml/"/>
<updated>2021-04-29T14:49:05.379Z</updated>
<id>http://zhouweidong.ml/</id>
<author>
<name>周卫东</name>
</author>
<generator uri="http://hexo.io/">Hexo</generator>
<entry>
<title>二分查找</title>
<link href="http://zhouweidong.ml/2021/04/29/%E4%BA%8C%E5%88%86%E6%9F%A5%E6%89%BE/"/>
<id>http://zhouweidong.ml/2021/04/29/二分查找/</id>
<published>2021-04-29T14:05:25.000Z</published>
<updated>2021-04-29T14:49:05.379Z</updated>
<content type="html"><![CDATA[<p> 二分查找是最基本算法,它的复杂度是 <span class="math inline">\(O (log_{2} n)\)</span>。</p><a id="more"></a><h1 id="基本思想">基本思想</h1><p> 二分查找的基本问题是:给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target ,写一个函数搜索 nums 中的 target,如果目标值存在返回下标,否则返回 -1。</p><p> 二分查找一般由三个主要部分组成:</p><ul><li>*** 预处理 *** —— 如果集合未排序,则进行排序。</li><li>*** 二分查找 *** —— 使用循环或递归在每次比较后将查找空间划分为两半。</li><li>*** 后处理 *** —— 在剩余空间中确定可行的候选者。</li></ul><h1 id="模板-1">模板 1</h1><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">binarySearch</span><span class="params">(nums, target)</span>:</span></span><br><span class="line"> <span class="string">"""</span></span><br><span class="line"><span class="string"> :type nums: List [int]</span></span><br><span class="line"><span class="string"> :type target: int</span></span><br><span class="line"><span class="string"> :rtype: int</span></span><br><span class="line"><span class="string">"""</span></span><br><span class="line"> <span class="keyword">if</span> len (nums) == <span class="number">0</span>:</span><br><span class="line"> <span class="keyword">return</span> <span class="number">-1</span></span><br><span class="line"> left, right = <span class="number">0</span>, len (nums) - <span class="number">1</span></span><br><span class="line"> <span class="comment"># End Condition: left > right</span></span><br><span class="line"> <span class="keyword">while</span> left <= right:</span><br><span class="line"> mid = (left + right) // <span class="number">2</span></span><br><span class="line"> <span class="keyword">if</span> nums [mid] == target:</span><br><span class="line"> <span class="keyword">return</span> mid</span><br><span class="line"> <span class="keyword">elif</span> nums [mid] < target:</span><br><span class="line"> left = mid + <span class="number">1</span></span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> right = mid - <span class="number">1</span></span><br><span class="line"> <span class="keyword">return</span> <span class="number">-1</span></span><br></pre></td></tr></table></figure><p> 实现 <code>int sqrt (int x)</code> 函数: 计算并返回 <em>x</em> 的平方根,其中 <em>x</em> 是非负整数。</p><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kity.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kityminder.core.min.js"></script><script defer="true" type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.js"></script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.css">]]></content>
<summary type="html">
<p> 二分查找是最基本算法,它的复杂度是 <span class="math inline">\(O (log_{2} n)\)</span>。</p>
</summary>
<category term="算法" scheme="http://zhouweidong.ml/categories/%E7%AE%97%E6%B3%95/"/>
<category term="算法" scheme="http://zhouweidong.ml/tags/%E7%AE%97%E6%B3%95/"/>
</entry>
<entry>
<title>数据挖掘面试题</title>
<link href="http://zhouweidong.ml/2021/01/17/%E6%95%B0%E6%8D%AE%E6%8C%96%E6%8E%98%E9%9D%A2%E8%AF%95%E9%A2%98/"/>
<id>http://zhouweidong.ml/2021/01/17/数据挖掘面试题/</id>
<published>2021-01-17T12:59:57.000Z</published>
<updated>2021-01-18T11:15:03.730Z</updated>
<content type="html"><![CDATA[<div id="hbe-security"> <div class="hbe-input-container"> <input type="password" class="hbe-form-control" id="pass" placeholder="Please enter the password to read the blog." /> <label for="pass">Please enter the password to read the blog.</label> <div class="bottom-line"></div> </div></div><div id="decryptionError" style="display: none;">Incorrect Password!</div><div id="noContentError" style="display: none;">No content to display!</div><div id="encrypt-blog" style="display:none">U2FsdGVkX19DBr+bmM70MVVg0wUmf4csjHNDG2jhvPyc9qBWCZkNpIghxUK4Gyd8Gk3P0QBKIF8W3fqLSAhG39nWjP3Br03zb8tA49b0HDj7c1CIc0owwY3dmGJrJPL9YQnI5axw5ykIcQicu6Xfgehs4LeicK5hKefCpIph5G80a+UsWAyW/R3NddzhwicByycaHfqD519cEHi3U3VDr6xN5o6FHvRrb8yewqCXdSpKsn6hmrlYmaT+fw7RktElBmW0tKiv12GDbiBvQMfKsTCPUxzhLBA5spzNROBItvNMOE/lOuclQwgwD2QWWVFZW0KTIZ330iXpADLXa6zYERxu/1UPC9ugmHugatYVVs7NvfLYkjoK3ERMx25X9G1CWPpaE/3vI+iTymXjvo2Cher0FtvzC3zvDVirrGpvgZeOLc+y9v37UgD1x+sJJsX/7zRCSkTOVkEZMRCwmfIVt2XUae+P6O1KGmIEBYqzcfFcIQb4w4fls6SniyUFKkrQ/4f27X7JPWY1JGW2BwPdFU21lhvpEpSWQYnV9LPRHINcefS8B4l8FPIVtA9ZNp44YFfjufQp4ncrcwK+ZxhS0nFCo8ruD1q9wrkVdPt0Pg12Fw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Y/y3T4w6yJJAHa+jJhEPwfLH9zHI63g2hN73xvRZaG6BZgw48VIXQPtmXhdMoI+f//1oiNPELsO4B5k8anvIyXPem13Kd0/FNudwNh3Gmpb/BIie6fFlOk3Jlw6wPFZNywO9b85vOSD4FYqUkiXMlG0z4kJMfRiDPk6+Fbz2ItyBzfMCyJxunSqfUdLOZ9cWTkboLsj59ta3N0WCYi</div><script src="/lib/crypto-js.js"></script><script src="/lib/blog-encrypt.js"></script><link href="/css/blog-encrypt.css" rel="stylesheet" type="text/css"><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kity.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kityminder.core.min.js"></script><script defer="true" type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.js"></script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.css">]]></content>
<summary type="html">
The article has been encrypted, please enter your password to view.<br>
</summary>
</entry>
<entry>
<title>面试题:你的平均薪水是多少?</title>
<link href="http://zhouweidong.ml/2021/01/12/%E9%9D%A2%E8%AF%95%E9%A2%98%EF%BC%9A%E4%BD%A0%E7%9A%84%E5%B9%B3%E5%9D%87%E8%96%AA%E6%B0%B4%E6%98%AF%E5%A4%9A%E5%B0%91%EF%BC%9F/"/>
<id>http://zhouweidong.ml/2021/01/12/面试题:你的平均薪水是多少?/</id>
<published>2021-01-12T13:38:28.000Z</published>
<updated>2021-01-12T14:51:55.617Z</updated>
<content type="html"><![CDATA[<p> 面试题:查询出每个部门出去最高和最低薪水后的平均薪水,并保留整数。</p><a id="more"></a><table><thead><tr class="header"><th style="text-align: center;">雇员编号</th><th style="text-align: center;">部门编号</th><th style="text-align: center;">薪水</th></tr></thead><tbody><tr class="odd"><td style="text-align: center;">10001</td><td style="text-align: center;">1</td><td style="text-align: center;">60117</td></tr><tr class="even"><td style="text-align: center;">10002</td><td style="text-align: center;">2</td><td style="text-align: center;">92102</td></tr><tr class="odd"><td style="text-align: center;">10003</td><td style="text-align: center;">2</td><td style="text-align: center;">86074</td></tr><tr class="even"><td style="text-align: center;">10004</td><td style="text-align: center;">1</td><td style="text-align: center;">66596</td></tr><tr class="odd"><td style="text-align: center;">10005</td><td style="text-align: center;">1</td><td style="text-align: center;">66961</td></tr><tr class="even"><td style="text-align: center;">10006</td><td style="text-align: center;">2</td><td style="text-align: center;">81046</td></tr><tr class="odd"><td style="text-align: center;">10007</td><td style="text-align: center;">2</td><td style="text-align: center;">94333</td></tr><tr class="even"><td style="text-align: center;">10008</td><td style="text-align: center;">1</td><td style="text-align: center;">75286</td></tr><tr class="odd"><td style="text-align: center;">10009</td><td style="text-align: center;">2</td><td style="text-align: center;">85994</td></tr><tr class="even"><td style="text-align: center;">10010</td><td style="text-align: center;">1</td><td style="text-align: center;">76884</td></tr></tbody></table><p> 解题思路:分部门查询最高薪水和最低薪水,然后去掉。</p><h1 id="查询最高薪水和最低薪水">查询最高薪水和最低薪水</h1><p> 查询最高和最低薪水可以利用窗口函数 rank ():</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span></span><br><span class="line">*,</span><br><span class="line"><span class="keyword">rank</span> () <span class="keyword">over</span> ( <span class="keyword">PARTITION</span> <span class="keyword">BY</span> department_id <span class="keyword">ORDER</span> <span class="keyword">BY</span> salary ) rank_1,</span><br><span class="line"><span class="keyword">rank</span> () <span class="keyword">over</span> ( <span class="keyword">PARTITION</span> <span class="keyword">BY</span> department_id <span class="keyword">ORDER</span> <span class="keyword">BY</span> salary <span class="keyword">DESC</span> ) rank_2 </span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line">data_test.salary_table</span><br></pre></td></tr></table></figure><table><thead><tr class="header"><th style="text-align: center;">employee_id</th><th style="text-align: center;">department_id</th><th style="text-align: center;">salary</th><th style="text-align: center;">rank_1</th><th style="text-align: center;">rank_2</th></tr></thead><tbody><tr class="odd"><td style="text-align: center;">10010</td><td style="text-align: center;">1</td><td style="text-align: center;">76884</td><td style="text-align: center;">5</td><td style="text-align: center;">1</td></tr><tr class="even"><td style="text-align: center;">10008</td><td style="text-align: center;">1</td><td style="text-align: center;">75286</td><td style="text-align: center;">4</td><td style="text-align: center;">2</td></tr><tr class="odd"><td style="text-align: center;">10005</td><td style="text-align: center;">1</td><td style="text-align: center;">66961</td><td style="text-align: center;">3</td><td style="text-align: center;">3</td></tr><tr class="even"><td style="text-align: center;">10004</td><td style="text-align: center;">1</td><td style="text-align: center;">66596</td><td style="text-align: center;">2</td><td style="text-align: center;">4</td></tr><tr class="odd"><td style="text-align: center;">10001</td><td style="text-align: center;">1</td><td style="text-align: center;">60117</td><td style="text-align: center;">1</td><td style="text-align: center;">5</td></tr><tr class="even"><td style="text-align: center;">10007</td><td style="text-align: center;">2</td><td style="text-align: center;">94333</td><td style="text-align: center;">5</td><td style="text-align: center;">1</td></tr><tr class="odd"><td style="text-align: center;">10002</td><td style="text-align: center;">2</td><td style="text-align: center;">92102</td><td style="text-align: center;">4</td><td style="text-align: center;">2</td></tr><tr class="even"><td style="text-align: center;">10003</td><td style="text-align: center;">2</td><td style="text-align: center;">86074</td><td style="text-align: center;">3</td><td style="text-align: center;">3</td></tr><tr class="odd"><td style="text-align: center;">10009</td><td style="text-align: center;">2</td><td style="text-align: center;">85994</td><td style="text-align: center;">2</td><td style="text-align: center;">4</td></tr><tr class="even"><td style="text-align: center;">10006</td><td style="text-align: center;">2</td><td style="text-align: center;">81046</td><td style="text-align: center;">1</td><td style="text-align: center;">5</td></tr></tbody></table><h1 id="去除最高薪水和最低薪水">去除最高薪水和最低薪水</h1><p> 利用子查询 + where 语句:</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span></span><br><span class="line">department_id,</span><br><span class="line"><span class="keyword">AVG</span>(salary) avg_salary</span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line">(<span class="keyword">SELECT</span></span><br><span class="line">*,</span><br><span class="line"><span class="keyword">rank</span> () <span class="keyword">over</span> ( <span class="keyword">PARTITION</span> <span class="keyword">BY</span> department_id <span class="keyword">ORDER</span> <span class="keyword">BY</span> salary ) rank_1,</span><br><span class="line"><span class="keyword">rank</span> () <span class="keyword">over</span> ( <span class="keyword">PARTITION</span> <span class="keyword">BY</span> department_id <span class="keyword">ORDER</span> <span class="keyword">BY</span> salary <span class="keyword">DESC</span> ) rank_2 </span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line">data_test.salary_table) t</span><br><span class="line"><span class="keyword">WHERE</span></span><br><span class="line">t.rank_1 > <span class="number">1</span> <span class="keyword">and</span> t.rank_2 > <span class="number">1</span></span><br><span class="line"><span class="keyword">GROUP</span> <span class="keyword">BY</span></span><br><span class="line">department_id</span><br></pre></td></tr></table></figure><table><thead><tr class="header"><th style="text-align: center;">department_id</th><th style="text-align: center;">avg_salary</th></tr></thead><tbody><tr class="odd"><td style="text-align: center;">1</td><td style="text-align: center;">69614.3333</td></tr><tr class="even"><td style="text-align: center;">2</td><td style="text-align: center;">88056.6667</td></tr></tbody></table><p> 最后需要对结果进行格式化处理:format 函数</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span></span><br><span class="line">department_id,</span><br><span class="line"><span class="keyword">FORMAT</span>(<span class="keyword">AVG</span>(salary), <span class="number">0</span>) avg_salary</span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line">(<span class="keyword">SELECT</span></span><br><span class="line">*,</span><br><span class="line"><span class="keyword">rank</span> () <span class="keyword">over</span> ( <span class="keyword">PARTITION</span> <span class="keyword">BY</span> department_id <span class="keyword">ORDER</span> <span class="keyword">BY</span> salary ) rank_1,</span><br><span class="line"><span class="keyword">rank</span> () <span class="keyword">over</span> ( <span class="keyword">PARTITION</span> <span class="keyword">BY</span> department_id <span class="keyword">ORDER</span> <span class="keyword">BY</span> salary <span class="keyword">DESC</span> ) rank_2 </span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line">data_test.salary_table) t</span><br><span class="line"><span class="keyword">WHERE</span></span><br><span class="line">t.rank_1 > <span class="number">1</span> <span class="keyword">and</span> t.rank_2 > <span class="number">1</span></span><br><span class="line"><span class="keyword">GROUP</span> <span class="keyword">BY</span></span><br><span class="line">department_id</span><br></pre></td></tr></table></figure><table><thead><tr class="header"><th style="text-align: center;">department_id</th><th style="text-align: center;">avg_salary</th></tr></thead><tbody><tr class="odd"><td style="text-align: center;">1</td><td style="text-align: center;">69614</td></tr><tr class="even"><td style="text-align: center;">2</td><td style="text-align: center;">88057</td></tr></tbody></table><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kity.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kityminder.core.min.js"></script><script defer="true" type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.js"></script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.css">]]></content>
<summary type="html">
<p> 面试题:查询出每个部门出去最高和最低薪水后的平均薪水,并保留整数。</p>
</summary>
<category term="sql面试题" scheme="http://zhouweidong.ml/categories/sql%E9%9D%A2%E8%AF%95%E9%A2%98/"/>
<category term="面试题" scheme="http://zhouweidong.ml/tags/%E9%9D%A2%E8%AF%95%E9%A2%98/"/>
<category term="sql" scheme="http://zhouweidong.ml/tags/sql/"/>
</entry>
<entry>
<title>如何分析交易量增加的原因?</title>
<link href="http://zhouweidong.ml/2021/01/11/%E5%A6%82%E4%BD%95%E5%88%86%E6%9E%90%E4%BA%A4%E6%98%93%E9%87%8F%E5%A2%9E%E5%8A%A0%E7%9A%84%E5%8E%9F%E5%9B%A0%EF%BC%9F/"/>
<id>http://zhouweidong.ml/2021/01/11/如何分析交易量增加的原因?/</id>
<published>2021-01-11T14:10:33.000Z</published>
<updated>2021-01-11T15:05:52.805Z</updated>
<content type="html"><![CDATA[<p> 面试题:根据下面的业务数据,进行分析并得出观点。</p><table><thead><tr class="header"><th style="text-align: center;">日期</th><th style="text-align: center;">交易量</th><th style="text-align: center;">交易笔数</th><th style="text-align: center;">客户数</th><th style="text-align: center;">新客户数</th><th style="text-align: center;">新客户交易笔数</th><th style="text-align: center;">新客户交易量</th></tr></thead><tbody><tr class="odd"><td style="text-align: center;">2020/4/1</td><td style="text-align: center;">594.7</td><td style="text-align: center;">16.8</td><td style="text-align: center;">13.5</td><td style="text-align: center;">1.9</td><td style="text-align: center;">2.2</td><td style="text-align: center;">65.9</td></tr><tr class="even"><td style="text-align: center;">2020/4/2</td><td style="text-align: center;">601.9</td><td style="text-align: center;">17.0</td><td style="text-align: center;">13.5</td><td style="text-align: center;">4.0</td><td style="text-align: center;">4.7</td><td style="text-align: center;">133.8</td></tr><tr class="odd"><td style="text-align: center;">2020/4/3</td><td style="text-align: center;">607.2</td><td style="text-align: center;">17.4</td><td style="text-align: center;">13.8</td><td style="text-align: center;">4.4</td><td style="text-align: center;">4.4</td><td style="text-align: center;">132.8</td></tr><tr class="even"><td style="text-align: center;">2020/4/4</td><td style="text-align: center;">632.1</td><td style="text-align: center;">17.9</td><td style="text-align: center;">14.1</td><td style="text-align: center;">4.8</td><td style="text-align: center;">4.8</td><td style="text-align: center;">162.5</td></tr><tr class="odd"><td style="text-align: center;">2020/4/5</td><td style="text-align: center;">685.4</td><td style="text-align: center;">19.1</td><td style="text-align: center;">15.0</td><td style="text-align: center;">6.1</td><td style="text-align: center;">6.1</td><td style="text-align: center;">192.8</td></tr><tr class="even"><td style="text-align: center;">2020/4/6</td><td style="text-align: center;">756.6</td><td style="text-align: center;">18.7</td><td style="text-align: center;">14.9</td><td style="text-align: center;">5.3</td><td style="text-align: center;">5.3</td><td style="text-align: center;">217.5</td></tr><tr class="odd"><td style="text-align: center;">2020/4/7</td><td style="text-align: center;">753.4</td><td style="text-align: center;">18.2</td><td style="text-align: center;">14.5</td><td style="text-align: center;">4.1</td><td style="text-align: center;">4.1</td><td style="text-align: center;">164.7</td></tr><tr class="even"><td style="text-align: center;">2020/4/8</td><td style="text-align: center;">640.3</td><td style="text-align: center;">18.8</td><td style="text-align: center;">14.6</td><td style="text-align: center;">4.7</td><td style="text-align: center;">4.7</td><td style="text-align: center;">164.8</td></tr><tr class="odd"><td style="text-align: center;">2020/4/9</td><td style="text-align: center;">1236.2</td><td style="text-align: center;">39.6</td><td style="text-align: center;">23.9</td><td style="text-align: center;">18.8</td><td style="text-align: center;">18.8</td><td style="text-align: center;">412.2</td></tr><tr class="even"><td style="text-align: center;">2020/4/10</td><td style="text-align: center;">664.6</td><td style="text-align: center;">19.7</td><td style="text-align: center;">15.3</td><td style="text-align: center;">4.4</td><td style="text-align: center;">4.4</td><td style="text-align: center;">145.8</td></tr></tbody></table><a id="more"></a><h1 id="明确要解决的问题">明确要解决的问题</h1><h2 id="第一步明确数据的来源和验证其准确性">第一步:明确数据的来源和验证其准确性</h2><p> 可以从时间、地点、数据来源三个维度验证数据的准确性:</p><ul><li>时间:2020.4.1~2020.4.10</li><li>地点:全网销售</li><li>数据来源:后台导出,数据准确。</li></ul><h2 id="第二步可视化相关数据">第二步:可视化相关数据</h2><p><img src="https://picgo-1258437747.cos.ap-nanjing.myqcloud.com/20210111222241.png"></p><p> 从折线图可知:9 号交易量骤然增加,其余日期交易量相差不大,比较平稳。</p><p><img src="https://picgo-1258437747.cos.ap-nanjing.myqcloud.com/20210111222620.png"></p><p> 排除数据异常问题,可以进一步获取上月同期和上年同期的交易量:</p><p><img src="https://picgo-1258437747.cos.ap-nanjing.myqcloud.com/20210111222931.png"></p><h1 id="分析问题出现的原因">分析问题出现的原因</h1><p> 分析问题出现的原因,可以对指标进行多维度拆解:</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">graph LR</span><br><span class="line">A (交易量) --> B (交易笔数)</span><br><span class="line">A (交易量) --> C (单均交易金额)</span><br><span class="line">B (交易笔数) --> D1 (新客户交易笔数)</span><br><span class="line">B (交易笔数) --> D2 (老客户交易笔数)</span><br></pre></td></tr></table></figure><p> 根据指标的拆解,可以做出如下假设:</p><ul><li>假设 1:单均交易金额增加导致 9 号交易量增加。</li><li>假设 2:新客户交易笔数增加导致 9 号交易量增加。</li><li>假设 3:老客户交易笔数增加导致 9 号交易量增加。</li></ul><p> 单均交易金额 = 交易量 / 交易笔数</p><figure><img src="https://picgo-1258437747.cos.ap-nanjing.myqcloud.com/20210111225431.png" alt="20210111225431"><figcaption aria-hidden="true">20210111225431</figcaption></figure><p> 可以看到:9 号单均交易金额骤然下降,与交易量骤增的形势正好相反,假设 1 不成立。</p><p> 交易笔数 = 新客户交易笔数 + 老客户交易笔数</p><figure><img src="https://picgo-1258437747.cos.ap-nanjing.myqcloud.com/20210111230048.png" alt="20210111230048"><figcaption aria-hidden="true">20210111230048</figcaption></figure><p> 可以看到:新客户交易笔数和老客户交易笔数在 9 号同时骤然增加,与交易量的走势相符,表明假设 2 和假设 3 正确。</p><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kity.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kityminder.core.min.js"></script><script defer="true" type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.js"></script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.css">]]></content>
<summary type="html">
<p> 面试题:根据下面的业务数据,进行分析并得出观点。</p>
<table>
<thead>
<tr class="header">
<th style="text-align: center;">日期</th>
<th style="text-align: center;">交易量</th>
<th style="text-align: center;">交易笔数</th>
<th style="text-align: center;">客户数</th>
<th style="text-align: center;">新客户数</th>
<th style="text-align: center;">新客户交易笔数</th>
<th style="text-align: center;">新客户交易量</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: center;">2020/4/1</td>
<td style="text-align: center;">594.7</td>
<td style="text-align: center;">16.8</td>
<td style="text-align: center;">13.5</td>
<td style="text-align: center;">1.9</td>
<td style="text-align: center;">2.2</td>
<td style="text-align: center;">65.9</td>
</tr>
<tr class="even">
<td style="text-align: center;">2020/4/2</td>
<td style="text-align: center;">601.9</td>
<td style="text-align: center;">17.0</td>
<td style="text-align: center;">13.5</td>
<td style="text-align: center;">4.0</td>
<td style="text-align: center;">4.7</td>
<td style="text-align: center;">133.8</td>
</tr>
<tr class="odd">
<td style="text-align: center;">2020/4/3</td>
<td style="text-align: center;">607.2</td>
<td style="text-align: center;">17.4</td>
<td style="text-align: center;">13.8</td>
<td style="text-align: center;">4.4</td>
<td style="text-align: center;">4.4</td>
<td style="text-align: center;">132.8</td>
</tr>
<tr class="even">
<td style="text-align: center;">2020/4/4</td>
<td style="text-align: center;">632.1</td>
<td style="text-align: center;">17.9</td>
<td style="text-align: center;">14.1</td>
<td style="text-align: center;">4.8</td>
<td style="text-align: center;">4.8</td>
<td style="text-align: center;">162.5</td>
</tr>
<tr class="odd">
<td style="text-align: center;">2020/4/5</td>
<td style="text-align: center;">685.4</td>
<td style="text-align: center;">19.1</td>
<td style="text-align: center;">15.0</td>
<td style="text-align: center;">6.1</td>
<td style="text-align: center;">6.1</td>
<td style="text-align: center;">192.8</td>
</tr>
<tr class="even">
<td style="text-align: center;">2020/4/6</td>
<td style="text-align: center;">756.6</td>
<td style="text-align: center;">18.7</td>
<td style="text-align: center;">14.9</td>
<td style="text-align: center;">5.3</td>
<td style="text-align: center;">5.3</td>
<td style="text-align: center;">217.5</td>
</tr>
<tr class="odd">
<td style="text-align: center;">2020/4/7</td>
<td style="text-align: center;">753.4</td>
<td style="text-align: center;">18.2</td>
<td style="text-align: center;">14.5</td>
<td style="text-align: center;">4.1</td>
<td style="text-align: center;">4.1</td>
<td style="text-align: center;">164.7</td>
</tr>
<tr class="even">
<td style="text-align: center;">2020/4/8</td>
<td style="text-align: center;">640.3</td>
<td style="text-align: center;">18.8</td>
<td style="text-align: center;">14.6</td>
<td style="text-align: center;">4.7</td>
<td style="text-align: center;">4.7</td>
<td style="text-align: center;">164.8</td>
</tr>
<tr class="odd">
<td style="text-align: center;">2020/4/9</td>
<td style="text-align: center;">1236.2</td>
<td style="text-align: center;">39.6</td>
<td style="text-align: center;">23.9</td>
<td style="text-align: center;">18.8</td>
<td style="text-align: center;">18.8</td>
<td style="text-align: center;">412.2</td>
</tr>
<tr class="even">
<td style="text-align: center;">2020/4/10</td>
<td style="text-align: center;">664.6</td>
<td style="text-align: center;">19.7</td>
<td style="text-align: center;">15.3</td>
<td style="text-align: center;">4.4</td>
<td style="text-align: center;">4.4</td>
<td style="text-align: center;">145.8</td>
</tr>
</tbody>
</table>
</summary>
<category term="数据分析" scheme="http://zhouweidong.ml/categories/%E6%95%B0%E6%8D%AE%E5%88%86%E6%9E%90/"/>
<category term="面试题" scheme="http://zhouweidong.ml/tags/%E9%9D%A2%E8%AF%95%E9%A2%98/"/>
<category term="数据分析" scheme="http://zhouweidong.ml/tags/%E6%95%B0%E6%8D%AE%E5%88%86%E6%9E%90/"/>
</entry>
<entry>
<title>Excel 复杂业务查询</title>
<link href="http://zhouweidong.ml/2021/01/09/Excel-%E5%A4%8D%E6%9D%82%E4%B8%9A%E5%8A%A1%E6%9F%A5%E8%AF%A2/"/>
<id>http://zhouweidong.ml/2021/01/09/Excel-复杂业务查询/</id>
<published>2021-01-09T14:50:06.000Z</published>
<updated>2021-01-09T15:51:51.959Z</updated>
<content type="html"><![CDATA[<p> 面试题:利用下拉框选择机构名称、利率档、期限等条件,查询出相应的费用。</p><a id="more"></a><p><img src="https://picgo-1258437747.cos.ap-nanjing.myqcloud.com/20210109225303.png"></p><h1 id="制作联动下拉列表">制作联动下拉列表</h1><p> 制作下拉列表可以借助数据有效性,具体步骤见下图:</p><p><img src="https://picgo-1258437747.cos.ap-nanjing.myqcloud.com/20210109225732.png"></p><p> 具体到本案例中,首先需要找到机构名称和利率档的非重复值,可以利用高级筛选,步骤见下图:</p><p><img src="https://picgo-1258437747.cos.ap-nanjing.myqcloud.com/20210109230109.png"></p><p> 筛选结果见下图:</p><p><img src="https://picgo-1258437747.cos.ap-nanjing.myqcloud.com/20210109230409.png"></p><p> 需要转换成下图的格式:</p><p><img src="https://picgo-1258437747.cos.ap-nanjing.myqcloud.com/20210109230729.png"></p><p> 然后利用公式 - 定义的名称,对区域进行定义,具体步骤见下图:</p><p><img src="https://picgo-1258437747.cos.ap-nanjing.myqcloud.com/20210109233052.png"></p><p> 同理,可以分别将各机构的利率档定义为 A 机构、B 机构和 C 机构。</p><p> 为了实现 <a href="https://jingyan.baidu.com/article/7c6fb42802525a80642c9034.html" target="_blank" rel="noopener"></a>,需要将序列的名称用公式定义:</p><p><img src="https://picgo-1258437747.cos.ap-nanjing.myqcloud.com/20210109233319.png"></p><h1 id="查询费用">查询费用</h1><p> 查询费用即根据条件在区域内匹配值,可以利用 index+match,index 的作用是在区域内选择指定位置的数,具体参数见下图:</p><p><img src="https://picgo-1258437747.cos.ap-nanjing.myqcloud.com/20210109233534.png"></p><p> 区域是确定的,变化的是行号和列号,需要借助 match 进行匹配:</p><p><img src="https://picgo-1258437747.cos.ap-nanjing.myqcloud.com/20210109233654.png"></p><p> 首先是列号,比较简单:MATCH (I3&"期费用",A1:E1,0)。</p><p> 然后是行号,稍微复杂,有两个条件,对应两个区域,条件和条件间、区域和区域间用 & 进行连接:MATCH (I1&I2, A:A&B:B,0)。</p><p> 总结可得公式为:INDEX (A:E, MATCH (I1&I2, A:A&B:B,0), MATCH (I3&"期费用",A1:E1,0)),值得注意的是,这是一个数组公式,输入公式后,按住 ctrl_shift+enter 公式才会生效。</p><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kity.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kityminder.core.min.js"></script><script defer="true" type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.js"></script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.css">]]></content>
<summary type="html">
<p> 面试题:利用下拉框选择机构名称、利率档、期限等条件,查询出相应的费用。</p>
</summary>
<category term="Excel" scheme="http://zhouweidong.ml/categories/Excel/"/>
<category term="Excel" scheme="http://zhouweidong.ml/tags/Excel/"/>
<category term="面试题" scheme="http://zhouweidong.ml/tags/%E9%9D%A2%E8%AF%95%E9%A2%98/"/>
</entry>
<entry>
<title>会员价值分析</title>
<link href="http://zhouweidong.ml/2021/01/03/%E4%BC%9A%E5%91%98%E4%BB%B7%E5%80%BC%E5%88%86%E6%9E%90/"/>
<id>http://zhouweidong.ml/2021/01/03/会员价值分析/</id>
<published>2021-01-03T10:16:15.000Z</published>
<updated>2021-01-03T15:45:50.995Z</updated>
<content type="html"><![CDATA[<p> 目前有如下数据:会员卡信息(会员 Id、卡状态、性别、年龄、HYKTYPE(等同于会员卡类型编码)等)、会员卡类型(卡折扣率、卡类型编码和卡类型名称)、门店品牌表(品牌代码、品牌名、品类代码、品类名、店代码、店名等)和销售明细表(会员卡类型编码、销售金额、销售数量、会员卡类型、会员卡 Id 等)。</p><a id="more"></a><h1 id="漏斗图分析会员结构的合理性">漏斗图分析会员结构的合理性</h1><p> 如何进行会员价值分析呢?一个方法是漏斗图,漏斗图可以用于分析流程中每一步的转化和流失:</p><p><img src="https://picgo-1258437747.cos.ap-nanjing.myqcloud.com/20210103224153.png"></p><p> 可以看出,会员转化不符合倒金字塔形状,贵宾卡用户数大于积分卡用户,原因可能是前期快速扩张导致的,如果是此原因后期需控制积分卡升级比率,保障会员卡的价值性。</p><p> 同时,与会员结构息息相关的是会员流失情况,明显会员流失情况较为严重,大于一般行业平均水平 10%。</p><p><img src="https://picgo-1258437747.cos.ap-nanjing.myqcloud.com/20210103225037.png"></p><p> 最后,会员总数也是十分重要的观察指标。</p><p><img src="https://picgo-1258437747.cos.ap-nanjing.myqcloud.com/20210103225922.png"></p><h1 id="rfm-模型用户价值分类">RFM 模型:用户价值分类</h1><p> RFM 模型可以给用户进行分类,衡量各分类用户的价值和获利能力:</p><p><img src="https://picgo-1258437747.cos.ap-nanjing.myqcloud.com/20210103231359.png"></p><p> 进一步,分析每类用户的消费金额:</p><p><img src="https://picgo-1258437747.cos.ap-nanjing.myqcloud.com/20210103231735.png"></p><p> 具体,可以分会员类型进行 RFM 分类:</p><p><img src="https://picgo-1258437747.cos.ap-nanjing.myqcloud.com/20210103232122.png"></p><p> 具体结论:</p><ul><li><p>低价值的一般挽留客户人数过多,反映出前期会员人数快速扩张但后期维护未及时跟上导致的后遗症;</p></li><li><p>最高等级的钻石和黑钻会员中出现了较高比例的价值一般性用户,可能存在一卡多用或将会员卡权益进行倒卖的情况,需重点排查。</p></li></ul><h1 id="会员消费偏好">会员消费偏好</h1><p> 分类会员消费偏好,从品类到品牌:</p><p><img src="https://picgo-1258437747.cos.ap-nanjing.myqcloud.com/20210103233314.png"></p><p><img src="https://picgo-1258437747.cos.ap-nanjing.myqcloud.com/20210103233346.png"></p><p> 也可以分析每类用户的消费区间,看用户类型是否合理:</p><p><img src="https://picgo-1258437747.cos.ap-nanjing.myqcloud.com/20210103233545.png"></p><p> 具体结论: - 根据不同会员的消费偏好,定向推送活动信息。比如针对积分卡用户重点以化妆品类中的兰蔻、香奈儿等品牌作为大促活动的主推点; - 积分卡和贵宾卡会员的消费区间主要落在 0-300 和 500-1000 的区间,建议每期大促可针对该二类会员定向推送 300-xx 元和 500-xx 元的满减优惠券;钻石卡会员建议大促推送 300 元和 2000 元的满减优惠券;黑钻卡会员建议大促推送 1000 元和 2000 元的满减优惠券。</p><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kity.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kityminder.core.min.js"></script><script defer="true" type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.js"></script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.css">]]></content>
<summary type="html">
<p> 目前有如下数据:会员卡信息(会员 Id、卡状态、性别、年龄、HYKTYPE(等同于会员卡类型编码)等)、会员卡类型(卡折扣率、卡类型编码和卡类型名称)、门店品牌表(品牌代码、品牌名、品类代码、品类名、店代码、店名等)和销售明细表(会员卡类型编码、销售金额、销售数量、会员卡类型、会员卡 Id 等)。</p>
</summary>
</entry>
<entry>
<title>7 周成为数据分析师:SQL 练习题</title>
<link href="http://zhouweidong.ml/2020/12/29/7%E5%91%A8%E7%A7%B0%E4%B8%BA%E6%95%B0%E6%8D%AE%E5%88%86%E6%9E%90%E5%B8%88%EF%BC%9ASQL-%E7%BB%83%E4%B9%A0%E9%A2%98/"/>
<id>http://zhouweidong.ml/2020/12/29/7周称为数据分析师:SQL-练习题/</id>
<published>2020-12-29T13:32:36.000Z</published>
<updated>2020-12-30T11:19:22.973Z</updated>
<content type="html"><![CDATA[<p> 在秦路老师的《7 周成为数据分析师》的 SQL 教程中,提出了几个问题:</p><ul><li>统计不同月份的下单人数</li><li>统计用户三月份的回购率和复购率</li><li>统计男女用户的消费频次是否有差异?</li><li>统计多次消费的用户,第一次和最后一次消费间隔是多少?</li><li>统计不同年龄段,用户的消费金额是否有差异?</li><li>统计消费的二八法则,消费的 top 20% 用户,贡献了多少额度?</li></ul><a id="more"></a><h1 id="统计不同月份的下单人数">统计不同月份的下单人数</h1><p> 首先,需要剔除未下单的用户;其次需要按年和月份进行分组统计;然后,需要对不同用户进行统计;最后,需要剔除脏数据(年份为 0000 的数据)。</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> </span><br><span class="line"> <span class="keyword">YEAR</span>(paid_time) <span class="keyword">year</span>,</span><br><span class="line"> <span class="keyword">MONTH</span>(paid_time) <span class="keyword">month</span>,</span><br><span class="line"> <span class="keyword">count</span>( <span class="keyword">distinct</span> userId ) <span class="keyword">num</span></span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line"> data_test.order_info </span><br><span class="line"><span class="keyword">WHERE</span></span><br><span class="line"> is_paid = <span class="string">' 已支付 '</span> <span class="keyword">AND</span></span><br><span class="line"> <span class="keyword">year</span>(paid_time) > <span class="number">0</span></span><br><span class="line"><span class="keyword">GROUP</span> <span class="keyword">BY</span></span><br><span class="line"> <span class="keyword">YEAR</span>(paid_time) , </span><br><span class="line"> <span class="keyword">MONTH</span> (paid_time)</span><br></pre></td></tr></table></figure><h1 id="统计用户三月份的回购率和复购率">统计用户三月份的回购率和复购率</h1><p> 回购率是指回购用户占总用户的比例,回购用户是指购买产品一段时间后再次购买该产品的用户。对于不同属性的产品,间隔时间的设定应该是不一样的,这里暂设为一个月。</p><p> 用 SQL 计算回购率,最便捷的方式是自连接,即两个同样的表进行连接,但连接键设置为相隔一个月,即 3 月和 2 月连接。</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span></span><br><span class="line"> <span class="keyword">DATE_FORMAT</span>(t1.m, <span class="string">"% Y-% m"</span>) <span class="keyword">month</span>,</span><br><span class="line"> <span class="keyword">count</span>(t2.m)/<span class="keyword">count</span>(t1.m) repurchase_rate</span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line"> (<span class="keyword">SELECT</span> </span><br><span class="line"> userId, </span><br><span class="line"> <span class="keyword">DATE_FORMAT</span>(paid_time, <span class="string">"% Y-% m-01"</span>) m</span><br><span class="line"> <span class="keyword">FROM</span> </span><br><span class="line"> data_test.order_info</span><br><span class="line"> <span class="keyword">WHERE</span></span><br><span class="line"> is_paid = <span class="string">' 已支付 '</span></span><br><span class="line"> <span class="keyword">GROUP</span> <span class="keyword">BY</span> </span><br><span class="line"> userId, </span><br><span class="line"> <span class="keyword">DATE_FORMAT</span>(paid_time, <span class="string">"% Y-% m-01"</span>)) t1</span><br><span class="line"><span class="keyword">LEFT</span> <span class="keyword">JOIN</span> (<span class="keyword">SELECT</span></span><br><span class="line"> userId,</span><br><span class="line"> <span class="keyword">DATE_FORMAT</span>(paid_time, <span class="string">"% Y-% m-01"</span>) m</span><br><span class="line"> <span class="keyword">FROM</span></span><br><span class="line"> data_test.order_info</span><br><span class="line"> <span class="keyword">WHERE</span></span><br><span class="line"> is_paid = <span class="string">' 已支付 '</span></span><br><span class="line"> <span class="keyword">GROUP</span> <span class="keyword">BY</span></span><br><span class="line"> userId,</span><br><span class="line"> <span class="keyword">DATE_FORMAT</span>(paid_time, <span class="string">"% Y-% m-01"</span>)) t2</span><br><span class="line"><span class="keyword">ON</span></span><br><span class="line"> t1.userId = t2.userId <span class="keyword">AND</span></span><br><span class="line"> t1.m = <span class="keyword">DATE_SUB</span>(t2.m,<span class="built_in">INTERVAL</span> <span class="number">1</span> <span class="keyword">MONTH</span>)</span><br><span class="line"><span class="keyword">WHERE</span></span><br><span class="line"> t1.m > <span class="number">0</span> <span class="keyword">or</span></span><br><span class="line"> t2.m > <span class="number">0</span></span><br><span class="line"><span class="keyword">GROUP</span> <span class="keyword">BY</span></span><br><span class="line"> t1.m</span><br></pre></td></tr></table></figure><p> 复购率是指购买次数大于 1 的用户数占总用户数的比例,这里可以用 count 嵌套 if 函数进行处理:</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> </span><br><span class="line"> d.order_month <span class="keyword">month</span>,</span><br><span class="line"> <span class="keyword">count</span>(<span class="keyword">if</span>(d.c > <span class="number">1</span>, <span class="number">1</span>, <span class="literal">null</span>))/<span class="keyword">count</span>(d.c) repurchase_rate</span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line"> (<span class="keyword">SELECT</span> </span><br><span class="line"> userId,</span><br><span class="line"> <span class="keyword">DATE_FORMAT</span>(paid_time, <span class="string">"% Y-% m"</span>) order_month,</span><br><span class="line"> <span class="keyword">COUNT</span>(userId) c</span><br><span class="line"> <span class="keyword">FROM</span></span><br><span class="line"> data_test.order_info</span><br><span class="line"> <span class="keyword">WHERE</span></span><br><span class="line"> is_paid = <span class="string">' 已支付 '</span></span><br><span class="line"> <span class="keyword">GROUP</span> <span class="keyword">BY</span></span><br><span class="line"> userId,</span><br><span class="line"> order_month</span><br><span class="line"> ) d</span><br><span class="line"> <span class="keyword">GROUP</span> <span class="keyword">BY</span></span><br><span class="line"> d.order_month</span><br></pre></td></tr></table></figure><h1 id="统计男女用户的消费频次是否有差异">统计男女用户的消费频次是否有差异</h1><p> 首先需要通过多表连接将订单表和用户表拼接在一起,获取相应信息;其次需要分性别和用户统计订单数;最后,利用平均数计算消费频次。</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span></span><br><span class="line"> sex,</span><br><span class="line"> <span class="keyword">avg</span>(<span class="keyword">num</span>) fre</span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line"> (<span class="keyword">SELECT</span></span><br><span class="line"> o.userId, sex, <span class="keyword">count</span>(<span class="number">1</span>) <span class="keyword">num</span></span><br><span class="line"> <span class="keyword">FROM</span></span><br><span class="line"> data_test.order_info o</span><br><span class="line"> <span class="keyword">inner</span> <span class="keyword">JOIN</span> (</span><br><span class="line"> <span class="keyword">SELECT</span></span><br><span class="line"> *</span><br><span class="line"> <span class="keyword">FROM</span></span><br><span class="line"> data_test.user_info</span><br><span class="line"> <span class="keyword">WHERE</span></span><br><span class="line"> sex <> <span class="string">''</span> <span class="keyword">AND</span></span><br><span class="line"> is_paid = <span class="string">' 已支付 '</span>) u</span><br><span class="line"> <span class="keyword">ON</span></span><br><span class="line"> o.userId = u.userId</span><br><span class="line"> <span class="keyword">GROUP</span> <span class="keyword">BY</span></span><br><span class="line"> userId,</span><br><span class="line"> sex) t</span><br><span class="line"> <span class="keyword">GROUP</span> <span class="keyword">BY</span></span><br><span class="line"> sex</span><br></pre></td></tr></table></figure><h1 id="统计多次消费的用户第一次和最后一次消费间隔是多少">统计多次消费的用户,第一次和最后一次消费间隔是多少?</h1><p> 分组统计消费间隔,筛选出消费次数大于 1 的用户。</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span></span><br><span class="line"> userId,</span><br><span class="line"> <span class="keyword">DATEDIFF</span>(<span class="keyword">max</span>(paid_time), <span class="keyword">min</span>(paid_time)) intervals</span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line"> data_test.order_info</span><br><span class="line"><span class="keyword">WHERE</span></span><br><span class="line"> is_paid = <span class="string">' 已支付 '</span></span><br><span class="line"><span class="keyword">GROUP</span> <span class="keyword">BY</span></span><br><span class="line"> userId</span><br><span class="line"><span class="keyword">HAVING</span></span><br><span class="line"> <span class="keyword">count</span>(<span class="number">1</span>) > <span class="number">1</span></span><br></pre></td></tr></table></figure><h1 id="统计不同年龄段用户的消费金额是否有差异">统计不同年龄段,用户的消费金额是否有差异?</h1><p> 如果只是想看一下各个年龄段的消费金额是否有差异,可以用年龄除以区间间隔,向上取整,然后求消费金额的平均数:</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span></span><br><span class="line">age,</span><br><span class="line"><span class="keyword">AVG</span>(price) consumes</span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line">(<span class="keyword">SELECT</span></span><br><span class="line">o.userId,</span><br><span class="line"><span class="keyword">CEIL</span>((<span class="keyword">year</span>(<span class="keyword">now</span>()) - <span class="keyword">year</span>(birth_date))/<span class="number">10</span>) age,</span><br><span class="line">price</span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line">data_test.order_info o</span><br><span class="line"><span class="keyword">INNER</span> <span class="keyword">JOIN</span></span><br><span class="line">data_test.user_info u</span><br><span class="line"><span class="keyword">ON</span></span><br><span class="line">o.userId = u.userId</span><br><span class="line"><span class="keyword">WHERE</span></span><br><span class="line">is_paid = <span class="string">' 已支付 '</span> <span class="keyword">AND</span></span><br><span class="line">birth_date > <span class="built_in">date</span>(<span class="string">'1901-00-00'</span>)) t</span><br><span class="line"><span class="keyword">GROUP</span> <span class="keyword">BY</span></span><br><span class="line">age</span><br></pre></td></tr></table></figure><p> 如果想看一下哪个年龄段的消费金额最高或最低,需要利用 case when :</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span></span><br><span class="line">(<span class="keyword">CASE</span> </span><br><span class="line"> <span class="keyword">WHEN</span> age < <span class="number">10</span> <span class="keyword">THEN</span> <span class="string">'10 岁以内 '</span></span><br><span class="line"> <span class="keyword">WHEN</span> age <span class="keyword">BETWEEN</span> <span class="number">10</span> <span class="keyword">AND</span> <span class="number">19</span> <span class="keyword">THEN</span> <span class="string">'10~19 岁 '</span></span><br><span class="line"> <span class="keyword">WHEN</span> age <span class="keyword">BETWEEN</span> <span class="number">20</span> <span class="keyword">AND</span> <span class="number">29</span> <span class="keyword">THEN</span> <span class="string">'20~29 岁 '</span></span><br><span class="line"> <span class="keyword">WHEN</span> age <span class="keyword">BETWEEN</span> <span class="number">30</span> <span class="keyword">AND</span> <span class="number">39</span> <span class="keyword">THEN</span> <span class="string">'30~39 岁 '</span></span><br><span class="line"> <span class="keyword">WHEN</span> age <span class="keyword">BETWEEN</span> <span class="number">40</span> <span class="keyword">AND</span> <span class="number">49</span> <span class="keyword">THEN</span> <span class="string">'40~49 岁 '</span></span><br><span class="line"> <span class="keyword">WHEN</span> age <span class="keyword">BETWEEN</span> <span class="number">50</span> <span class="keyword">AND</span> <span class="number">59</span> <span class="keyword">THEN</span> <span class="string">'50~59 岁 '</span></span><br><span class="line"> <span class="keyword">ELSE</span> <span class="string">'60 岁及以上 '</span></span><br><span class="line"><span class="keyword">END</span>) <span class="keyword">as</span> ages,</span><br><span class="line"><span class="keyword">AVG</span>(price) price_avg</span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line">(<span class="keyword">SELECT</span></span><br><span class="line">o.userId,</span><br><span class="line">(<span class="keyword">year</span>(<span class="keyword">now</span>()) - <span class="keyword">year</span>(birth_date)) age,</span><br><span class="line">price</span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line">data_test.order_info o</span><br><span class="line"><span class="keyword">INNER</span> <span class="keyword">JOIN</span></span><br><span class="line">data_test.user_info u</span><br><span class="line"><span class="keyword">ON</span></span><br><span class="line">o.userId = u.userId</span><br><span class="line"><span class="keyword">WHERE</span></span><br><span class="line">is_paid = <span class="string">' 已支付 '</span> <span class="keyword">AND</span></span><br><span class="line">birth_date > <span class="built_in">date</span>(<span class="string">'1901-00-00'</span>)) t</span><br><span class="line"><span class="keyword">GROUP</span> <span class="keyword">BY</span></span><br><span class="line">ages</span><br></pre></td></tr></table></figure><h1 id="统计消费的二八法则消费的-top-20-用户贡献了多少额度">统计消费的二八法则,消费的 top 20% 用户,贡献了多少额度?</h1><p> 由于 MySQL 没有分组排名的函数 row_number,因此需借助 limit 限制条数来达到分组排名统计的功能:</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span></span><br><span class="line"><span class="keyword">COUNT</span>(userId) user_num,</span><br><span class="line"><span class="keyword">SUM</span>(total) total_num</span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line">(<span class="keyword">SELECT</span></span><br><span class="line">userId,</span><br><span class="line"><span class="keyword">sum</span>(price) <span class="keyword">as</span> total</span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line">data_test.order_info</span><br><span class="line"><span class="keyword">WHERE</span></span><br><span class="line">is_paid = <span class="string">' 已支付 '</span></span><br><span class="line"><span class="keyword">GROUP</span> <span class="keyword">BY</span></span><br><span class="line">userId</span><br><span class="line"><span class="keyword">ORDER</span> <span class="keyword">BY</span></span><br><span class="line">total <span class="keyword">DESC</span></span><br><span class="line"><span class="keyword">LIMIT</span></span><br><span class="line"><span class="number">17000</span>) t</span><br></pre></td></tr></table></figure><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kity.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kityminder.core.min.js"></script><script defer="true" type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.js"></script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.css">]]></content>
<summary type="html">
<p> 在秦路老师的《7 周成为数据分析师》的 SQL 教程中,提出了几个问题:</p>
<ul>
<li>统计不同月份的下单人数</li>
<li>统计用户三月份的回购率和复购率</li>
<li>统计男女用户的消费频次是否有差异?</li>
<li>统计多次消费的用户,第一次和最后一次消费间隔是多少?</li>
<li>统计不同年龄段,用户的消费金额是否有差异?</li>
<li>统计消费的二八法则,消费的 top 20% 用户,贡献了多少额度?</li>
</ul>
</summary>
<category term="数据分析" scheme="http://zhouweidong.ml/categories/%E6%95%B0%E6%8D%AE%E5%88%86%E6%9E%90/"/>
<category term="SQL" scheme="http://zhouweidong.ml/categories/%E6%95%B0%E6%8D%AE%E5%88%86%E6%9E%90/SQL/"/>
</entry>
<entry>
<title>win10 安装 superset</title>
<link href="http://zhouweidong.ml/2020/12/18/win10-%E5%AE%89%E8%A3%85-superset/"/>
<id>http://zhouweidong.ml/2020/12/18/win10-安装-superset/</id>
<published>2020-12-18T02:59:50.000Z</published>
<updated>2020-12-18T05:18:45.180Z</updated>
<content type="html"><![CDATA[<p> 在安装 superset 之前,为了避免包版本的混乱,需要先创立一个虚拟的环境:</p><a id="more"></a><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">conda create -n superset python=<span class="number">3.7</span> <span class="comment"># 指定 python 版本为 3.7</span></span><br></pre></td></tr></table></figure><p> 然后,进入虚拟环境安装相应的包:</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">conda activate superset</span><br></pre></td></tr></table></figure><p> 在正式安装 superset 之前,需要先安装 sasl 和 python-geohash:</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">conda install -c conda-forge python-geohash -y</span><br><span class="line">conda install -c conda-forge sasl -y</span><br></pre></td></tr></table></figure><p> 最后,就可以安装 superset:</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">pip install apache-superset -i https://pypi.douban.com/simple</span><br><span class="line">pip install Pillow -i https://pypi.douban.com/simple</span><br></pre></td></tr></table></figure><p> 之后,可以对 superset 进行初始化:</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 创建管理员账户 </span></span><br><span class="line">set FLASK_APP=superset</span><br><span class="line">flask fab create-admin</span><br><span class="line"></span><br><span class="line"><span class="comment"># 初始化数据库 </span></span><br><span class="line">superset db upgrade</span><br><span class="line"></span><br><span class="line"><span class="comment"># 载入案例数据 </span></span><br><span class="line">superset load_examples</span><br><span class="line"></span><br><span class="line"><span class="comment"># 初始化角色和权限 </span></span><br><span class="line">superset init</span><br><span class="line"></span><br><span class="line"><span class="comment"># 启动服务,端口号 8001,使用 -p 更改端口号 </span></span><br><span class="line">superset run -p <span class="number">8001</span> --<span class="keyword">with</span>-threads --reload --debugger</span><br></pre></td></tr></table></figure><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kity.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kityminder.core.min.js"></script><script defer="true" type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.js"></script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.css">]]></content>
<summary type="html">
<p> 在安装 superset 之前,为了避免包版本的混乱,需要先创立一个虚拟的环境:</p>
</summary>
</entry>
<entry>
<title>AB test</title>
<link href="http://zhouweidong.ml/2020/12/16/AB-test/"/>
<id>http://zhouweidong.ml/2020/12/16/AB-test/</id>
<published>2020-12-16T12:49:41.000Z</published>
<updated>2020-12-29T04:51:16.672Z</updated>
<content type="html"><![CDATA[<p> AB test 是一种对照实验,目的是为了验证一个模型 / 版本是不是比另外一个模型 / 版本更好。 AB test</p><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kity.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kityminder.core.min.js"></script><script defer="true" type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.js"></script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.css">]]></content>
<summary type="html">
<p> AB test 是一种对照实验,目的是为了验证一个模型 / 版本是不是比另外一个模型 / 版本更好。 AB test</p>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/kity
</summary>
</entry>
<entry>
<title>数据分析:异常值处理</title>
<link href="http://zhouweidong.ml/2020/12/11/%E6%95%B0%E6%8D%AE%E5%88%86%E6%9E%90%EF%BC%9A%E5%BC%82%E5%B8%B8%E5%80%BC%E5%A4%84%E7%90%86/"/>
<id>http://zhouweidong.ml/2020/12/11/数据分析:异常值处理/</id>
<published>2020-12-11T02:15:48.000Z</published>
<updated>2020-12-11T07:29:11.221Z</updated>
<content type="html"><![CDATA[<p> 异常值处理是数据分析中的一个基本步骤,异常值是指不符合整体样本一般性性质的数值。</p><a id="more"></a><h1 id="异常值的检测方法">异常值的检测方法</h1><p> 常用的检测异常值的方法主要是:3<span class="math inline">\(\sigma\)</span> 原则、箱型图、格拉布斯准则和多维度异常检测。</p><h2 id="sigma-原则">3<span class="math inline">\(\sigma\)</span> 原则</h2><p> 3<span class="math inline">\(\sigma\)</span> 原则的必要条件是:数据需要服从正态分布。在 3<span class="math inline">\(\sigma\)</span> 原则下,异常值是指超过 3 倍标准差的值;如果数据不服从正态分布,也可以用近似值的 3 倍标准差作为替代。</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line"><span class="keyword">import</span> matplotlib.pyplot <span class="keyword">as</span> plt</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">ThreeSigmod</span><span class="params">(value)</span>:</span></span><br><span class="line"> avg = np.mean (value)</span><br><span class="line"> std = np.std (value)</span><br><span class="line"> threshold_up = avg + <span class="number">3</span>*std</span><br><span class="line"> threshold_down = avg - <span class="number">3</span>*std</span><br><span class="line"> <span class="keyword">return</span> [float (threshold_down), float (threshold_up)]</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">identify_outliers</span><span class="params">(value)</span>:</span></span><br><span class="line"> threshold = ThreeSigmod (value)</span><br><span class="line"> outliers = list (filter (<span class="keyword">lambda</span> x: (s<threshold [<span class="number">0</span>])|(s>threshold [<span class="number">1</span>]), value))</span><br><span class="line"> <span class="keyword">return</span> outliers</span><br></pre></td></tr></table></figure><h2 id="箱型图">箱型图</h2><p> </p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> matplotlib.pyplot <span class="keyword">as</span> plt</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">fig, axes = plt.subplots ()</span><br><span class="line">data.boxplot (column = <span class="string">''</span>, by=[<span class="string">''</span>, <span class="string">''</span>], ax=axes)</span><br></pre></td></tr></table></figure><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">threshold_value</span><span class="params">(value)</span>:</span></span><br><span class="line"> normal_value = np.quantile (value,<span class="number">0.75</span>) - np.quantile (value,<span class="number">0.25</span>)</span><br><span class="line"> quan_down = np.quantile (value,<span class="number">0.25</span>)<span class="number">-1.5</span>*normal_value</span><br><span class="line"> quan_up = np.quantile (value,<span class="number">0.75</span>)+<span class="number">1.5</span>*normal_value</span><br><span class="line"> <span class="keyword">return</span> [float (quan_down),float (quan_up)]</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">identity_outliers</span><span class="params">(value)</span>:</span></span><br><span class="line"> threshold = threshold_value (value)</span><br><span class="line"> outliers = list (filter (<span class="keyword">lambda</span> x: (s<threshold [<span class="number">0</span>])|(s>threshold [<span class="number">1</span>]), value))</span><br><span class="line"> <span class="keyword">return</span> outliers</span><br></pre></td></tr></table></figure><h2 id="格拉布斯法则">格拉布斯法则</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> outliers <span class="keyword">import</span> smirnov_grubbs <span class="keyword">as</span> grubbs</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">identity_outliers</span><span class="params">(value)</span>:</span></span><br><span class="line"> outliers = set (value) - set (list (grubbs.test (value, alpha = <span class="number">0.05</span>)))</span><br></pre></td></tr></table></figure><h2 id="多维度异常检测">多维度异常检测</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> scipy.spatial <span class="keyword">import</span> distance</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">identity_outliers</span><span class="params">(data, num)</span>:</span></span><br><span class="line"> m_dist_order = Series ([float (distance.mahalanobis (data.iloc [i], data.mean (), np.mat (data.cov ().as_matrix ()).I) ** <span class="number">2</span>) <span class="keyword">for</span> i <span class="keyword">in</span> range (len (df))]).sort_values (ascending=<span class="keyword">False</span>).index.tolist ()</span><br><span class="line"> outliers_index = m_dist_order [:num]</span><br><span class="line"> outliers = data.iloc [outliers_index]</span><br><span class="line"> <span class="keyword">return</span> outliers</span><br></pre></td></tr></table></figure><h1 id="异常值的处理方法">异常值的处理方法</h1><p> 处理异常值一般采用移动平均值和直接删除。</p><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kity.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kityminder.core.min.js"></script><script defer="true" type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.js"></script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.css">]]></content>
<summary type="html">
<p> 异常值处理是数据分析中的一个基本步骤,异常值是指不符合整体样本一般性性质的数值。</p>
</summary>
</entry>
<entry>
<title>Power BI 学习 Day1</title>
<link href="http://zhouweidong.ml/2020/12/08/Power-BI-%E5%AD%A6%E4%B9%A0-Day1/"/>
<id>http://zhouweidong.ml/2020/12/08/Power-BI-学习-Day1/</id>
<published>2020-12-08T13:16:58.000Z</published>
<updated>2020-12-08T13:58:54.872Z</updated>
<content type="html"><![CDATA[<p> 在 Power BI 中,可以很方便地为报表插入链接。</p><a id="more"></a><h1 id="向表中插入超链接"><a href="https://docs.microsoft.com/zh-cn/power-bi/create-reports/power-bi-hyperlinks-in-tables" target="_blank" rel="noopener">向表中插入超链接</a></h1><p> 如果数据已经包含 URL,可以通过将数据类别转换为 “Web URL” 插入超链接,具体操作如下图所示:</p><figure><img src="https://picgo-1258437747.cos.ap-nanjing.myqcloud.com/20201208213456.png" alt="20201208213456"><figcaption aria-hidden="true">20201208213456</figcaption></figure><p> 如果数据尚未包含 URL, 可以通过建立自定义列的方式插入超链接。</p><p> 对于长链接,可以在表中显示超链接图表,具体操作如下图所示:</p><figure><img src="https://picgo-1258437747.cos.ap-nanjing.myqcloud.com/20201208214252.png" alt="20201208214252"><figcaption aria-hidden="true">20201208214252</figcaption></figure><h1 id="向文本框插入超链接"><a href="https://docs.microsoft.com/zh-cn/power-bi/create-reports/service-add-hyperlink-to-text-box" target="_blank" rel="noopener">向文本框插入超链接</a></h1><p> 插入文本框并输入一段文字后,可以选中部分文字插入超链接,具体操作如下:</p><figure><img src="https://picgo-1258437747.cos.ap-nanjing.myqcloud.com/20201208214504.png" alt="20201208214504"><figcaption aria-hidden="true">20201208214504</figcaption></figure><figure><img src="https://picgo-1258437747.cos.ap-nanjing.myqcloud.com/20201208214513.png" alt="20201208214513"><figcaption aria-hidden="true">20201208214513</figcaption></figure><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kity.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kityminder.core.min.js"></script><script defer="true" type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.js"></script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.css">]]></content>
<summary type="html">
<p> 在 Power BI 中,可以很方便地为报表插入链接。</p>
</summary>
<category term="Power BI" scheme="http://zhouweidong.ml/tags/Power-BI/"/>
<category term="数据分析" scheme="http://zhouweidong.ml/tags/%E6%95%B0%E6%8D%AE%E5%88%86%E6%9E%90/"/>
<category term="可视化" scheme="http://zhouweidong.ml/tags/%E5%8F%AF%E8%A7%86%E5%8C%96/"/>
</entry>
<entry>
<title>LeetCode MySQL Day9</title>
<link href="http://zhouweidong.ml/2020/09/07/%E2%80%9CLeetCode-MySQL-Day9/"/>
<id>http://zhouweidong.ml/2020/09/07/“LeetCode-MySQL-Day9/</id>
<published>2020-09-07T14:34:41.000Z</published>
<updated>2020-09-07T14:49:43.376Z</updated>
<content type="html"><![CDATA[<p> 第 9 天:<br> <a id="more"></a> Write a SQL query to find all numbers that appear at least three times consecutively.</p><table><thead><tr class="header"><th>Id</th><th>Num</th></tr></thead><tbody><tr class="odd"><td>1</td><td>1</td></tr><tr class="even"><td>2</td><td>1</td></tr><tr class="odd"><td>3</td><td>1</td></tr><tr class="even"><td>4</td><td>2</td></tr><tr class="odd"><td>5</td><td>1</td></tr><tr class="even"><td>6</td><td>2</td></tr><tr class="odd"><td>7</td><td>2</td></tr><tr class="even"><td>-</td><td>--</td></tr></tbody></table><p>For example, given the above Logs table, 1 is the only number that appears consecutively for at least three times.</p><table><thead><tr class="header"><th>ConsecutiveNums</th></tr></thead><tbody><tr class="odd"><td>1</td></tr></tbody></table><p> 解决思路:前后相同则表示连续三个数均相同。<br></p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> <span class="keyword">DISTINCT</span></span><br><span class="line">( a.Num ) <span class="keyword">AS</span> ConsecutiveNums </span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line">(</span><br><span class="line"><span class="keyword">SELECT</span></span><br><span class="line"><span class="keyword">id</span>,</span><br><span class="line"><span class="keyword">num</span>,</span><br><span class="line">lag ( <span class="keyword">num</span>, <span class="number">1</span>, <span class="number">0</span> ) <span class="keyword">over</span> () <span class="keyword">AS</span> pre,</span><br><span class="line"><span class="keyword">lead</span> ( <span class="keyword">num</span>, <span class="number">1</span>, <span class="number">0</span> ) <span class="keyword">over</span> () <span class="keyword">AS</span> nxt </span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line"><span class="keyword">LOGS</span> </span><br><span class="line">) a </span><br><span class="line"><span class="keyword">WHERE</span></span><br><span class="line">a.Num = a.pre </span><br><span class="line"><span class="keyword">AND</span> a.Num = a.nxt</span><br></pre></td></tr></table></figure><p> 知识点:窗口函数 <a href="https://www.jianshu.com/p/e0d73f8b71ec" target="_blank" rel="noopener">lag () 和 lead ()</a>。</p><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kity.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kityminder.core.min.js"></script><script defer="true" type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.js"></script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.css">]]></content>
<summary type="html">
<p> 第 9 天:<br>
</summary>
</entry>
<entry>
<title>LeetCode MySQL Day8</title>
<link href="http://zhouweidong.ml/2020/09/06/LeetCode-MySQL-Day8/"/>
<id>http://zhouweidong.ml/2020/09/06/LeetCode-MySQL-Day8/</id>
<published>2020-09-06T14:16:34.000Z</published>
<updated>2020-09-06T15:07:07.672Z</updated>
<content type="html"><![CDATA[<p> 第 8 天:</p><a id="more"></a><p> Write a SQL query to rank scores. If there is a tie between two scores, both should have the same ranking. Note that after a tie, the next ranking number should be the next consecutive integer value. In other words, there should be no "holes" between ranks.</p><table><thead><tr class="header"><th>Id</th><th>Score</th></tr></thead><tbody><tr class="odd"><td>1</td><td>3.50</td></tr><tr class="even"><td>2</td><td>3.65</td></tr><tr class="odd"><td>3</td><td>4.00</td></tr><tr class="even"><td>4</td><td>3.85</td></tr><tr class="odd"><td>5</td><td>4.00</td></tr><tr class="even"><td>6</td><td>3.65</td></tr></tbody></table><p> For example, given the above Scores table, your query should generate the following report (order by highest score):</p><table><thead><tr class="header"><th>score</th><th>Rank</th></tr></thead><tbody><tr class="odd"><td>4.00</td><td>1</td></tr><tr class="even"><td>4.00</td><td>1</td></tr><tr class="odd"><td>3.85</td><td>2</td></tr><tr class="even"><td>3.65</td><td>3</td></tr><tr class="odd"><td>3.65</td><td>3</td></tr><tr class="even"><td>3.50</td><td>4</td></tr></tbody></table><p> Important Note: For MySQL solutions, to escape reserved words used as column names, you can use an apostrophe before and after the keyword. For example <code>Rank</code>.</p><p> 解决方案:</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> score, <span class="keyword">dense_rank</span>() <span class="keyword">over</span>(<span class="keyword">order</span> <span class="keyword">by</span> score <span class="keyword">desc</span>) <span class="keyword">as</span> <span class="string">'Rank'</span> <span class="keyword">from</span> Scores</span><br></pre></td></tr></table></figure><p> 知识点:</p><p><img src="https://picgo-1258437747.cos.ap-nanjing.myqcloud.com/20200906224250.png"></p><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kity.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kityminder.core.min.js"></script><script defer="true" type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.js"></script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.css">]]></content>
<summary type="html">
<p> 第 8 天:</p>
</summary>
</entry>
<entry>
<title>从瑟尔沃法则看内外双循环</title>
<link href="http://zhouweidong.ml/2020/09/04/%E4%BB%8E%E7%91%9F%E5%B0%94%E6%B2%83%E6%B3%95%E5%88%99%E7%9C%8B%E5%86%85%E5%A4%96%E5%8F%8C%E5%BE%AA%E7%8E%AF/"/>
<id>http://zhouweidong.ml/2020/09/04/从瑟尔沃法则看内外双循环/</id>
<published>2020-09-04T13:58:37.000Z</published>
<updated>2020-09-04T14:46:51.400Z</updated>
<content type="html"><![CDATA[<blockquote><p> 在收支均衡约束下的哈罗德模型中,经济增长对外部需求扩张的依赖关系,被称为 “瑟尔沃法则”。<br> 令 M 为进口,X 为出口,Y 为实际 GDP,<span class="math inline">\(\mu\)</span> 为进口倾向,则在内部均衡的前提下,外部均衡为 <span class="math inline">\(X=M=\mu Y\)</span>,则有 <span class="math inline">\(Y=\frac {X}{\mu}\)</span>,得 <span class="math inline">\(g = \frac {\Delta Y}{Y} = \frac {\frac {\Delta X}{X}}{\epsilon}\)</span>。<br></p></blockquote><p> 从分子来看:外部需求的增长与经济增长存在着对应关系。<br> 从分母来看:如果要实现 “内循环”,必须降低国内循环的对外需求,即实现消费品和生产资料的进口替代。</p><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kity.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kityminder.core.min.js"></script><script defer="true" type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.js"></script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.css">]]></content>
<summary type="html">
<blockquote>
<p> 在收支均衡约束下的哈罗德模型中,经济增长对外部需求扩张的依赖关系,被称为 “瑟尔沃法则”。<br> 令 M 为进口,X 为出口,Y 为实际 GDP,<span class="math inline">\(\mu\)</span> 为进
</summary>
<category term="宏观经济" scheme="http://zhouweidong.ml/categories/%E5%AE%8F%E8%A7%82%E7%BB%8F%E6%B5%8E/"/>
</entry>
<entry>
<title>LeetCode MySql Day07</title>
<link href="http://zhouweidong.ml/2020/09/02/LeetCode-MySql-Day07/"/>
<id>http://zhouweidong.ml/2020/09/02/LeetCode-MySql-Day07/</id>
<published>2020-09-02T15:50:21.000Z</published>
<updated>2020-09-02T15:54:31.192Z</updated>
<content type="html"><![CDATA[<p> 第 7 天 <br> <a id="more"></a> Write a SQL query to get the nth highest salary from the Employee table.</p><table><thead><tr class="header"><th>Id</th><th>Salary</th></tr></thead><tbody><tr class="odd"><td>1</td><td>100</td></tr><tr class="even"><td>2</td><td>200</td></tr><tr class="odd"><td>3</td><td>300</td></tr></tbody></table><p>For example, given the above Employee table, the nth highest salary where n = 2 is 200. If there is no nth highest salary, then the query should return null.</p><table><thead><tr class="header"><th>getNthHighestSalary (2)</th></tr></thead><tbody><tr class="odd"><td>200</td></tr></tbody></table><p> 解决方法如下:<br></p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">CREATE</span> <span class="keyword">FUNCTION</span> getNthHighestSalary (N <span class="built_in">INT</span>) <span class="keyword">RETURNS</span> <span class="built_in">INT</span></span><br><span class="line"><span class="keyword">BEGIN</span></span><br><span class="line"><span class="keyword">DECLARE</span> M <span class="built_in">INT</span>;</span><br><span class="line"><span class="keyword">SET</span> M = N<span class="number">-1</span>;</span><br><span class="line"> RETURN (</span><br><span class="line"> # Write your MySQL query statement below.</span><br><span class="line"> <span class="keyword">SELECT</span> <span class="keyword">DISTINCT</span> Salary</span><br><span class="line"> <span class="keyword">FROM</span> Employee</span><br><span class="line"> <span class="keyword">ORDER</span> <span class="keyword">BY</span> Salary <span class="keyword">DESC</span></span><br><span class="line"> <span class="keyword">LIMIT</span> <span class="number">1</span> <span class="keyword">OFFSET</span> M</span><br><span class="line"> );</span><br><span class="line"><span class="keyword">END</span></span><br></pre></td></tr></table></figure><p> 这里涉及到自定义函数,基本的语法格式如下:<br> <figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">CREATE</span> <span class="keyword">FUNCTION</span> < 函数名 & gt; ( [ < 参数 < span class="number">1</span>> < 类型 < span class="number">1> [ , < 参数 < span class="number">2> < 类型 < span class="number">2>] ] … )<br><span class="line"> <span class="keyword">RETURNS</span> < 类型 & gt;</span><br><span class="line"> < 函数主体 & gt;</span><br></pre></td></tr></table></figure></p><p> 调用自定义函数的基本语法格式如下:<br> <figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> < 自定义函数名 & gt; ([< 参数 & gt; [,...]])</span><br></pre></td></tr></table></figure></p><p> 删除自定义函数的基本语法格式如下:<br> <figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">DROP</span> <span class="keyword">FUNCTION</span> [ <span class="keyword">IF</span> <span class="keyword">EXISTS</span> ] < 自定义函数名 & gt;</span><br></pre></td></tr></table></figure></p><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kity.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kityminder.core.min.js"></script><script defer="true" type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.js"></script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.css">]]></content>
<summary type="html">
<p> 第 7 天 <br>
</summary>
<category term="LeetCode" scheme="http://zhouweidong.ml/categories/LeetCode/"/>
<category term="MySQL" scheme="http://zhouweidong.ml/tags/MySQL/"/>
<category term="Function" scheme="http://zhouweidong.ml/tags/Function/"/>
</entry>
<entry>
<title>灰色预测模型</title>
<link href="http://zhouweidong.ml/2020/09/02/%E7%81%B0%E8%89%B2%E9%A2%84%E6%B5%8B%E6%A8%A1%E5%9E%8B/"/>
<id>http://zhouweidong.ml/2020/09/02/灰色预测模型/</id>
<published>2020-09-02T14:44:02.000Z</published>
<updated>2020-09-02T15:01:40.129Z</updated>
<content type="html"><![CDATA[<p> 灰色预测模型是在样本较小、信息不完全的情况下,通过挖掘数据的特征进行预测的方法。<br> <a id="more"></a> 首先需要由原始数据序列计算一次累加序列:<br></p><figure class="highlight r"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">x0 <- seq (<span class="number">1</span>, <span class="number">9</span>)</span><br><span class="line">x1 <- cumsum (x)</span><br></pre></td></tr></table></figure><p> 然后,需要建立矩阵 <span class="math display">\[\pmb {B}=\left [\begin {array}{cc} -\frac {1}{2}[x^{(1)}(2)+x^{(1)}(1)]&1\\ \vdots&\vdots\\ -\frac {1}{2}[x^{(1)}(k)+x^{(1)}(k-1)]&1 \end {array}\right]\]</span> 和 <span class="math display">\[\pmb {y}=\left [\begin {array}{c} x^{0}_{2}\\ \vdots \\ x^{0}_{k}\end {array}\right]\]</span></p><figure class="highlight r"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">B = matrix (data = <span class="number">1</span>, nrow = (length (x1) - <span class="number">1</span>), ncol = <span class="number">2</span>)</span><br><span class="line"><span class="keyword">for</span> (i <span class="keyword">in</span> <span class="number">1</span>:(length (x1) - <span class="number">1</span>)) {</span><br><span class="line"> B [i, <span class="number">1</span>] = (x1 [i] + x1 [i+<span class="number">1</span>])*(-<span class="number">1.0</span>)/<span class="number">2</span></span><br><span class="line">}</span><br><span class="line">y = as.matrix (x0 [<span class="number">2</span>:length (x0)])</span><br></pre></td></tr></table></figure><p> 之后,可以由 <span class="math inline">\(\hat {U} = (\pmb {B}^{T}\pmb {B})^{-1}\pmb {B}^{T}\pmb {y}=\left [\begin {array}{c} \hat {a} \\ \hat {u} \end {array}\right]\)</span>, 求出 <span class="math inline">\(\hat {a}\)</span> 和 <span class="math inline">\(\hat {u}\)</span>:</p><figure class="highlight r"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">BT = t (B)</span><br><span class="line">a = sovle (BT %*% B) %*% BT %*% y</span><br></pre></td></tr></table></figure><p> 最后,把 <span class="math inline">\(\hat {a}\)</span> 和 <span class="math inline">\(\hat {u}\)</span> 代入时间响应方程:</p><p><span class="math display">\[x^{(1)}(k+1) = [x^{(1)}_{1} - \frac {\hat {u}}{\hat {a}}] e^{-\hat {a} k} + \frac {\hat {u}}{\hat {a}}\]</span></p><figure class="highlight r"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">k <- rep (<span class="number">1</span>, length)</span><br><span class="line">xk <- rep (<span class="number">1</span>,(length (x0)+n))</span><br><span class="line"><span class="keyword">for</span> (i <span class="keyword">in</span> <span class="number">1</span>:length (x0)+n) {</span><br><span class="line"> u = a [<span class="number">2</span>]/a [<span class="number">1</span>]</span><br><span class="line"> xk [i] = (x1 [<span class="number">1</span>] - u)*exp ((-<span class="number">1</span>)*a [<span class="number">1</span>]*(k [i])) + u</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p> 此时得到的结果是拟合值 <span class="math inline">\(\hat {x}^{1}_{i}\)</span> ,需要利用后减运算还原为模型的拟合值 <span class="math inline">\(\hat {x}^{0}_{i}\)</span>:</p><figure class="highlight r"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">xhat <- rep (<span class="number">1</span>, length (xk))</span><br><span class="line"><span class="keyword">for</span> (i <span class="keyword">in</span> <span class="number">1</span>:legth (xk)) {</span><br><span class="line"> <span class="keyword">if</span> (i == <span class="number">1</span>) {</span><br><span class="line"> xhat [i] = x0 [<span class="number">1</span>]</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> xhat [i] = xk [i] - xk [i-<span class="number">1</span>]</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line">}</span><br></pre></td></tr></table></figure><p> 得出拟合值后,需要评价一下模型的精度,主要评价指标有:</p><ul><li>残差:<span class="math inline">\(e (k)=x^{(0)}_{k}-\hat {x}^{(0)}_{k}\)</span>。</li><li>相对残差:<span class="math inline">\(e (k)=\frac {x^{(0)}_{k}-\hat {x}^{(0)}_{k}}{x^{(0)}_{k}}\)</span>。</li><li>均方差比(后残差比值):<span class="math inline">\(C=\frac {S_{2}}{S_{1}}=\frac {\sqrt {\frac {1}{N}\sum_{k=1}^{N}[x^{(0)}_k-\bar {X}]^{2}}}{\sqrt {\frac {1}{N-1}\sum_{k=2}^{N}[E (k)-\bar {E}]^{2}}}\)</span>,小于 0.35 表示模型精度相对比较好。</li><li>小误差概率:<span class="math inline">\(P = P {|E (k)-\bar {E}|<0.6745S_{1}}\)</span>,大于 0.95 表示模型精度相对比较好。</li></ul><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kity.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kityminder.core.min.js"></script><script defer="true" type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.js"></script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.css">]]></content>
<summary type="html">
<p> 灰色预测模型是在样本较小、信息不完全的情况下,通过挖掘数据的特征进行预测的方法。<br>
</summary>
</entry>
<entry>
<title>LeetCode MySql Day6</title>
<link href="http://zhouweidong.ml/2020/05/13/LeetCode-MySql-Day06/"/>
<id>http://zhouweidong.ml/2020/05/13/LeetCode-MySql-Day06/</id>
<published>2020-05-13T11:36:38.000Z</published>
<updated>2020-05-13T13:54:21.050Z</updated>
<content type="html"><![CDATA[<p> 第六天:</p><a id="more"></a><h2 id="swap-salary">627. Swap Salary</h2><p> Given a table <code>salary</code>, such as the one below, that has m=male and f=female values. Swap all f and m values (i.e., change all f values to m and vice versa) with a <strong>single update statement</strong> and no intermediate temp table.</p><p> Note that you must write a single update statement, <strong>DO NOT</strong> write any select statement for this problem.</p><p> <strong>Example:</strong></p><table><thead><tr class="header"><th>id</th><th>name</th><th>sex</th><th>salary</th></tr></thead><tbody><tr class="odd"><td>1</td><td>A</td><td>m</td><td>2500</td></tr><tr class="even"><td>2</td><td>B</td><td>f</td><td>1500</td></tr><tr class="odd"><td>3</td><td>C</td><td>m</td><td>5500</td></tr><tr class="even"><td>4</td><td>D</td><td>f</td><td>500</td></tr></tbody></table><p> After running your <strong>update</strong> statement, the above salary table should have the following rows:</p><table><thead><tr class="header"><th>id</th><th>name</th><th>sex</th><th>salary</th></tr></thead><tbody><tr class="odd"><td>1</td><td>A</td><td>f</td><td>2500</td></tr><tr class="even"><td>2</td><td>B</td><td>m</td><td>1500</td></tr><tr class="odd"><td>3</td><td>C</td><td>f</td><td>5500</td></tr><tr class="even"><td>4</td><td>D</td><td>m</td><td>500</td></tr></tbody></table><p> 解决方法:</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">update</span> salary <span class="keyword">set</span> sex = (<span class="keyword">case</span> <span class="keyword">when</span> sex = <span class="string">"f"</span> <span class="keyword">then</span> <span class="string">"m"</span> <span class="keyword">else</span> <span class="string">"f"</span> <span class="keyword">end</span>);</span><br></pre></td></tr></table></figure><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">update</span> salary <span class="keyword">set</span> sex = <span class="built_in">char</span>(<span class="keyword">ASCII</span>(<span class="string">'f'</span>) ^ <span class="keyword">ASCII</span>(<span class="string">'m'</span>) ^ <span class="keyword">ASCII</span>(sex));</span><br></pre></td></tr></table></figure><p> 解法一:利用条件表达式函数 case when … … then … … else … … end,相当于 if … … else 。</p><p> 解法二:利用 XOR (异或) 运算符和 ASCII 编码进行转换:f 和 m 的 ASCII 编码值分别为 102 和 109 ,XOR 运算符 (^) 的作用是舍去二进制的进位,具体而言:首先将十进制转换为二进制,即 bin (102)=0b1100110,bin (109)=0b1101101,易知 0b1100110^0b110110 = 0b1011 ,再将二进制转换为十进制,即 int (str (1011), 2) = 11,同理可知 11^102=109 和 11^109 = 102 。</p><h2 id="reformat-department-table">1179. Reformat Department Table</h2><p> Table: <code>Department</code></p><table><thead><tr class="header"><th>Column Name</th><th>Type</th></tr></thead><tbody><tr class="odd"><td>id</td><td>int</td></tr><tr class="even"><td>revenue</td><td>int</td></tr><tr class="odd"><td>month</td><td>varchar</td></tr></tbody></table><p> (id, month) is the primary key of this table.</p><p> The table has information about the revenue of each department per month.</p><p> The month has values in ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"].</p><p> Write an SQL query to reformat the table such that there is a department id column and a revenue column <strong>for each month</strong>.</p><p> The query result format is in the following example:</p><p> Department table:</p><table><thead><tr class="header"><th>id</th><th>revenue</th><th>month</th></tr></thead><tbody><tr class="odd"><td>1</td><td>8000</td><td>Jan</td></tr><tr class="even"><td>2</td><td>9000</td><td>Jan</td></tr><tr class="odd"><td>3</td><td>10000</td><td>Feb</td></tr><tr class="even"><td>1</td><td>7000</td><td>Feb</td></tr><tr class="odd"><td>1</td><td>6000</td><td>Mar</td></tr></tbody></table><p> Result table:</p><table><thead><tr class="header"><th>id</th><th>Jan_Revenue</th><th>Feb_Revenue</th><th>Mar_Revenue</th><th>...</th><th>Dec_Revenue</th></tr></thead><tbody><tr class="odd"><td>1</td><td>8000</td><td>7000</td><td>6000</td><td>...</td><td>null</td></tr><tr class="even"><td>2</td><td>9000</td><td>null</td><td>null</td><td>...</td><td>null</td></tr><tr class="odd"><td>3</td><td>null</td><td>10000</td><td>null</td><td>...</td><td>null</td></tr></tbody></table><p> 解决方法:</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span></span><br><span class="line"> <span class="keyword">id</span>,</span><br><span class="line"> <span class="keyword">MAX</span>(<span class="keyword">IF</span>(<span class="keyword">month</span> = <span class="string">'Jan'</span>, revenue, <span class="literal">null</span>)) <span class="keyword">AS</span> Jan_Revenue,</span><br><span class="line"> <span class="keyword">MAX</span>(<span class="keyword">IF</span>(<span class="keyword">month</span> = <span class="string">'Feb'</span>, revenue, <span class="literal">null</span>)) <span class="keyword">AS</span> Feb_Revenue,</span><br><span class="line"> <span class="keyword">MAX</span>(<span class="keyword">IF</span>(<span class="keyword">month</span> = <span class="string">'Mar'</span>, revenue, <span class="literal">null</span>)) <span class="keyword">AS</span> Mar_Revenue,</span><br><span class="line"> <span class="keyword">MAX</span>(<span class="keyword">IF</span>(<span class="keyword">month</span> = <span class="string">'Apr'</span>, revenue, <span class="literal">null</span>)) <span class="keyword">AS</span> Apr_Revenue,</span><br><span class="line"> <span class="keyword">MAX</span>(<span class="keyword">IF</span>(<span class="keyword">month</span> = <span class="string">'May'</span>, revenue, <span class="literal">null</span>)) <span class="keyword">AS</span> May_Revenue,</span><br><span class="line"> <span class="keyword">MAX</span>(<span class="keyword">IF</span>(<span class="keyword">month</span> = <span class="string">'Jun'</span>, revenue, <span class="literal">null</span>)) <span class="keyword">AS</span> Jun_Revenue,</span><br><span class="line"> <span class="keyword">MAX</span>(<span class="keyword">IF</span>(<span class="keyword">month</span> = <span class="string">'Jul'</span>, revenue, <span class="literal">null</span>)) <span class="keyword">AS</span> Jul_Revenue,</span><br><span class="line"> <span class="keyword">MAX</span>(<span class="keyword">IF</span>(<span class="keyword">month</span> = <span class="string">'Aug'</span>, revenue, <span class="literal">null</span>)) <span class="keyword">AS</span> Aug_Revenue,</span><br><span class="line"> <span class="keyword">MAX</span>(<span class="keyword">IF</span>(<span class="keyword">month</span> = <span class="string">'Sep'</span>, revenue, <span class="literal">null</span>)) <span class="keyword">AS</span> Sep_Revenue,</span><br><span class="line"> <span class="keyword">MAX</span>(<span class="keyword">IF</span>(<span class="keyword">month</span> = <span class="string">'Oct'</span>, revenue, <span class="literal">null</span>)) <span class="keyword">AS</span> Oct_Revenue,</span><br><span class="line"> <span class="keyword">MAX</span>(<span class="keyword">IF</span>(<span class="keyword">month</span> = <span class="string">'Nov'</span>, revenue, <span class="literal">null</span>)) <span class="keyword">AS</span> Nov_Revenue,</span><br><span class="line"> <span class="keyword">MAX</span>(<span class="keyword">IF</span>(<span class="keyword">month</span> = <span class="string">'Dec'</span>, revenue, <span class="literal">null</span>)) <span class="keyword">AS</span> Dec_Revenue</span><br><span class="line"><span class="keyword">FROM</span> Department</span><br><span class="line"><span class="keyword">GROUP</span> <span class="keyword">BY</span> <span class="keyword">id</span></span><br><span class="line"><span class="keyword">ORDER</span> <span class="keyword">BY</span> <span class="keyword">id</span>;</span><br></pre></td></tr></table></figure><p> 知识点:if (expr1,expr2,expr3),如果 expr1 是 TRUE (expr1 <> 0 and expr1 <> NULL),则 IF () 的返回值为 expr2; 否则返回值则为 expr3。</p><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kity.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kityminder.core.min.js"></script><script defer="true" type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.js"></script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.css">]]></content>
<summary type="html">
<p> 第六天:</p>
</summary>
<category term="LeetCode" scheme="http://zhouweidong.ml/categories/LeetCode/"/>
<category term="LeetCode" scheme="http://zhouweidong.ml/tags/LeetCode/"/>
<category term="MySql" scheme="http://zhouweidong.ml/tags/MySql/"/>
</entry>
<entry>
<title>LeetCode MySql Day5</title>
<link href="http://zhouweidong.ml/2020/05/12/LeetCode-MySql-Day5/"/>
<id>http://zhouweidong.ml/2020/05/12/LeetCode-MySql-Day5/</id>
<published>2020-05-12T12:38:05.000Z</published>
<updated>2020-05-12T13:28:06.264Z</updated>
<content type="html"><![CDATA[<p> 第五天:</p><a id="more"></a><h2 id="classes-more-than-5-students">596. Classes More Than 5 Students</h2><p> There is a table <code>courses</code> with columns: <strong>student</strong> and <strong>class</strong></p><p> Please list out all classes which have more than or equal to 5 students.</p><p> For example, the table:</p><table><thead><tr class="header"><th>student</th><th>class</th></tr></thead><tbody><tr class="odd"><td>A</td><td>Math</td></tr><tr class="even"><td>B</td><td>English</td></tr><tr class="odd"><td>C</td><td>Math</td></tr><tr class="even"><td>D</td><td>Biology</td></tr><tr class="odd"><td>E</td><td>Math</td></tr><tr class="even"><td>F</td><td>Computer</td></tr><tr class="odd"><td>G</td><td>Math</td></tr><tr class="even"><td>H</td><td>Math</td></tr><tr class="odd"><td>I</td><td>Math</td></tr></tbody></table><p> Should output:</p><table><thead><tr class="header"><th>class</th></tr></thead><tbody><tr class="odd"><td>Math</td></tr></tbody></table><p> <strong>Note:</strong></p><p> The students should not be counted duplicate in each course.</p><p> 解决方法:</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="keyword">class</span> <span class="keyword">from</span> courses <span class="keyword">group</span> <span class="keyword">by</span> <span class="keyword">class</span> <span class="keyword">having</span> <span class="keyword">count</span>(<span class="keyword">distinct</span> student) >= <span class="number">5</span>;</span><br></pre></td></tr></table></figure><h2 id="not-boring-movies">620. Not Boring Movies</h2><p> X city opened a new cinema, many people would like to go to this cinema. The cinema also gives out a poster indicating the movies’ ratings and descriptions.</p><p> Please write a SQL query to output movies with an odd numbered ID and a description that is not 'boring'. Order the result by rating.</p><p> For example, table <code>cinema</code>:</p><table><thead><tr class="header"><th>id</th><th>movie</th><th>description</th><th>rating</th></tr></thead><tbody><tr class="odd"><td>1</td><td>War</td><td>great 3D</td><td>8.9</td></tr><tr class="even"><td>2</td><td>Science</td><td>fiction</td><td>8.5</td></tr><tr class="odd"><td>3</td><td>irish</td><td>boring</td><td>6.2</td></tr><tr class="even"><td>4</td><td>Ice song</td><td>Fantacy</td><td>8.6</td></tr><tr class="odd"><td>5</td><td>House card</td><td>Interesting</td><td>9.1</td></tr></tbody></table><p> For the example above, the output should be:</p><table><thead><tr class="header"><th>id</th><th>movie</th><th>description</th><th>rating</th></tr></thead><tbody><tr class="odd"><td>5</td><td>House card</td><td>Interesting</td><td>9.1</td></tr><tr class="even"><td>1</td><td>War</td><td>great 3D</td><td>8.9</td></tr></tbody></table><p> 解决方法:</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> cinema <span class="keyword">where</span> <span class="keyword">mod</span>(<span class="keyword">id</span>, <span class="number">2</span>) != <span class="number">0</span> <span class="keyword">and</span> description <span class="keyword">not</span> <span class="keyword">like</span> <span class="string">'boring'</span> <span class="keyword">order</span> <span class="keyword">by</span> rating <span class="keyword">DESC</span>;</span><br></pre></td></tr></table></figure><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> * <span class="keyword">from</span> cinema <span class="keyword">where</span> description <> <span class="string">'boring'</span> <span class="keyword">and</span> <span class="keyword">id</span> & <span class="number">1</span> = <span class="number">1</span> <span class="keyword">order</span> <span class="keyword">by</span> rating <span class="keyword">DESC</span></span><br></pre></td></tr></table></figure><p> 两个知识点:对于字符串 not like 是模糊查询,<> 则是完整查询,二者的区别是前者要求数据里面不包含某个字段,后者要求数据不是某个字段;& 是位运算符,可以用于判断对 <span class="math inline">\(2^n\)</span> 取余,是一个取巧的办法,加快了运算速度。</p><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kity.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kityminder.core.min.js"></script><script defer="true" type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.js"></script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.css">]]></content>
<summary type="html">
<p> 第五天:</p>
</summary>
<category term="LeetCode" scheme="http://zhouweidong.ml/categories/LeetCode/"/>
<category term="LeetCode" scheme="http://zhouweidong.ml/tags/LeetCode/"/>
<category term="MySql" scheme="http://zhouweidong.ml/tags/MySql/"/>
</entry>
<entry>
<title>LeetCode MySql Day4</title>
<link href="http://zhouweidong.ml/2020/05/08/LeetCode-MySql-Day4/"/>
<id>http://zhouweidong.ml/2020/05/08/LeetCode-MySql-Day4/</id>
<published>2020-05-08T04:57:53.000Z</published>
<updated>2020-05-08T05:40:29.680Z</updated>
<content type="html"><![CDATA[<p> 第四天:</p><a id="more"></a><h2 id="rising-temperature">197. Rising Temperature</h2><p> Given a Weather table, write a SQL query to find all dates' Ids with higher temperature compared to its previous (yesterday's) dates.</p><table><thead><tr class="header"><th>Id (INT)</th><th>RecordDate (DATE)</th><th>Temperature (INT)</th></tr></thead><tbody><tr class="odd"><td>1</td><td>2015-01-01</td><td>10</td></tr><tr class="even"><td>2</td><td>2015-01-02</td><td>25</td></tr><tr class="odd"><td>3</td><td>2015-01-03</td><td>20</td></tr><tr class="even"><td>4</td><td>2015-01-04</td><td>30</td></tr></tbody></table><p> For example, return the following Ids for the above Weather table:title: LeetCode MySql Day4</p><table><thead><tr class="header"><th style="text-align: left;">Id</th></tr></thead><tbody><tr class="odd"><td style="text-align: left;">2</td></tr><tr class="even"><td style="text-align: left;">4</td></tr></tbody></table><p> 解决方法:</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> w1.Id <span class="keyword">from</span> Weather w1, Weather w2 <span class="keyword">where</span> w1.Temperature > w2.Temperature <span class="keyword">and</span> <span class="keyword">DateDiff</span>(w1. RecordDate, w2.RecordDate) = <span class="number">1</span>;</span><br></pre></td></tr></table></figure><p> 知识点:时间类型的处理 - DataDiff (d1, d2):计算日期 d1->d2 之间相隔的天数。</p><h2 id="big-countries">595. Big Countries</h2><p> There is a table World:</p><table><thead><tr class="header"><th>name</th><th>continent</th><th>area</th><th>population</th><th>gdp</th></tr></thead><tbody><tr class="odd"><td>Afghanistan</td><td>Asia</td><td>652230</td><td>25500100</td><td>20343000</td></tr><tr class="even"><td>Albania</td><td>Europe</td><td>28748</td><td>2831741</td><td>12960000</td></tr><tr class="odd"><td>Algeria</td><td>Africa</td><td>2381741</td><td>37100000</td><td>1.89E+08</td></tr><tr class="even"><td>Andorra</td><td>Europe</td><td>468</td><td>78115</td><td>3712000</td></tr><tr class="odd"><td>Angola</td><td>Africa</td><td>1246700</td><td>20609294</td><td>1.01E+08</td></tr></tbody></table><p> A country is big if it has an area of bigger than 3 million square km or a population of more than 25 million.</p><p> Write a SQL solution to output big countries' name, population and area.</p><p> For example, according to the above table, we should output:</p><table><thead><tr class="header"><th>name</th><th>population</th><th>area</th></tr></thead><tbody><tr class="odd"><td>Afghanistan</td><td>25500100</td><td>652230</td></tr><tr class="even"><td>Algeria</td><td>37100000</td><td>2381741</td></tr></tbody></table><p> 解决方法:</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="keyword">name</span>, population, area <span class="keyword">from</span> World <span class="keyword">where</span> population > <span class="number">25000000</span> <span class="keyword">or</span> area > <span class="number">3000000</span>;</span><br></pre></td></tr></table></figure><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="keyword">name</span>, population, area <span class="keyword">from</span> World <span class="keyword">where</span> population > <span class="number">25000000</span> <span class="keyword">union</span> <span class="keyword">select</span> <span class="keyword">name</span>, population, area <span class="keyword">from</span> World <span class="keyword">where</span> area > <span class="number">3000000</span>;</span><br></pre></td></tr></table></figure><p> union 替代 or 虽然看起来比较繁琐,但会加快运算速度。</p><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kity.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kityminder.core.min.js"></script><script defer="true" type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.js"></script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.css">]]></content>
<summary type="html">
<p> 第四天:</p>
</summary>
<category term="LeetCode" scheme="http://zhouweidong.ml/categories/LeetCode/"/>
<category term="LeetCode" scheme="http://zhouweidong.ml/tags/LeetCode/"/>
<category term="MySql" scheme="http://zhouweidong.ml/tags/MySql/"/>
</entry>
<entry>
<title>LeetCode MySql Day3</title>
<link href="http://zhouweidong.ml/2020/05/07/LeetCode-MySql-Day3/"/>
<id>http://zhouweidong.ml/2020/05/07/LeetCode-MySql-Day3/</id>
<published>2020-05-07T04:48:04.000Z</published>
<updated>2020-09-03T12:52:27.461Z</updated>
<content type="html"><![CDATA[<p> 第三天:</p><a id="more"></a><h2 id="customers-who-never-order">183. Customers Who Never Order</h2><p> Suppose that a website contains two tables, the Customers table and the Orders table. Write a SQL query to find all customers who never order anything.</p><p> Table: Customers.</p><table><thead><tr class="header"><th>Id</th><th>Name</th></tr></thead><tbody><tr class="odd"><td>1</td><td>Joe</td></tr><tr class="even"><td>2</td><td>Henry</td></tr><tr class="odd"><td>3</td><td>Sam</td></tr><tr class="even"><td>4</td><td>Max</td></tr></tbody></table><p> Table: Orders.</p><table><thead><tr class="header"><th>Id</th><th>CustomerId</th></tr></thead><tbody><tr class="odd"><td>1</td><td>3</td></tr><tr class="even"><td>2</td><td>1</td></tr></tbody></table><p>Using the above tables as example, return the following:</p><table><thead><tr class="header"><th>Customers</th></tr></thead><tbody><tr class="odd"><td>Henry</td></tr><tr class="even"><td>Max</td></tr></tbody></table><p> 解决方法:</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="keyword">Name</span> <span class="keyword">as</span> Customers <span class="keyword">from</span> Customers <span class="keyword">where</span> <span class="keyword">Id</span> <span class="keyword">not</span> <span class="keyword">in</span> (<span class="keyword">select</span> CustimerId <span class="keyword">from</span> Orders);</span><br></pre></td></tr></table></figure><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> c.Name <span class="keyword">from</span> Customers c <span class="keyword">left</span> <span class="keyword">join</span> Orders o <span class="keyword">on</span> c.Id = o.CustormerId <span class="keyword">where</span> o.CustomerId <span class="keyword">is</span> <span class="literal">NULL</span>;</span><br></pre></td></tr></table></figure><p> 前者的逻辑清晰,但对于大数据而言运算速度慢于后者。</p><h2 id="delete-duplicate-emails">196. Delete Duplicate Emails</h2><p> Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique emails based on its smallest Id.</p><table><thead><tr class="header"><th>Id</th><th>Email</th></tr></thead><tbody><tr class="odd"><td>1</td><td>[email protected]</td></tr><tr class="even"><td>2</td><td>[email protected]</td></tr><tr class="odd"><td>3</td><td>[email protected]</td></tr></tbody></table><p> Id is the primary key column for this table.</p><p> For example, after running your query, the above Person table should have the following rows:</p><table><thead><tr class="header"><th>Id</th><th>Email</th></tr></thead><tbody><tr class="odd"><td>1</td><td>[email protected]</td></tr><tr class="even"><td>2</td><td>[email protected]</td></tr></tbody></table><p> 解决方法:</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">delete</span> p1 <span class="keyword">from</span> Person p1, Person p2 <span class="keyword">where</span> p1.Email = p2.Email <span class="keyword">and</span> p1.Id > p2.Id;</span><br></pre></td></tr></table></figure><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">delete</span> <span class="keyword">from</span> Person <span class="keyword">where</span> <span class="keyword">Id</span> <span class="keyword">not</span> <span class="keyword">in</span> (<span class="keyword">select</span> * <span class="keyword">from</span> (<span class="keyword">select</span> <span class="keyword">min</span>(<span class="keyword">id</span>) <span class="keyword">from</span> Person <span class="keyword">group</span> <span class="keyword">by</span> Email) <span class="keyword">as</span> p);</span><br></pre></td></tr></table></figure><p> 前者的逻辑相对比较清晰;后者的逻辑是:无论是否重复,利用 group by 分组 求 Id 的最小值,即需要保留的数据,然后利用 not in 删除剩余部分。</p><p> 值得注意的是:利用 delete 语句删除数据前,务必先使用 select 语句确定删除的数据是否正确,避免误删。</p><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kity.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/kityminder.core.min.js"></script><script defer="true" type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.js"></script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/mindmap.min.css">]]></content>
<summary type="html">
<p> 第三天:</p>
</summary>
<category term="LeetCode" scheme="http://zhouweidong.ml/categories/LeetCode/"/>
<category term="LeetCode" scheme="http://zhouweidong.ml/tags/LeetCode/"/>
<category term="MySql" scheme="http://zhouweidong.ml/tags/MySql/"/>
</entry>
</feed>