質問編集履歴
5
解決方法
test
CHANGED
File without changes
|
test
CHANGED
@@ -75,3 +75,49 @@
|
|
75
75
|
;
|
76
76
|
|
77
77
|
```
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
<11/29 追記2>
|
84
|
+
|
85
|
+
@gpsoft様のクエリを参考にさせていただきました。
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
```SQL
|
90
|
+
|
91
|
+
SELECT A.ID
|
92
|
+
|
93
|
+
, A.EVENT_DATE
|
94
|
+
|
95
|
+
, COUNT(B.ID) AS FREQ_PAST_1YEAR
|
96
|
+
|
97
|
+
FROM Tbl A
|
98
|
+
|
99
|
+
LEFT JOIN Tbl B
|
100
|
+
|
101
|
+
ON A.ID = B.ID
|
102
|
+
|
103
|
+
AND B.EVENT_DATE < A.EVENT_DATE
|
104
|
+
|
105
|
+
AND B.EVENT_DATE > ADD_MONTHS(A.EVENT_DATE, -12)
|
106
|
+
|
107
|
+
GROUP BY A.ID, A.EVENT_DATE
|
108
|
+
|
109
|
+
ORDER BY A.ID, A.EVENT_DATE
|
110
|
+
|
111
|
+
;
|
112
|
+
|
113
|
+
```
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
@gpsoft様、@sazi様、@Orlofsky様
|
118
|
+
|
119
|
+
初心者にもかかわらずご丁寧に教えていただき、大変勉強になりました。
|
120
|
+
|
121
|
+
今後質問させていただく時は、Markdownやタグ付けの徹底など留意させていただきます。
|
122
|
+
|
123
|
+
どうぞよろしくお願い申し上げます。
|
4
SQL server→ Oracleへ変更
test
CHANGED
File without changes
|
test
CHANGED
File without changes
|
3
DATEに修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -32,43 +32,43 @@
|
|
32
32
|
|
33
33
|
```SQL
|
34
34
|
|
35
|
-
CREATE TABLE
|
35
|
+
CREATE TABLE TBL
|
36
36
|
|
37
|
-
("ID" varchar2(1), "
|
37
|
+
("ID" varchar2(1), "EVENT_DATE" timestamp)
|
38
38
|
|
39
39
|
;
|
40
40
|
|
41
41
|
|
42
42
|
|
43
|
-
INSERT ALL
|
43
|
+
INSERT ALL
|
44
44
|
|
45
|
-
INTO
|
45
|
+
INTO TBL ("ID", "EVENT_DATE")
|
46
46
|
|
47
|
-
VALUES ('A', 2015
|
47
|
+
VALUES ('A', '01-Jan-2015 12:00:00 AM')
|
48
48
|
|
49
|
-
INTO
|
49
|
+
INTO TBL ("ID", "EVENT_DATE")
|
50
50
|
|
51
|
-
VALUES ('A', 2015
|
51
|
+
VALUES ('A', '03-Feb-2015 12:00:00 AM')
|
52
52
|
|
53
|
-
INTO
|
53
|
+
INTO TBL ("ID", "EVENT_DATE")
|
54
54
|
|
55
|
-
VALUES ('A', 20150
|
55
|
+
VALUES ('A', '03-Aug-2015 12:00:00 AM')
|
56
56
|
|
57
|
-
INTO
|
57
|
+
INTO TBL ("ID", "EVENT_DATE")
|
58
58
|
|
59
|
-
VALUES ('A', 2016
|
59
|
+
VALUES ('A', '01-Feb-2016 12:00:00 AM')
|
60
60
|
|
61
|
-
INTO
|
61
|
+
INTO TBL ("ID", "EVENT_DATE")
|
62
62
|
|
63
|
-
VALUES ('B', 20130
|
63
|
+
VALUES ('B', '01-Apr-2013 12:00:00 AM')
|
64
64
|
|
65
|
-
INTO
|
65
|
+
INTO TBL ("ID", "EVENT_DATE")
|
66
66
|
|
67
|
-
VALUES ('B', 20140
|
67
|
+
VALUES ('B', '01-May-2014 12:00:00 AM')
|
68
68
|
|
69
|
-
INTO
|
69
|
+
INTO TBL ("ID", "EVENT_DATE")
|
70
70
|
|
71
|
-
VALUES ('B', 20150
|
71
|
+
VALUES ('B', '31-Mar-2015 12:00:00 AM')
|
72
72
|
|
73
73
|
SELECT * FROM dual
|
74
74
|
|
2
markdown
test
CHANGED
File without changes
|
test
CHANGED
@@ -29,6 +29,8 @@
|
|
29
29
|
<2020/11/29追記>
|
30
30
|
|
31
31
|
サンプルデータは以下で作成いたしました。
|
32
|
+
|
33
|
+
```SQL
|
32
34
|
|
33
35
|
CREATE TABLE event
|
34
36
|
|
@@ -71,3 +73,5 @@
|
|
71
73
|
SELECT * FROM dual
|
72
74
|
|
73
75
|
;
|
76
|
+
|
77
|
+
```
|
1
サンプルデータのコード
test
CHANGED
File without changes
|
test
CHANGED
@@ -21,3 +21,53 @@
|
|
21
21
|
となっています。
|
22
22
|
|
23
23
|
(うるう年の処理が困難な場合は、365日と定義しても差し支えないと考えています)
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
<2020/11/29追記>
|
30
|
+
|
31
|
+
サンプルデータは以下で作成いたしました。
|
32
|
+
|
33
|
+
CREATE TABLE event
|
34
|
+
|
35
|
+
("ID" varchar2(1), "event_date" int)
|
36
|
+
|
37
|
+
;
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
INSERT ALL
|
42
|
+
|
43
|
+
INTO event ("ID", "event_date")
|
44
|
+
|
45
|
+
VALUES ('A', 20150101)
|
46
|
+
|
47
|
+
INTO event ("ID", "event_date")
|
48
|
+
|
49
|
+
VALUES ('A', 20150203)
|
50
|
+
|
51
|
+
INTO event ("ID", "event_date")
|
52
|
+
|
53
|
+
VALUES ('A', 20150803)
|
54
|
+
|
55
|
+
INTO event ("ID", "event_date")
|
56
|
+
|
57
|
+
VALUES ('A', 20160201)
|
58
|
+
|
59
|
+
INTO event ("ID", "event_date")
|
60
|
+
|
61
|
+
VALUES ('B', 20130401)
|
62
|
+
|
63
|
+
INTO event ("ID", "event_date")
|
64
|
+
|
65
|
+
VALUES ('B', 20140501)
|
66
|
+
|
67
|
+
INTO event ("ID", "event_date")
|
68
|
+
|
69
|
+
VALUES ('B', 20150331)
|
70
|
+
|
71
|
+
SELECT * FROM dual
|
72
|
+
|
73
|
+
;
|