刚刚问了个动态语句 行转列的问题在追问一下: 如何在没列的最底下加上合计呢?http://topic.csdn.net/u/20110705/18/ea79b3e4-efa3-4bfc-bcca-920adff11535.html?seed=137860374&r=74236818#r_74236818

解决方案 »

  1.   

    declare @sql varchar(8000)
    set @sql = 'select SP01 '
    select @sql = @sql + ', max(case SP02+SP03 when ''' + SP02+SP03 + ''' then SP04 else 0 end) [' + SP02+'|'+SP03 + ']'
    from (SELECT DISTINCT SP02,SP03 FROM S_ISP)T
    set @sql = @sql + ' from S_ISP group by SP01 with rollup'
    EXEC (@sql) 
      

  2.   

    CREATE TABLE [dbo].[S_ISP] (
        [SP01] [nvarchar] (10) COLLATE Chinese_PRC_BIN NULL ,
        [SP02] [nvarchar] (10) COLLATE Chinese_PRC_BIN NULL ,
        [SP03] [nvarchar] (8) COLLATE Chinese_PRC_BIN NULL ,
        [SP04] [nvarchar] (8) COLLATE Chinese_PRC_BIN NULL 
    ) ON [PRIMARY]
    go
    INSERT INTO [S_ISP]
    SELECT     'A','2011年6月','I(min)','5' UNION ALL
    SELECT     'A','2011年6月','I(max)','9' UNION ALL
    SELECT     'A','2011年6月','S(min)','8' UNION ALL
    SELECT     'A','2011年6月','S(max)','9' UNION ALL
    SELECT     'B','2011年6月','I(min)','4' UNION ALL
    SELECT     'B','2011年6月','I(max)','6' UNION ALL
    SELECT     'B','2011年6月','S(min)','48' UNION ALL
    SELECT     'B','2011年6月','S(max)','69' UNION ALLSELECT     'A','2011年7月','I(min)','15' UNION ALL
    SELECT     'A','2011年7月','I(max)','19' UNION ALL
    SELECT     'A','2011年7月','S(min)','18' UNION ALL
    SELECT     'A','2011年7月','S(max)','19' UNION ALL
    SELECT     'B','2011年7月','I(min)','14' UNION ALL
    SELECT     'B','2011年7月','I(max)','16' UNION ALL
    SELECT     'B','2011年7月','S(min)','408' UNION ALL
    SELECT     'B','2011年7月','S(max)','609' 
    go
    ;with cte as(
    select sp01,sp02+'|'+sp03 sp023,convert(int,sp04)sp04 from [s_isp]
    ),c2 as(
    select SP01,[2011年6月|I(min)],[2011年6月|I(max)],[2011年6月|S(min)],[2011年6月|S(max)]
    ,[2011年7月|I(min)],[2011年7月|I(max)],[2011年7月|S(min)],[2011年7月|S(max)]
    from cte
    pivot (sum(SP04) for SP023 in(
    [2011年6月|I(min)],[2011年6月|I(max)],[2011年6月|S(min)],[2011年6月|S(max)]
    ,[2011年7月|I(min)],[2011年7月|I(max)],[2011年7月|S(min)],[2011年7月|S(max)]
    ))b
    )select * from c2
    union all
    select '合计',sum([2011年6月|I(min)]),sum([2011年6月|I(max)]),sum([2011年6月|S(min)]),sum([2011年6月|S(max)])
    ,sum([2011年7月|I(min)]),sum([2011年7月|I(max)]),sum([2011年7月|S(min)]),sum([2011年7月|S(max)])
     from c2
    go
    drop table [S_ISP]
    /*
    SP01       2011年6月|I(min) 2011年6月|I(max) 2011年6月|S(min) 2011年6月|S(max) 2011年7月|I(min) 2011年7月|I(max) 2011年7月|S(min) 2011年7月|S(max)
    ---------- -------------- -------------- -------------- -------------- -------------- -------------- -------------- --------------
    A          5              9              8              9              15             19             18             19
    B          4              6              48             69             14             16             408            609
    合计         9              15             56             78             29             35             426            628(3 行受影响)*/