日期:2014-05-19  浏览次数:20769 次

一个字段中不同数据计算后分两列显示的问题
表中2字段type和name,type字段只有 'a ', 'b '两种值,我希望按type的不同计算count(type),if( 'a ')   count(type)   as   'A ';if( 'b ')   count(type)   as   'B '并按name分组,实现如下表结构
name|A|B

------解决方案--------------------
Select
name,
SUM(Case type When 'A ' Then 1 Else 0 End) As A,
SUM(Case type When 'B ' Then 1 Else 0 End) As B
From
TableName
Group By
name
------解决方案--------------------
不知楼主是否此意:
select name,sum(case type when 'a ' then 1 else 0 end) as 'A ',sum(case type when 'b ' then 1 else 0 end) from table1