回答編集履歴
3
ご希望の動作になるよう、SQL文を変更
answer
CHANGED
@@ -1,13 +1,18 @@
|
|
1
1
|
> 重複データの中で、insert_dateで最も古いデータ1つ以外をUPDATEする。
|
2
2
|
> -- UPDATE対象を確認。(insert_dateで最も古いデータ以外がUPDATE対象)
|
3
3
|
|
4
|
-
### 2021/08/19
|
4
|
+
### 2021/08/19 22:21 のコメントに基づき、SQL文を編集
|
5
5
|
~~**model 毎に 最新の insert_date に該当しなくなったレコード**~~
|
6
6
|
~~の ```del_flg```列 を ```1``` に更新したい~~
|
7
7
|
~~ということですよね。~~
|
8
|
-
|
8
|
+
↓
|
9
|
+
- model
|
9
|
-
|
10
|
+
- color_code
|
11
|
+
- color_name
|
12
|
+
- size_code
|
13
|
+
- size_name
|
14
|
+
毎に
|
10
|
-
の ```del_flg```列 を ```1``` に更新
|
15
|
+
最古の insert_date に該当しないレコードの ```del_flg``` 列 を ```1``` に更新
|
11
16
|
というロジックに変更。
|
12
17
|
|
13
18
|
色々な記述方法があるのですけど、ここでは2種類のSQLを載せておきます。
|
@@ -23,11 +28,13 @@
|
|
23
28
|
, row_number()
|
24
29
|
over( partition by model
|
25
30
|
, coalesce( color_code, N'' )
|
31
|
+
, coalesce( color_name, N'' )
|
26
32
|
, coalesce( size_code, N'' )
|
33
|
+
, coalesce( size_name, N'' )
|
27
34
|
order by insert_date asc
|
28
35
|
, order_id asc
|
29
36
|
) grank
|
30
|
-
FROM
|
37
|
+
FROM order1_trn
|
31
38
|
WHERE del_flg = 0
|
32
39
|
) q
|
33
40
|
WHERE grank > 1
|
@@ -37,14 +44,16 @@
|
|
37
44
|
```SQL
|
38
45
|
UPDATE x
|
39
46
|
SET del_flg = 1
|
40
|
-
FROM
|
47
|
+
FROM order1_trn x
|
41
48
|
WHERE x.del_flg = 0
|
42
49
|
AND EXISTS (
|
43
|
-
SELECT 1 FROM
|
50
|
+
SELECT 1 FROM order1_trn y
|
44
51
|
WHERE y.del_flg = 0
|
45
52
|
AND y.model = x.model
|
46
53
|
AND coalesce( y.color_code, N'' ) = coalesce( x.color_code, N'' )
|
54
|
+
AND coalesce( y.color_name, N'' ) = coalesce( x.color_name, N'' )
|
47
|
-
AND coalesce( y.size_code,
|
55
|
+
AND coalesce( y.size_code, N'' ) = coalesce( x.size_code, N'' )
|
56
|
+
AND coalesce( y.size_name, N'' ) = coalesce( x.size_name, N'' )
|
48
57
|
AND ( y.insert_date < x.insert_date
|
49
58
|
OR
|
50
59
|
y.insert_date = x.insert_date
|
2
誤字の修正
answer
CHANGED
@@ -40,8 +40,7 @@
|
|
40
40
|
FROM テーブル名 x
|
41
41
|
WHERE x.del_flg = 0
|
42
42
|
AND EXISTS (
|
43
|
-
SELECT 1 FROM sample y
|
44
|
-
|
43
|
+
SELECT 1 FROM テーブル名 y
|
45
44
|
WHERE y.del_flg = 0
|
46
45
|
AND y.model = x.model
|
47
46
|
AND coalesce( y.color_code, N'' ) = coalesce( x.color_code, N'' )
|
1
SQL文のロジック変更
answer
CHANGED
@@ -1,25 +1,31 @@
|
|
1
1
|
> 重複データの中で、insert_dateで最も古いデータ1つ以外をUPDATEする。
|
2
2
|
> -- UPDATE対象を確認。(insert_dateで最も古いデータ以外がUPDATE対象)
|
3
3
|
|
4
|
+
### 2021/08/19 23:07 のコメントに基づき、SQL文を編集
|
5
|
+
~~**model 毎に 最新の insert_date に該当しなくなったレコード**~~
|
6
|
+
~~の ```del_flg```列 を ```1``` に更新したい~~
|
7
|
+
~~ということですよね。~~
|
8
|
+
↓
|
9
|
+
**model, color_code, size_code 毎に 最古の insert_date に該当しないレコード**
|
10
|
+
の ```del_flg```列 を ```1``` に更新
|
11
|
+
というロジックに変更。
|
4
12
|
|
5
|
-
**model 毎に 最新の insert_date に該当しなくなったレコード**
|
6
|
-
の ```del_flg```列 を ```1``` に更新したい
|
7
|
-
ということですよね。
|
8
|
-
|
9
13
|
色々な記述方法があるのですけど、ここでは2種類のSQLを載せておきます。
|
10
14
|
|
11
15
|
```SQL
|
12
16
|
UPDATE q
|
13
|
-
SET del_flg = 1
|
17
|
+
SET del_flg = 1
|
14
18
|
FROM
|
15
19
|
(
|
16
20
|
SELECT order_id
|
17
21
|
, model
|
18
22
|
, del_flg
|
19
23
|
, row_number()
|
20
|
-
over( partition by model
|
24
|
+
over( partition by model
|
25
|
+
, coalesce( color_code, N'' )
|
26
|
+
, coalesce( size_code, N'' )
|
21
|
-
order by insert_date
|
27
|
+
order by insert_date asc
|
22
|
-
, order_id
|
28
|
+
, order_id asc
|
23
29
|
) grank
|
24
30
|
FROM テーブル名
|
25
31
|
WHERE del_flg = 0
|
@@ -30,18 +36,21 @@
|
|
30
36
|
|
31
37
|
```SQL
|
32
38
|
UPDATE x
|
33
|
-
SET
|
39
|
+
SET del_flg = 1
|
34
40
|
FROM テーブル名 x
|
35
41
|
WHERE x.del_flg = 0
|
36
42
|
AND EXISTS (
|
43
|
+
SELECT 1 FROM sample y
|
37
|
-
SELECT 1 FROM テーブル名 y
|
44
|
+
--SELECT 1 FROM テーブル名 y
|
45
|
+
WHERE y.del_flg = 0
|
38
|
-
|
46
|
+
AND y.model = x.model
|
39
|
-
AND y.
|
47
|
+
AND coalesce( y.color_code, N'' ) = coalesce( x.color_code, N'' )
|
48
|
+
AND coalesce( y.size_code, N'' ) = coalesce( x.size_code, N'' )
|
40
|
-
AND ( y.insert_date
|
49
|
+
AND ( y.insert_date < x.insert_date
|
41
50
|
OR
|
42
51
|
y.insert_date = x.insert_date
|
43
52
|
AND
|
44
|
-
y.order_id
|
53
|
+
y.order_id < x.order_id
|
45
54
|
)
|
46
55
|
)
|
47
56
|
;
|