回答編集履歴

1

追記

2019/07/30 16:59

投稿

sazi
sazi

スコア25195

test CHANGED
@@ -6,7 +6,59 @@
6
6
 
7
7
  SQL自体はそのような感じで他の方法でも大差は無いと思います。
8
8
 
9
+ ```SQL
9
10
 
11
+ select class,
12
+
13
+ case sex
14
+
15
+ when '女' then '女'
16
+
17
+ when '男' and info like 'man%' then '男'
18
+
19
+ when '男' and info not like 'man%' then '不明' END
20
+
21
+ ,count(*)
22
+
23
+ from table1
24
+
25
+ group by class,
26
+
27
+ case sex
28
+
29
+ when '女' then '女'
30
+
31
+ when '男' and info like 'man%' then '男'
32
+
33
+ when '男' and info not like 'man%' then '不明' END
34
+
35
+ ```
36
+
37
+ 可読性を高めるなら
38
+
39
+ ```SQL
40
+
41
+ select class, new_sex, count(*)
42
+
43
+ from (
44
+
45
+ select class,
46
+
47
+ case sex
48
+
49
+ when '女' then '女'
50
+
51
+ when '男' and info like 'man%' then '男'
52
+
53
+ when '男' and info not like 'man%' then '不明' END as new_sex
54
+
55
+ from table1
56
+
57
+ ) t1
58
+
59
+ group by class, new_sex
60
+
61
+ ```
10
62
 
11
63
  性能という事なら、式インデックスを適用すると良いかもしれません。
12
64