项目编号    年     月       到位           完成
   1        2000   1         45            879
   2        2000   2         4578          1321 
   3        2000   3         789            789
   4        2000   4         4578          1321 
   5        2000   5         45128         111321 
项目编号为 nvarchar
年月 为 int
到位  完成 为  float
现在想通过转置的方式
项目编号    年      ?         1月   2月   3月    4月   5月  
   1        2000   到位      45   4578  789    4578  1321
   2        2000   完成      879  1321  789   45128  111321      
             

解决方案 »

  1.   

    ----------------------------------------------------------------
    -- Author  :SQL77(只为思齐老)
    -- Date    :2010-03-04 19:54:38
    -- Version:
    --      Microsoft SQL Server  2000 - 8.00.194 (Intel X86) 
    -- Aug  6 2000 00:57:48 
    -- Copyright (c) 1988-2000 Microsoft Corporation
    -- Desktop Engine on Windows NT 5.1 (Build 2600: Service Pack 3)
    --
    ----------------------------------------------------------------
    --> 测试数据:#TB
    if object_id('tempdb.dbo.#TB') is not null drop table #TB
    go 
    create table #TB([项目编号] int,[年] int,[月] int,[到位] int,[完成] int)
    insert #TB
    select 1,2000,1,45,879 union all
    select 2,2000,2,4578,1321 union all
    select 3,2000,3,789,789 union all
    select 4,2000,4,4578,1321 union all
    select 5,2000,5,45128,111321
    --------------开始查询--------------------------
    SELECT [年],
    SUM(CASE WHEN 月=1 THEN 到位 ELSE 0 END)AS '1',
    SUM(CASE WHEN 月=2 THEN 到位 ELSE 0 END)AS '2',
    SUM(CASE WHEN 月=3 THEN 到位 ELSE 0 END)AS '3',
    SUM(CASE WHEN 月=4 THEN 到位 ELSE 0 END)AS '4',
    SUM(CASE WHEN 月=5 THEN 到位 ELSE 0 END)AS '5',
    FLAGFROM 
    (
    select [项目编号] ,[年] ,[月] ,[到位],'到位' FLAG from #TB
    UNION ALL
    select [项目编号] ,[年] ,[月] ,[完成],'完成' from #TB
    )AS T GROUP BY [年],FLAG
    ----------------结果----------------------------
    /* (所影响的行数为 5 行)年           1           2           3           4           5           FLAG 
    ----------- ----------- ----------- ----------- ----------- ----------- ---- 
    2000        45          4578        789         4578        45128       到位
    2000        879         1321        789         1321        111321      完成(所影响的行数为 2 行)
    */
      

  2.   

    ----------------------------------------------------------------
    -- Author  :SQL77(只为思齐老)
    -- Date    :2010-03-04 19:54:38
    -- Version:
    --      Microsoft SQL Server  2000 - 8.00.194 (Intel X86) 
    -- Aug  6 2000 00:57:48 
    -- Copyright (c) 1988-2000 Microsoft Corporation
    -- Desktop Engine on Windows NT 5.1 (Build 2600: Service Pack 3)
    --
    ----------------------------------------------------------------
    --> 测试数据:#TB
    if object_id('tempdb.dbo.#TB') is not null drop table #TB
    go 
    create table #TB([项目编号] int,[年] int,[月] int,[到位] int,[完成] int)
    insert #TB
    select 1,2000,1,45,879 union all
    select 2,2000,2,4578,1321 union all
    select 3,2000,3,789,789 union all
    select 4,2000,4,4578,1321 union all
    select 5,2000,5,45128,111321
    --------------开始查询--------------------------
    SELECT [年],FLAG,
    SUM(CASE WHEN 月=1 THEN 到位 ELSE 0 END)AS '1',
    SUM(CASE WHEN 月=2 THEN 到位 ELSE 0 END)AS '2',
    SUM(CASE WHEN 月=3 THEN 到位 ELSE 0 END)AS '3',
    SUM(CASE WHEN 月=4 THEN 到位 ELSE 0 END)AS '4',
    SUM(CASE WHEN 月=5 THEN 到位 ELSE 0 END)AS '5'
    FROM 
    (
    select [项目编号] ,[年] ,[月] ,[到位],'到位' FLAG from #TB
    UNION ALL
    select [项目编号] ,[年] ,[月] ,[完成],'完成' from #TB
    )AS T GROUP BY [年],FLAG
    ----------------结果----------------------------
    /* (所影响的行数为 5 行)年           FLAG 1           2           3           4           5           
    ----------- ---- ----------- ----------- ----------- ----------- ----------- 
    2000        到位   45          4578        789         4578        45128
    2000        完成   879         1321        789         1321        111321(所影响的行数为 2 行)
    */
      

  3.   

    create table TB([项目编号] int,[年] int,[月] int,[到位] int,[完成] int)
    insert TB
    select 1,2000,1,45,879 union all
    select 2,2000,2,4578,1321 union all
    select 3,2000,3,789,789 union all
    select 4,2000,4,4578,1321 union all
    select 5,2000,5,45128,111321
    goselect 项目编号 = 1 , 年 , flag = '到位',
      max(case 月 when 1 then 到位 else 0 end) [1月],
      max(case 月 when 2 then 到位 else 0 end) [2月],
      max(case 月 when 3 then 到位 else 0 end) [3月],
      max(case 月 when 4 then 到位 else 0 end) [4月],
      max(case 月 when 5 then 到位 else 0 end) [5月]
    from tb group by 年
    union 
    select 项目编号 = 2 , 年 , flag = '完成',
      max(case 月 when 1 then 完成 else 0 end) [1月],
      max(case 月 when 2 then 完成 else 0 end) [2月],
      max(case 月 when 3 then 完成 else 0 end) [3月],
      max(case 月 when 4 then 完成 else 0 end) [4月],
      max(case 月 when 5 then 完成 else 0 end) [5月]
    from tb group by 年drop table tb/*
    项目编号        年           flag 1月          2月          3月          4月          5月          
    ----------- ----------- ---- ----------- ----------- ----------- ----------- ----------- 
    1           2000        到位   45          4578        789         4578        45128
    2           2000        完成   879         1321        789         1321        111321(所影响的行数为 2 行)
    */