编号 省     市     县    类别  
1   HN    CS      NX    1
2   HN    CS      WC    2
3   HN    CD      CA    1
4   HN    HY     UY    1统计 每个市 下面有几个县,县的类别各有多少种(类别 只有 1 2 3 4 )结果
省    市   县数目  类别为1数目  类别为2 数目 类别为3数目  类别为4数目
HN   CS    2        1           1           0          0
HN   CD    1        1           0           0          0

解决方案 »

  1.   

    select 省,市,count(1) as 县数目
    ,case 类别 when 1 then 1 else 0 end as 类别为1数目
    ,case 类别 when 2 then 1 else 0 end as 类别为2数目
    ,case 类别 when 3 then 1 else 0 end as 类别为3数目
    ,case 类别 when 4 then 1 else 0 end as 类别为4数目
    Group by 省,市
      

  2.   

    create table dq(编号 int,省 varchar(10), 市 varchar(10),县 varchar(10),类别 int)
    insert into dq select 1,'HN','CS','NX',1 
    insert into dq select 2,'HN','CS','WC',2 
    insert into dq select 3,'HN','CD','CA',1 
    insert into dq select 4,'HN','HY','UY',1 
    go
    select 省,市,count(*) as 县数目,
    sum(case when 类别=1 then 1 else 0 end) as 类别1,
    sum(case when 类别=2 then 1 else 0 end) as 类别2,
    sum(case when 类别=3 then 1 else 0 end) as 类别3,
    sum(case when 类别=4 then 1 else 0 end) as 类别4 
    from dq 
    group by 省,市
    go
    drop table dq
    /*
    省          市          县数目         类别1         类别2         类别3         类别4
    ---------- ---------- ----------- ----------- ----------- ----------- -----------
    HN         CD         1           1           0           0           0
    HN         CS         2           1           1           0           0
    HN         HY         1           1           0           0           0
    */
      

  3.   

    declare @tb table(编号 int,省份 varchar(50),市 varchar(50),县 varchar(50),类别 int)
    insert into @tb select 1,'HN','CS','NX',1
    insert into @tb select 2,'HN','CS','WC',2
    insert into @tb select 3,'HN','CD','CA',1
    insert into @tb select 4,'HN','HY','UY',1select 省份,市,count(1) as 县数目,
    sum(case when 类别=1 then 1 else 0 end) as [类别1数目],
    sum(case when 类别=2 then 1 else 0 end) as [类别2数目],
    sum(case when 类别=3 then 1 else 0 end) as [类别3数目],
    sum(case when 类别=4 then 1 else 0 end) as [类别4数目]
    from @tb 
    group by 省份,市省份 市 县数目 类别1数目 类别2数目 类别3数目 类别4数目
    HN CD 1 1 0 0 0
    HN CS 2 1 1 0 0
    HN HY 1 1 0 0 0
      

  4.   

    select 省,市,count(1) as 县数目 
    ,sum(case 类别 when 1 then 1 else 0 end) as 类别为1数目
    ,Sum(case 类别 when 2 then 1 else 0 end) as 类别为2数目 
    ,Sum(case 类别 when 3 then 1 else 0 end) as 类别为3数目 
    ,sum(case 类别 when 4 then 1 else 0 end) as 类别为4数目 
    Group by 省,市
      

  5.   

    select 省,市,count(1) as 县数目 
    ,sum(case 类别 when 1 then 1 else 0 end) as 类别为1数目 
    ,Sum(case 类别 when 2 then 1 else 0 end) as 类别为2数目 
    ,Sum(case 类别 when 3 then 1 else 0 end) as 类别为3数目 
    ,sum(case 类别 when 4 then 1 else 0 end) as 类别为4数目 
    From tableName
    Group by 省,市
      

  6.   

    CREATE TABLE T(BH INT,PROVINCE VARCHAR(2),CITY VARCHAR(2),COUNTY VARCHAR(2),TYPE INT)
    GO
    INSERT INTO T
     SELECT 1,'HN','CS','NX',1 UNION ALL
     SELECT 2,'HN','CS','WC',2 UNION ALL
     SELECT 3,'HN','CD','CA',1 UNION ALL
     SELECT 4,'HN','HY','UY',1
    GO
    DECLARE @S VARCHAR(4000)
    SET @S='SELECT PROVINCE,CITY,COUNT(COUNTY) '
    SELECT @S=@S+',SUM(CASE WHEN type='+CONVERT(VARCHAR(10),TYPE)+' THEN 1 ELSE 0 END) AS [类型为'+CONVERT(VARCHAR(10),TYPE)+'的数目]' from
     (select distinct type from t) as a
    SELECT @S=@S+' FROM T GROUP BY PROVINCE,CITY'
    EXEC(@S)HN CD 1 1 0
    HN CS 2 1 1
    HN HY 1 1 0