我的数据库结构:(表名称为company_info
)
|comp_id | comp_name | comp_nature
| 001 | Citigroup, Inc. | Bank
| 002 | Harvard University | Academe
| 003 | Western Union | Courier
| 004 | C.I.A. | Government Agency
我想创建一个包含comp_nature
的查询。但是,如果comp_nature
不是“银行”或“学院”,我希望将其归类为“其他”,因此查询结果应类似于以下内容。
| comp_nature | count |
| Bank | 1 |
| Academe | 1 |
| Other | 2 |
我能够使用COUNT
关键字,但似乎无法在SELECT
语句中获得如何创建“行”(其他)的方法。 (或者仅用一个SELECT
语句就有可能吗?)
答案 0 :(得分:1)
会是这样的:
select comp_nature, count(*) as cnt from
(
select
case when comp_nature in('bank','acadme') then comp_nature
else 'Other' end as comp_nature
from company_info
) as X
group by comp_nature
order by <whatever makes sense>
答案 1 :(得分:1)
Select
case
when comp_nature not in ('Academe', 'Bank') then 'other'
else comp_nature end as col,
count(*) from t
group by col