SELECT COUNT(id), COUNT(val1 = 3 or null) FROM test_as
COUNT(id)
COUNT(val1 = 3 or null)
7
4
CASE WHEN ~ THEN | AND条件
test_asテーブル
id
val1
text1
test_date
test_dt
1
1
neko
2014/4/1
2014/12/12 0:00
2
2
kani
2014/4/2
2014/12/12 0:00
4
4
buta
2014/4/4
2014/12/12 0:00
5
3
yagi
2014/4/3
2014/12/12 0:00
6
3
ari
2014/4/3
2014/12/12 0:00
7
3
tori
2014/4/3
2014/12/12 0:00
8
4
kame
2014/4/3
2014/12/12 0:00
9
-1
unagi
NULL
NULL
ANDの記述例
SELECT
MIN(CASE WHEN test_date >= '2014-4-2' AND test_date < '2014-4-4' THEN val1 ELSE null END) as min_val1
FROM test_as;
min_val1
2
CASE WHEN ~ THEN | 入れ子
test_asテーブル
id
val1
text1
test_date
test_dt
1
1
neko
2014/4/1
2014/12/12 0:00
2
2
kani
2014/4/2
2014/12/12 0:00
4
4
buta
2014/4/4
2014/12/12 0:00
5
3
yagi
2014/4/3
2014/12/12 0:00
6
3
ari
2014/4/3
2014/12/12 0:00
7
3
tori
2014/4/3
2014/12/12 0:00
8
4
kame
2014/4/3
2014/12/12 0:00
9
-1
unagi
NULL
NULL
分岐の入れ子:例1
SELECT
MIN(
CASE WHEN ISNULL(test_date) THEN
-999
ELSE
CASE WHEN test_date >= '2014-4-2' AND test_date < '2014-4-4' THEN val1 ELSE null END
END
) AS min_val1
FROM test_as;
min_val1
-999
分岐の入れ子:例2
SELECT
MIN(
CASE WHEN ISNULL(test_date) THEN
999
ELSE
CASE WHEN test_date >= '2014-4-2' AND test_date < '2014-4-4' THEN val1 ELSE null END
END
) AS min_val1
FROM test_as;