表a:
 companyname       city    industry       address ......
 贵州省遵义化工厂     遵义     制造业           遵义市汇州区.....
 广州市城市规划局     广州     政府             广东省吉祥路80号12楼......
 重庆信息技术职业学院 重庆     教育             重庆市万州区董家镇巨人路1号
 ......效果见下表
  city  制造业       政府      教育      ......   汇总
  北京     1        2         3       ......   6+......              
  上海     30       114       87       ......   231+......
  广州     5        21        69       ......   95+......
  .......
  汇总    36+ ...  137+...   159+......         332+......
     1        2         3       
      30       114       87      这些数字是每个城市公司所属行业的个数
      5        21        69       
  
要求是每一行的汇总是各个城市所有行业的总和,每列最后一行统计各行业的总和(sqlserver2000)

解决方案 »

  1.   

    看着好象可以用ROLLUP和CUBE. LZ可以去联机丛书看看.
      

  2.   

    IF(EXISTS(SELECT * FROM SYSOBJECTS WHERE name = 'tableA' AND TYPE = 'T'))
    BEGIN
    DROP TABLE tableA
    ENDCREATE TABLE tableA
    (
    companyname NVARCHAR(200),
    city NVARCHAR(20),
    industry NVARCHAR(100)
    )INSERT INTO tableA VALUES('北京A公司','北京','教育业')
    INSERT INTO tableA VALUES('北京B公司','北京','IT业')
    INSERT INTO tableA VALUES('北京C公司','北京','IT业')
    INSERT INTO tableA VALUES('北京D公司','北京','工业')
    INSERT INTO tableA VALUES('深圳A公司','深圳','教育业')
    INSERT INTO tableA VALUES('深圳B公司','深圳','工业')
    INSERT INTO tableA VALUES('武汉A公司','武汉','教育业')
    INSERT INTO tableA VALUES('武汉B公司','武汉','工业')
    INSERT INTO tableA VALUES('武汉C公司','武汉','教育业')
    INSERT INTO tableA VALUES('上海A公司','上海','教育业')
    INSERT INTO tableA VALUES('上海B公司','上海','工业')
    INSERT INTO tableA VALUES('上海C公司','上海','工业')
    INSERT INTO tableA VALUES('上海D公司','上海','IT业')
    INSERT INTO tableA VALUES('上海E公司','上海','IT业')
    INSERT INTO tableA VALUES('天津A公司','天津','教育业')SELECT * FROM tableASELECT ISNULL(a.city,'合计') AS city,SUM(c) AS c FROM
    (
    SELECT city,COUNT(industry) AS c FROM tableA
    GROUP BY city
    ) AS a
    GROUP BY city WITH CUBE
      

  3.   

    declare @sql varchar(4000),@sql1 varchar(4000),@sql2 varchar(4000)
    set @sql='select city'
    select @sql2=',count(case when industry='+industry+' then 1 else 0 end) ['+industry+']'
    from (select distinct industry from table) t
    select @sql1='select 汇总'
    exec (@sql+@sql2+',count(*) as 汇总 from table group by city union all '+@sql1+@sql2 +' from table group by industry')
      

  4.   


    if object_id('tempdb..#t') is not null drop table #t
    create table #t (id int,city varchar(10),industry varchar(10))
    insert into #t
    select 1,'广州','教育' union
    select 2,'重庆','制造业' union
    select 3,'重庆','教育' union
    select 4,'北京','服务' union
    select 5,'广州','制造业' union
    select 6,'广州','教育' union
    select 7,'广州','服务' union
    select 8,'重庆','教育'declare @sql varchar(1000)
    set @sql='select isnull(city,''汇总'') as city'
    select @sql=@sql+',sum(case when industry='''+industry+''' then 1 else 0 end) as ['+industry+']'
    from (select industry
             from #t 
             group by industry
            ) a
    set @sql=@sql+' from #t group by city with rollup'execute( @sql )/*
    city       服务          教育          制造业
    ---------- ----------- ----------- -----------
    北京         1           0           0
    广州         1           2           1
    重庆         0           2           1
    汇总         2           4           2(4 行受影响)
    */