回答編集履歴
1
調整
test
CHANGED
@@ -37,3 +37,67 @@
|
|
37
37
|
);
|
38
38
|
|
39
39
|
```
|
40
|
+
|
41
|
+
# 調整
|
42
|
+
|
43
|
+
- KEY1を比較
|
44
|
+
|
45
|
+
- 同じならKEY2を比較
|
46
|
+
|
47
|
+
- 同じならKEY3を比較
|
48
|
+
|
49
|
+
- すべて同じなら1個だけ
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
```SQL
|
54
|
+
|
55
|
+
create table tbl(KEY1 varchar(20),KEY2 varchar(10),KEY3 varchar(10));
|
56
|
+
|
57
|
+
insert into tbl values
|
58
|
+
|
59
|
+
('A1191028000001','00002','0'),
|
60
|
+
|
61
|
+
('B1191028000001','00001','1'),
|
62
|
+
|
63
|
+
('B1191028000002','00001','1'),
|
64
|
+
|
65
|
+
('B1191028000003','00002','0'),
|
66
|
+
|
67
|
+
('B1191028000003','00002','1'),
|
68
|
+
|
69
|
+
('C1191028000001','00002','1'),
|
70
|
+
|
71
|
+
('C1191028000002','00001','1'),
|
72
|
+
|
73
|
+
('C1191028000002','00002','0'),
|
74
|
+
|
75
|
+
('D1191028000001','00001','0'),
|
76
|
+
|
77
|
+
('D1191028000001','00001','0');
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
select distinct * from tbl as t1
|
84
|
+
|
85
|
+
where not exists
|
86
|
+
|
87
|
+
(select 1 from tbl where
|
88
|
+
|
89
|
+
(substr(KEY1,1,2)=substr(t1.KEY1,1,2) and
|
90
|
+
|
91
|
+
(KEY1>t1.KEY1 or
|
92
|
+
|
93
|
+
KEY1=t1.KEY1 and KEY2>t1.KEY2 or
|
94
|
+
|
95
|
+
KEY1=t1.KEY1 and KEY2=t1.KEY2 and KEY3>t1.KEY3
|
96
|
+
|
97
|
+
)
|
98
|
+
|
99
|
+
)
|
100
|
+
|
101
|
+
);
|
102
|
+
|
103
|
+
```
|