1

我有一个具有以下值的 oracle 数据库表

+------+--------+
| id   | Name   |
+------+--------+
|    0 | One    |
|    1 | Two    |
|    2 | Three  |
|    3 | four   |
|    4 | One    |
|    5 | One    |
|    6 | Two    |
|    7 | five   |
+------+--------+

我想计算名称为“一”、“二”、其余名称的行数。结果集应该像

+------+--------+
| name | count  |
+------+--------+
|  One | 3      |
+------+--------+
|  Two | 2      |
+------+--------+
|  Rest| 3      |
+------+--------+

有什么帮助吗?

4

1 回答 1

4
select
  case name
    when 'One' then 'One'
    when 'Two' then 'Two'
    else 'Rest'
  end name,
  count(*)
from
  my_table
group by
  case name
    when 'One' then 'One'
    when 'Two' then 'Two'
    else 'Rest'
  end

避免重复:

select
  name,
  count(*)
from (
  select
    case name
      when 'One' then 'One'
      when 'Two' then 'Two'
      else 'Rest'
    end name
  from
    my_table)
group by
  name
于 2013-04-26T07:40:10.847 回答