teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

4

追加

2017/01/08 02:54

投稿

A.Ichi
A.Ichi

スコア4070

answer CHANGED
@@ -38,4 +38,27 @@
38
38
  group by 1) t4
39
39
  on t5.card_no=t4.card_no and t5.ymd=t4.ymd and t5. flg='1') t6
40
40
  using (card_no);
41
- ```
41
+ ```
42
+
43
+
44
+ 要件をどのように考えたかをご説明します。下記の解釈に従って作成しました。
45
+
46
+ ①idが重複せず 最新の日付のflgが0のデータを取得。
47
+  card_noは、重複していて、その中より最新で、かつフラグが0のデータを抽出します。
48
+  select t1.* from abc t1 join (select card_no, max(ymd) ymd from abc where flg='0' group by 1) t2
49
+  on t1.card_no=t2.card_no and t1.ymd=t2.ymd and t1.flg='0') t3
50
+
51
+ ②idが重複せず 現在の西暦月から-15ヶ月引いた西暦の4月1日から4月11日までの間で日付が12日に一番近いflgが1のデータを取得
52
+
53
+  現在の西暦月は、プログラムで設定する値と想像して、SQL中はコンスタントの値として'20170821133000'を使っています。
54
+  15ヶ月引いた西暦を求めて、その年の4月1日から11日のものを抽出します。
55
+  ※日付の場合、CAST(::date)して数字を日付型に変換してから扱います。
56
+ ・15ヶ月引いた西暦
57
+  to_char((substr('20170821133000',1,8))::date - interval '15 month','YYYY')
58
+ ・上記西暦の4月1日
59
+  (to_char(…)||'0401')::date
60
+ ・同様に
61
+  (to_char(…)||'0411')::date
62
+ ・4月1日から11日中で同じcard_noで一番大きい日付を抽出するします( flg='1') 
63
+  
64
+ ③ ①と②を①基準でjoinする。

3

訂正

2017/01/08 02:54

投稿

A.Ichi
A.Ichi

スコア4070

answer CHANGED
@@ -34,7 +34,7 @@
34
34
  (select t5.* from abc t5 join
35
35
  (select card_no, max(ymd) ymd from abc where flg='1' and
36
36
  ymd between (to_char((substr('20170821133000',1,8))::date - interval '15 month','YYYY')||'0401')::date
37
- and ( to_char((substr('20170821133000',1,8))::date - interval '15 month','YYYY')||'0412')::date
37
+ and ( to_char((substr('20170821133000',1,8))::date - interval '15 month','YYYY')||'0411')::date
38
38
  group by 1) t4
39
39
  on t5.card_no=t4.card_no and t5.ymd=t4.ymd and t5. flg='1') t6
40
40
  using (card_no);

2

訂正

2017/01/08 02:43

投稿

A.Ichi
A.Ichi

スコア4070

answer CHANGED
@@ -24,7 +24,7 @@
24
24
 
25
25
  ```
26
26
 
27
- 変更しました
27
+ 変更しました,修正しました2
28
28
  ```c
29
29
  select t3.card_no, t3.rank, t3.ymd, t3.flg,
30
30
  t6.rank, t6.ymd, t6.flg from
@@ -33,8 +33,8 @@
33
33
  left join
34
34
  (select t5.* from abc t5 join
35
35
  (select card_no, max(ymd) ymd from abc where flg='1' and
36
- ymd between (substr('20170821133000',1,4)||'0401')::date - interval '15 month'
36
+ ymd between (to_char((substr('20170821133000',1,8))::date - interval '15 month','YYYY')||'0401')::date
37
- and (substr('20170821133000',1,4)||'0412')::date - interval '15 month'
37
+ and ( to_char((substr('20170821133000',1,8))::date - interval '15 month','YYYY')||'0412')::date
38
38
  group by 1) t4
39
39
  on t5.card_no=t4.card_no and t5.ymd=t4.ymd and t5. flg='1') t6
40
40
  using (card_no);

1

変更

2017/01/08 02:33

投稿

A.Ichi
A.Ichi

スコア4070

answer CHANGED
@@ -22,4 +22,20 @@
22
22
  (3 rows)
23
23
 
24
24
 
25
+ ```
26
+
27
+ 変更しました
28
+ ```c
29
+ select t3.card_no, t3.rank, t3.ymd, t3.flg,
30
+ t6.rank, t6.ymd, t6.flg from
31
+ (select t1.* from abc t1 join (select card_no, max(ymd) ymd from abc where flg='0' group by 1) t2
32
+ on t1.card_no=t2.card_no and t1.ymd=t2.ymd and t1.flg='0') t3
33
+ left join
34
+ (select t5.* from abc t5 join
35
+ (select card_no, max(ymd) ymd from abc where flg='1' and
36
+ ymd between (substr('20170821133000',1,4)||'0401')::date - interval '15 month'
37
+ and (substr('20170821133000',1,4)||'0412')::date - interval '15 month'
38
+ group by 1) t4
39
+ on t5.card_no=t4.card_no and t5.ymd=t4.ymd and t5. flg='1') t6
40
+ using (card_no);
25
41
  ```