回答編集履歴
4
追加
test
CHANGED
@@ -79,3 +79,49 @@
|
|
79
79
|
using (card_no);
|
80
80
|
|
81
81
|
```
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
要件をどのように考えたかをご説明します。下記の解釈に従って作成しました。
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
①idが重複せず 最新の日付のflgが0のデータを取得。
|
92
|
+
|
93
|
+
card_noは、重複していて、その中より最新で、かつフラグが0のデータを抽出します。
|
94
|
+
|
95
|
+
select t1.* from abc t1 join (select card_no, max(ymd) ymd from abc where flg='0' group by 1) t2
|
96
|
+
|
97
|
+
on t1.card_no=t2.card_no and t1.ymd=t2.ymd and t1.flg='0') t3
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
②idが重複せず 現在の西暦月から-15ヶ月引いた西暦の4月1日から4月11日までの間で日付が12日に一番近いflgが1のデータを取得
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
現在の西暦月は、プログラムで設定する値と想像して、SQL中はコンスタントの値として'20170821133000'を使っています。
|
106
|
+
|
107
|
+
15ヶ月引いた西暦を求めて、その年の4月1日から11日のものを抽出します。
|
108
|
+
|
109
|
+
※日付の場合、CAST(::date)して数字を日付型に変換してから扱います。
|
110
|
+
|
111
|
+
・15ヶ月引いた西暦
|
112
|
+
|
113
|
+
to_char((substr('20170821133000',1,8))::date - interval '15 month','YYYY')
|
114
|
+
|
115
|
+
・上記西暦の4月1日
|
116
|
+
|
117
|
+
(to_char(…)||'0401')::date
|
118
|
+
|
119
|
+
・同様に
|
120
|
+
|
121
|
+
(to_char(…)||'0411')::date
|
122
|
+
|
123
|
+
・4月1日から11日中で同じcard_noで一番大きい日付を抽出するします( flg='1')
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
③ ①と②を①基準でjoinする。
|
3
訂正
test
CHANGED
@@ -70,7 +70,7 @@
|
|
70
70
|
|
71
71
|
ymd between (to_char((substr('20170821133000',1,8))::date - interval '15 month','YYYY')||'0401')::date
|
72
72
|
|
73
|
-
and ( to_char((substr('20170821133000',1,8))::date - interval '15 month','YYYY')||'041
|
73
|
+
and ( to_char((substr('20170821133000',1,8))::date - interval '15 month','YYYY')||'0411')::date
|
74
74
|
|
75
75
|
group by 1) t4
|
76
76
|
|
2
訂正
test
CHANGED
@@ -50,7 +50,7 @@
|
|
50
50
|
|
51
51
|
|
52
52
|
|
53
|
-
変更しました
|
53
|
+
変更しました,修正しました2
|
54
54
|
|
55
55
|
```c
|
56
56
|
|
@@ -68,9 +68,9 @@
|
|
68
68
|
|
69
69
|
(select card_no, max(ymd) ymd from abc where flg='1' and
|
70
70
|
|
71
|
-
ymd between (substr('20170821133000',1,
|
71
|
+
ymd between (to_char((substr('20170821133000',1,8))::date - interval '15 month','YYYY')||'0401')::date
|
72
72
|
|
73
|
-
and (substr('20170821133000',1,
|
73
|
+
and ( to_char((substr('20170821133000',1,8))::date - interval '15 month','YYYY')||'0412')::date
|
74
74
|
|
75
75
|
group by 1) t4
|
76
76
|
|
1
変更
test
CHANGED
@@ -47,3 +47,35 @@
|
|
47
47
|
|
48
48
|
|
49
49
|
```
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
変更しました
|
54
|
+
|
55
|
+
```c
|
56
|
+
|
57
|
+
select t3.card_no, t3.rank, t3.ymd, t3.flg,
|
58
|
+
|
59
|
+
t6.rank, t6.ymd, t6.flg from
|
60
|
+
|
61
|
+
(select t1.* from abc t1 join (select card_no, max(ymd) ymd from abc where flg='0' group by 1) t2
|
62
|
+
|
63
|
+
on t1.card_no=t2.card_no and t1.ymd=t2.ymd and t1.flg='0') t3
|
64
|
+
|
65
|
+
left join
|
66
|
+
|
67
|
+
(select t5.* from abc t5 join
|
68
|
+
|
69
|
+
(select card_no, max(ymd) ymd from abc where flg='1' and
|
70
|
+
|
71
|
+
ymd between (substr('20170821133000',1,4)||'0401')::date - interval '15 month'
|
72
|
+
|
73
|
+
and (substr('20170821133000',1,4)||'0412')::date - interval '15 month'
|
74
|
+
|
75
|
+
group by 1) t4
|
76
|
+
|
77
|
+
on t5.card_no=t4.card_no and t5.ymd=t4.ymd and t5. flg='1') t6
|
78
|
+
|
79
|
+
using (card_no);
|
80
|
+
|
81
|
+
```
|