回答編集履歴
2
追記
test
CHANGED
@@ -47,3 +47,27 @@
|
|
47
47
|
日曜を先頭とする場合は「%X%V」で集計します。
|
48
48
|
|
49
49
|
X年のV週目が集計されます。
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
# 追記
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
週の頭を日付でほしいならこうしてください
|
58
|
+
|
59
|
+
```ここに言語を入力
|
60
|
+
|
61
|
+
select *
|
62
|
+
|
63
|
+
,date_format(d,'%Y-%m-01') as m
|
64
|
+
|
65
|
+
,date_format(d,'%X-%V') as w1
|
66
|
+
|
67
|
+
,d - interval date_format(d,'%w') day as w2
|
68
|
+
|
69
|
+
from tbl
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
```
|
1
sample
test
CHANGED
@@ -1,3 +1,49 @@
|
|
1
1
|
週別、月別の集計頻度が高く、データが相当数になる場合は、それを前提にテーブルを設計します。
|
2
2
|
|
3
3
|
各レコードに週初の日付および、月初の日付を埋め込んで、集計項目と複合インデックスを貼っておきます
|
4
|
+
|
5
|
+
|
6
|
+
|
7
|
+
# sample
|
8
|
+
|
9
|
+
- 元データ
|
10
|
+
|
11
|
+
```SQL
|
12
|
+
|
13
|
+
create table tbl(id int unique,d date);
|
14
|
+
|
15
|
+
insert into tbl values
|
16
|
+
|
17
|
+
(1,'2016-12-31'),
|
18
|
+
|
19
|
+
(2,'2017-01-01'),
|
20
|
+
|
21
|
+
(3,'2017-01-31'),
|
22
|
+
|
23
|
+
(4,'2017-02-01'),
|
24
|
+
|
25
|
+
(5,'2017-12-01'),
|
26
|
+
|
27
|
+
(6,'2017-12-31'),
|
28
|
+
|
29
|
+
(7,'2018-01-01');
|
30
|
+
|
31
|
+
```
|
32
|
+
|
33
|
+
- 集計
|
34
|
+
|
35
|
+
```SQL
|
36
|
+
|
37
|
+
select *
|
38
|
+
|
39
|
+
,date_format(d,'%Y-%m-01') as m
|
40
|
+
|
41
|
+
,date_format(d,'%X%V') as w
|
42
|
+
|
43
|
+
from tbl
|
44
|
+
|
45
|
+
```
|
46
|
+
|
47
|
+
日曜を先頭とする場合は「%X%V」で集計します。
|
48
|
+
|
49
|
+
X年のV週目が集計されます。
|