日期:2014-05-18  浏览次数:20338 次

求一个sql语句~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
用一条语句得出结果
现有如下表
field1 field2
2303 1
2303 1
2305 2
2302 3
2305 2
2306 3
2307 3
2305 2

要求得出field2对应的field1的数量,不算重复的

字段名 '1' '2' '3'
结果 1 1 3

也就是field2为1的数量有1个(2303)
  field2为2的数量有1个(2305)
  field2为3的数量有3个(2302、2306、2307)

不知道我说的明不明白

------解决方案--------------------
SQL code
select field2,count(*) from 
(
  select distinct field1,field2 from tb 
) t
group by field2

------解决方案--------------------
SQL code

select sum(case field2 when 1 then 1 end) '1',
       sum(case field2 when 2 then 1 end) '2',
       sum(case field2 when 3 then 1 end) '3'
from tbl group by field1,field2