select count(*),地区, 年份 from table group by 地区, 年份

解决方案 »

  1.   

    select 地区,sum(case  when 年份 = 1995 then 人数 else 0 end) as 1995, 
                sum(case  when 年份 = 1996 then 人数 else 0 end) as 1996,
                sum(case  when 年份 = 1997 then 人数 else 0 end) as 1997,
                sum(case  when 年份 = 1998 then 人数 else 0 end) as 1998
    from 
    (select count(*) as 人数,地区, 年份 from 表 group by 地区, 年份) as temp
    group by 地区
      

  2.   

    declare @sql varchar(8000)
    set @sql = 'select 地区'
    select @sql = @sql + ',sum(case 年 when '''+cast( 年 as varchar)+''' then 数据 else 0 end) as '''+cast( 年 as varchar)+''''
      from (select distinct 年 from 有一表) as a
    select @sql = @sql+' from 有一表 group by 地区'exec(@sql)
    go
      

  3.   

    谢谢两位,待我试试,另外想问一下,哪有讲sql语句用法的书供下载?