回答編集履歴
7
chousei
test
CHANGED
@@ -68,7 +68,7 @@
|
|
68
68
|
|
69
69
|
when least (date_a,date_b)>'2019-03-13' then null
|
70
70
|
|
71
|
-
when
|
71
|
+
when not(date_a>=date_b)=('2019-03-13'>=greatest(date_a,date_b)) then license_b
|
72
72
|
|
73
73
|
else license_a
|
74
74
|
|
@@ -178,7 +178,7 @@
|
|
178
178
|
|
179
179
|
when least(coalesce(date_a,'9999-12-31'),coalesce(date_b,'9999-12-31'))>'2019-03-13' then null
|
180
180
|
|
181
|
-
when
|
181
|
+
when not(coalesce(date_a,'1900-01-01')>=coalesce(date_b,'1900-01-01'))=('2019-03-13'>=greatest(coalesce(date_a,'1900-01-01'),coalesce(date_b,'1900-01-01'))) then license_b
|
182
182
|
|
183
183
|
else license_a
|
184
184
|
|
6
調整
test
CHANGED
@@ -68,7 +68,7 @@
|
|
68
68
|
|
69
69
|
when least (date_a,date_b)>'2019-03-13' then null
|
70
70
|
|
71
|
-
when date_a>=date_b
|
71
|
+
when !(date_a>=date_b)=('2019-03-13'>=greatest(date_a,date_b)) then license_b
|
72
72
|
|
73
73
|
else license_a
|
74
74
|
|
@@ -76,9 +76,9 @@
|
|
76
76
|
|
77
77
|
from tbl
|
78
78
|
|
79
|
+
```
|
79
80
|
|
80
|
-
|
81
|
-
|
81
|
+
※こっちもXORを排除
|
82
82
|
|
83
83
|
|
84
84
|
|
5
調整
test
CHANGED
@@ -178,13 +178,15 @@
|
|
178
178
|
|
179
179
|
when least(coalesce(date_a,'9999-12-31'),coalesce(date_b,'9999-12-31'))>'2019-03-13' then null
|
180
180
|
|
181
|
-
when coalesce(date_a,'1900-01-01')>=coalesce(date_b,'1900-01-01')
|
181
|
+
when !(coalesce(date_a,'1900-01-01')>=coalesce(date_b,'1900-01-01'))=('2019-03-13'>=greatest(coalesce(date_a,'1900-01-01'),coalesce(date_b,'1900-01-01'))) then license_b
|
182
182
|
|
183
183
|
else license_a
|
184
184
|
|
185
185
|
end as lincese
|
186
186
|
|
187
187
|
from tbl
|
188
|
+
|
189
|
+
|
188
190
|
|
189
191
|
```
|
190
192
|
|
@@ -193,3 +195,5 @@
|
|
193
195
|
|
194
196
|
|
195
197
|
※oracleでは0年駄目なんですね?1900年に換えときました
|
198
|
+
|
199
|
+
※XORを別式に換えました
|
4
調整
test
CHANGED
@@ -178,7 +178,7 @@
|
|
178
178
|
|
179
179
|
when least(coalesce(date_a,'9999-12-31'),coalesce(date_b,'9999-12-31'))>'2019-03-13' then null
|
180
180
|
|
181
|
-
when coalesce(date_a,'00
|
181
|
+
when coalesce(date_a,'1900-01-01')>=coalesce(date_b,'1900-01-01') xor '2019-03-13'>=greatest(coalesce(date_a,'1900-01-01'),coalesce(date_b,'1900-01-01')) then license_b
|
182
182
|
|
183
183
|
else license_a
|
184
184
|
|
@@ -189,3 +189,7 @@
|
|
189
189
|
```
|
190
190
|
|
191
191
|
coalesceはnvlでもいけるかも
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
※oracleでは0年駄目なんですね?1900年に換えときました
|
3
chousei
test
CHANGED
@@ -6,31 +6,29 @@
|
|
6
6
|
|
7
7
|
insert into tbl values
|
8
8
|
|
9
|
-
( 1,'a','2017-01-01','b','2018-01-01'),/* 両方過去 a<b */
|
9
|
+
( 1,'a','2017-01-01','b','2018-01-01'),/* 両方過去 a<b 結果:b */
|
10
10
|
|
11
|
-
( 2,'a','2018-01-01','b','2017-01-01'),/* 両方過去 a>b */
|
11
|
+
( 2,'a','2018-01-01','b','2017-01-01'),/* 両方過去 a>b 結果:a */
|
12
12
|
|
13
|
-
( 3,'a','2018-01-01','b','2018-01-01'),/* 両方過去 a=b */
|
13
|
+
( 3,'a','2018-01-01','b','2018-01-01'),/* 両方過去 a=b 結果:a */
|
14
14
|
|
15
|
-
( 4,'a','2020-01-01','b','2018-01-01'),/* aのみ未来 */
|
15
|
+
( 4,'a','2020-01-01','b','2018-01-01'),/* aのみ未来 結果:b */
|
16
16
|
|
17
|
-
( 5,'a','2018-01-01','b','2020-01-01'),/* bのみ未来 */
|
17
|
+
( 5,'a','2018-01-01','b','2020-01-01'),/* bのみ未来 結果:a */
|
18
18
|
|
19
|
-
( 6,'a','2020-01-01','b','2021-01-01'),/* 両方未来 a<b */
|
19
|
+
( 6,'a','2020-01-01','b','2021-01-01'),/* 両方未来 a<b 結果:NULL */
|
20
20
|
|
21
|
-
( 7,'a','2021-01-01','b','2020-01-01'),/* 両方未来 a>b */
|
21
|
+
( 7,'a','2021-01-01','b','2020-01-01'),/* 両方未来 a>b 結果:NULL */
|
22
22
|
|
23
|
-
( 8,'a','2019-03-13','b','2020-01-01'),/* a当日,b未来 */
|
23
|
+
( 8,'a','2019-03-13','b','2020-01-01'),/* a当日,b未来 結果:a */
|
24
24
|
|
25
|
-
( 9,'a','2019-03-13','b','2018-01-01'),/* a当日,b過去 */
|
25
|
+
( 9,'a','2019-03-13','b','2018-01-01'),/* a当日,b過去 結果:a */
|
26
26
|
|
27
|
-
(10,'a','2020-01-01','b','2019-03-13'),/* b当日,a未来 */
|
27
|
+
(10,'a','2020-01-01','b','2019-03-13'),/* b当日,a未来 結果:b */
|
28
28
|
|
29
|
-
(11,'a','2018-01-01','b','2019-03-13'),/* b当日,a過去 */
|
29
|
+
(11,'a','2018-01-01','b','2019-03-13'),/* b当日,a過去 結果:b */
|
30
30
|
|
31
|
-
(12,'a','2019-03-13','b','2019-03-13');/* a,b当日 */
|
31
|
+
(12,'a','2019-03-13','b','2019-03-13');/* a,b当日 結果:a */
|
32
|
-
|
33
|
-
|
34
32
|
|
35
33
|
```
|
36
34
|
|
@@ -70,13 +68,15 @@
|
|
70
68
|
|
71
69
|
when least (date_a,date_b)>'2019-03-13' then null
|
72
70
|
|
73
|
-
when date_a>=date_b xor greatest(date_a,date_b
|
71
|
+
when date_a>=date_b xor '2019-03-13'>=greatest(date_a,date_b) then license_b
|
74
72
|
|
75
73
|
else license_a
|
76
74
|
|
77
75
|
end as lincese
|
78
76
|
|
79
77
|
from tbl
|
78
|
+
|
79
|
+
|
80
80
|
|
81
81
|
```
|
82
82
|
|
2
更新
test
CHANGED
@@ -167,3 +167,25 @@
|
|
167
167
|
結果
|
168
168
|
|
169
169
|
b-a-a-b-a-NULL-NULL-NULL-a-a-b-b-a-b-b-NULL-a-a-NULL-NULL
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
まとめると
|
174
|
+
|
175
|
+
```SQL
|
176
|
+
|
177
|
+
select code,case
|
178
|
+
|
179
|
+
when least(coalesce(date_a,'9999-12-31'),coalesce(date_b,'9999-12-31'))>'2019-03-13' then null
|
180
|
+
|
181
|
+
when coalesce(date_a,'0000-01-01')>=coalesce(date_b,'0000-01-01') xor '2019-03-13'>=greatest(coalesce(date_a,'0000-01-01'),coalesce(date_b,'0000-01-01')) then license_b
|
182
|
+
|
183
|
+
else license_a
|
184
|
+
|
185
|
+
end as lincese
|
186
|
+
|
187
|
+
from tbl
|
188
|
+
|
189
|
+
```
|
190
|
+
|
191
|
+
coalesceはnvlでもいけるかも
|
1
調整
test
CHANGED
@@ -79,3 +79,91 @@
|
|
79
79
|
from tbl
|
80
80
|
|
81
81
|
```
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
# 日付NULLあり
|
86
|
+
|
87
|
+
```SQL
|
88
|
+
|
89
|
+
create table tbl(code int,license_a varchar(20),date_a date null,license_b varchar(29),date_b date null);
|
90
|
+
|
91
|
+
insert into tbl values
|
92
|
+
|
93
|
+
( 1,'a','2017-01-01','b','2018-01-01'),/* 両方過去 a<b 結果:b */
|
94
|
+
|
95
|
+
( 2,'a','2018-01-01','b','2017-01-01'),/* 両方過去 a>b 結果:a */
|
96
|
+
|
97
|
+
( 3,'a','2018-01-01','b','2018-01-01'),/* 両方過去 a=b 結果:a */
|
98
|
+
|
99
|
+
( 4,'a','2020-01-01','b','2018-01-01'),/* aのみ未来 結果:b */
|
100
|
+
|
101
|
+
( 5,'a','2018-01-01','b','2020-01-01'),/* bのみ未来 結果:a */
|
102
|
+
|
103
|
+
( 6,'a','2020-01-01','b','2021-01-01'),/* 両方未来 a<b 結果:NULL */
|
104
|
+
|
105
|
+
( 7,'a','2021-01-01','b','2020-01-01'),/* 両方未来 a>b 結果:NULL */
|
106
|
+
|
107
|
+
( 8,'a','2020-01-01','b','2020-01-01'),/* 両方未来 a=b 結果:NULL */
|
108
|
+
|
109
|
+
( 9,'a','2019-03-13','b','2020-01-01'),/* a当日,b未来 結果:a */
|
110
|
+
|
111
|
+
(10,'a','2019-03-13','b','2018-01-01'),/* a当日,b過去 結果:a */
|
112
|
+
|
113
|
+
(11,'a','2020-01-01','b','2019-03-13'),/* b当日,a未来 結果:b */
|
114
|
+
|
115
|
+
(12,'a','2018-01-01','b','2019-03-13'),/* b当日,a過去 結果:b */
|
116
|
+
|
117
|
+
(13,'a','2019-03-13','b','2019-03-13'),/* a,b当日 結果:a */
|
118
|
+
|
119
|
+
(14,'a',NULL,'b','2018-01-01'), /* a NULL,b過去 結果:b */
|
120
|
+
|
121
|
+
(15,'a',NULL,'b','2019-03-13'), /* a NULL,b当日 結果:b */
|
122
|
+
|
123
|
+
(16,'a',NULL,'b','2020-01-01'), /* a NULL,b未来 結果:NULL */
|
124
|
+
|
125
|
+
(17,'a','2018-01-01','b',NULL), /* b NULL,a過去 結果:a */
|
126
|
+
|
127
|
+
(18,'a','2019-03-13','b',NULL), /* b NULL,a当日 結果:a */
|
128
|
+
|
129
|
+
(19,'a','2020-01-01','b',NULL), /* b NULL,a未来 結果:NULL */
|
130
|
+
|
131
|
+
(20,'a',NULL,'b',NULL); /* 両方 NULL 結果:NULL */
|
132
|
+
|
133
|
+
```
|
134
|
+
|
135
|
+
検索
|
136
|
+
|
137
|
+
```SQL
|
138
|
+
|
139
|
+
select code,case
|
140
|
+
|
141
|
+
when date_a<='2019-03-13' and date_b is null then license_a
|
142
|
+
|
143
|
+
when date_a> '2019-03-13' and date_b is null then null
|
144
|
+
|
145
|
+
when date_b<='2019-03-13' and date_a is null then license_b
|
146
|
+
|
147
|
+
when date_b> '2019-03-13' and date_a is null then null
|
148
|
+
|
149
|
+
when date_a is null and date_b is null then null
|
150
|
+
|
151
|
+
when date_a<='2019-03-13' and date_b<='2019-03-13' and date_a<date_b then license_b
|
152
|
+
|
153
|
+
when date_a<='2019-03-13' and date_b<='2019-03-13' and date_a>=date_b then license_a
|
154
|
+
|
155
|
+
when date_a<='2019-03-13' and date_b>='2019-03-13' then license_a
|
156
|
+
|
157
|
+
when date_a>='2019-03-13' and date_b<='2019-03-13' then license_b
|
158
|
+
|
159
|
+
when date_a>='2019-03-13' and date_b>='2019-03-13' then null
|
160
|
+
|
161
|
+
end as lincese
|
162
|
+
|
163
|
+
from tbl
|
164
|
+
|
165
|
+
```
|
166
|
+
|
167
|
+
結果
|
168
|
+
|
169
|
+
b-a-a-b-a-NULL-NULL-NULL-a-a-b-b-a-b-b-NULL-a-a-NULL-NULL
|