回答編集履歴
3
ご希望の動作になるよう、SQL文を変更
test
CHANGED
@@ -4,7 +4,7 @@
|
|
4
4
|
|
5
5
|
|
6
6
|
|
7
|
-
### 2021/08/19 2
|
7
|
+
### 2021/08/19 22:21 のコメントに基づき、SQL文を編集
|
8
8
|
|
9
9
|
~~**model 毎に 最新の insert_date に該当しなくなったレコード**~~
|
10
10
|
|
@@ -12,11 +12,21 @@
|
|
12
12
|
|
13
13
|
~~ということですよね。~~
|
14
14
|
|
15
|
-
↓
|
15
|
+
↓
|
16
16
|
|
17
|
-
|
17
|
+
- model
|
18
18
|
|
19
|
+
- color_code
|
20
|
+
|
21
|
+
- color_name
|
22
|
+
|
23
|
+
- size_code
|
24
|
+
|
25
|
+
- size_name
|
26
|
+
|
27
|
+
毎に
|
28
|
+
|
19
|
-
の ```del_flg```列 を ```1``` に更新
|
29
|
+
最古の insert_date に該当しないレコードの ```del_flg``` 列 を ```1``` に更新
|
20
30
|
|
21
31
|
というロジックに変更。
|
22
32
|
|
@@ -48,7 +58,11 @@
|
|
48
58
|
|
49
59
|
, coalesce( color_code, N'' )
|
50
60
|
|
61
|
+
, coalesce( color_name, N'' )
|
62
|
+
|
51
63
|
, coalesce( size_code, N'' )
|
64
|
+
|
65
|
+
, coalesce( size_name, N'' )
|
52
66
|
|
53
67
|
order by insert_date asc
|
54
68
|
|
@@ -56,7 +70,7 @@
|
|
56
70
|
|
57
71
|
) grank
|
58
72
|
|
59
|
-
FROM
|
73
|
+
FROM order1_trn
|
60
74
|
|
61
75
|
WHERE del_flg = 0
|
62
76
|
|
@@ -76,13 +90,13 @@
|
|
76
90
|
|
77
91
|
SET del_flg = 1
|
78
92
|
|
79
|
-
FROM
|
93
|
+
FROM order1_trn x
|
80
94
|
|
81
95
|
WHERE x.del_flg = 0
|
82
96
|
|
83
97
|
AND EXISTS (
|
84
98
|
|
85
|
-
SELECT 1 FROM
|
99
|
+
SELECT 1 FROM order1_trn y
|
86
100
|
|
87
101
|
WHERE y.del_flg = 0
|
88
102
|
|
@@ -90,7 +104,11 @@
|
|
90
104
|
|
91
105
|
AND coalesce( y.color_code, N'' ) = coalesce( x.color_code, N'' )
|
92
106
|
|
107
|
+
AND coalesce( y.color_name, N'' ) = coalesce( x.color_name, N'' )
|
108
|
+
|
93
|
-
AND coalesce( y.size_code, N'' )
|
109
|
+
AND coalesce( y.size_code, N'' ) = coalesce( x.size_code, N'' )
|
110
|
+
|
111
|
+
AND coalesce( y.size_name, N'' ) = coalesce( x.size_name, N'' )
|
94
112
|
|
95
113
|
AND ( y.insert_date < x.insert_date
|
96
114
|
|
2
誤字の修正
test
CHANGED
@@ -82,9 +82,7 @@
|
|
82
82
|
|
83
83
|
AND EXISTS (
|
84
84
|
|
85
|
-
SELECT 1 FROM sample y
|
86
|
-
|
87
|
-
|
85
|
+
SELECT 1 FROM テーブル名 y
|
88
86
|
|
89
87
|
WHERE y.del_flg = 0
|
90
88
|
|
1
SQL文のロジック変更
test
CHANGED
@@ -4,13 +4,21 @@
|
|
4
4
|
|
5
5
|
|
6
6
|
|
7
|
+
### 2021/08/19 23:07 のコメントに基づき、SQL文を編集
|
7
8
|
|
9
|
+
~~**model 毎に 最新の insert_date に該当しなくなったレコード**~~
|
8
10
|
|
9
|
-
|
11
|
+
~~の ```del_flg```列 を ```1``` に更新したい~~
|
10
12
|
|
11
|
-
|
13
|
+
~~ということですよね。~~
|
12
14
|
|
15
|
+
↓
|
16
|
+
|
17
|
+
**model, color_code, size_code 毎に 最古の insert_date に該当しないレコード**
|
18
|
+
|
19
|
+
の ```del_flg```列 を ```1``` に更新
|
20
|
+
|
13
|
-
という
|
21
|
+
というロジックに変更。
|
14
22
|
|
15
23
|
|
16
24
|
|
@@ -22,7 +30,7 @@
|
|
22
30
|
|
23
31
|
UPDATE q
|
24
32
|
|
25
|
-
SET del_flg = 1
|
33
|
+
SET del_flg = 1
|
26
34
|
|
27
35
|
FROM
|
28
36
|
|
@@ -36,11 +44,15 @@
|
|
36
44
|
|
37
45
|
, row_number()
|
38
46
|
|
39
|
-
over( partition by model
|
47
|
+
over( partition by model
|
40
48
|
|
41
|
-
o
|
49
|
+
, coalesce( color_code, N'' )
|
42
50
|
|
51
|
+
, coalesce( size_code, N'' )
|
52
|
+
|
53
|
+
order by insert_date asc
|
54
|
+
|
43
|
-
, order_id
|
55
|
+
, order_id asc
|
44
56
|
|
45
57
|
) grank
|
46
58
|
|
@@ -62,7 +74,7 @@
|
|
62
74
|
|
63
75
|
UPDATE x
|
64
76
|
|
65
|
-
SET
|
77
|
+
SET del_flg = 1
|
66
78
|
|
67
79
|
FROM テーブル名 x
|
68
80
|
|
@@ -70,13 +82,19 @@
|
|
70
82
|
|
71
83
|
AND EXISTS (
|
72
84
|
|
73
|
-
SELECT 1 FROM
|
85
|
+
SELECT 1 FROM sample y
|
74
86
|
|
75
|
-
|
87
|
+
--SELECT 1 FROM テーブル名 y
|
76
88
|
|
77
|
-
|
89
|
+
WHERE y.del_flg = 0
|
78
90
|
|
91
|
+
AND y.model = x.model
|
92
|
+
|
93
|
+
AND coalesce( y.color_code, N'' ) = coalesce( x.color_code, N'' )
|
94
|
+
|
95
|
+
AND coalesce( y.size_code, N'' ) = coalesce( x.size_code, N'' )
|
96
|
+
|
79
|
-
AND ( y.insert_date
|
97
|
+
AND ( y.insert_date < x.insert_date
|
80
98
|
|
81
99
|
OR
|
82
100
|
|
@@ -84,7 +102,7 @@
|
|
84
102
|
|
85
103
|
AND
|
86
104
|
|
87
|
-
y.order_id
|
105
|
+
y.order_id < x.order_id
|
88
106
|
|
89
107
|
)
|
90
108
|
|