我有一个表,它有一个只有2种输入的列:New和Old。它看起来像这样:
old_or_new
----------
old
old
new
new
old
我想计算旧的和新的数字并将表格调整为:
column_name | total
--------------------
old | 3
new | 2
我怎样才能做到这一点?
谢谢!
编辑:
我做的是:
select
count(case when old_or_new = 'Old' then 1 else 0 end) as old,
count(case when old_or_new = 'New' then 1 else 0 end) as new
from members
结果如下:
old | new
----------
3 | 2
答案 0 :(得分:3)
这是查询组的最基本形式:
select old_or_new as column_name,
count(*) as total
from the_table
group by old_or_new;
SQLFiddle:http://sqlfiddle.com/#!15/9469c/1