對於表,例如:如何計數並按多列組合進行分組?
foo_table
id | str_col | bool_col
1 "1234" 0
2 "3215" 0
3 "8132" 1
4 NULL 1
5 "" 1
6 "" 0
我知道如何查詢兩個:
count(*) | bool_col
3 0
3 1
和
count(*) | isnull(str_col) or str_col = ""
3 0
3 1
,但我怎麼能得到的東西,如:
count(*) | bool_col | isnull(str_col) or str_col = ""
2 0 0
1 0 1
1 1 0
2 1 1
在此期間,我只是個別做:
select count(*) from foo_table where bool_col and (isnull(str_col) or str_col = "");
select count(*) from foo_table where not bool_col and (isnull(str_col) or str_col = "");
select count(*) from foo_table where bool_col and not (isnull(str_col) or str_col = "");
select count(*) from foo_table where not bool_col and not (isnull(str_col) or str_col = "");
完美!我從來不知道'case',謝謝! – Kache 2013-05-03 21:25:40
你當然歡迎!很高興幫助。 – peterm 2013-05-03 22:58:27