上海 A 16
上海 B 8
上海 C 19
上海 D 30
北京    A 19
北京    B 5
北京    C 7
北京    D 18
深圳   A 62
深圳   B 38
深圳   C 29
 
我现在想把他转换成行不知道可有什么方法。。?
 
区域    A        B         C            D
上海    16        8         19          30
北京    19       5         7            18
深圳  62          38      29            0不知道这样的可有什么方法??

解决方案 »

  1.   

    --行列互转
    /******************************************************************************************************************************************************
    以学生成绩为例子,比较形象易懂整理人:中国风(Roy)日期:2008.06.06
    ******************************************************************************************************************************************************/--1、行互列
    --> --> (Roy)生成測試數據
     
    if not object_id('Class') is null
        drop table Class
    Go
    Create table Class([Student] nvarchar(2),[Course] nvarchar(2),[Score] int)
    Insert Class
    select N'张三',N'语文',78 union all
    select N'张三',N'数学',87 union all
    select N'张三',N'英语',82 union all
    select N'张三',N'物理',90 union all
    select N'李四',N'语文',65 union all
    select N'李四',N'数学',77 union all
    select N'李四',N'英语',65 union all
    select N'李四',N'物理',85 
    Go
    --2000方法:
    动态:declare @s nvarchar(4000)
    set @s=''
    Select     @s=@s+','+quotename([Course])+'=max(case when [Course]='+quotename([Course],'''')+' then [Score] else 0 end)'
    from Class group by[Course]
    exec('select [Student]'+@s+' from Class group by [Student]')
    生成静态:select 
        [Student],
        [数学]=max(case when [Course]='数学' then [Score] else 0 end),
        [物理]=max(case when [Course]='物理' then [Score] else 0 end),
        [英语]=max(case when [Course]='英语' then [Score] else 0 end),
        [语文]=max(case when [Course]='语文' then [Score] else 0 end) 
    from 
        Class 
    group by [Student]GO
    动态:declare @s nvarchar(4000)
    Select     @s=isnull(@s+',','')+quotename([Course]) from Class group by[Course]
    exec('select * from Class pivot (max([Score]) for [Course] in('+@s+'))b')生成静态:
    select * 
    from 
        Class 
    pivot 
        (max([Score]) for [Course] in([数学],[物理],[英语],[语文]))b生成格式:
    /*
    Student 数学          物理          英语          语文
    ------- ----------- ----------- ----------- -----------
    李四      77          85          65          65
    张三      87          90          82          78(2 行受影响)
    */------------------------------------------------------------------------------------------
    go
    --加上总成绩(学科平均分)--2000方法:
    动态:declare @s nvarchar(4000)
    set @s=''
    Select     @s=@s+','+quotename([Course])+'=max(case when [Course]='+quotename([Course],'''')+' then [Score] else 0 end)'
    from Class group by[Course]
    exec('select [Student]'+@s+',[总成绩]=sum([Score])  from Class group by [Student]')--加多一列(学科平均分用avg([Score]))生成动态:select 
        [Student],
        [数学]=max(case when [Course]='数学' then [Score] else 0 end),
        [物理]=max(case when [Course]='物理' then [Score] else 0 end),
        [英语]=max(case when [Course]='英语' then [Score] else 0 end),
        [语文]=max(case when [Course]='语文' then [Score] else 0 end),
        [总成绩]=sum([Score]) --加多一列(学科平均分用avg([Score]))
    from 
        Class 
    group by [Student]go--2005方法:动态:declare @s nvarchar(4000)
    Select     @s=isnull(@s+',','')+quotename([Course]) from Class group by[Course] --isnull(@s+',','') 去掉字符串@s中第一个逗号
    exec('select [Student],'+@s+',[总成绩] from (select *,[总成绩]=sum([Score])over(partition by [Student]) from Class) a 
    pivot (max([Score]) for [Course] in('+@s+'))b ')生成静态:select 
        [Student],[数学],[物理],[英语],[语文],[总成绩] 
    from 
        (select *,[总成绩]=sum([Score])over(partition by [Student]) from Class) a --平均分时用avg([Score])
    pivot 
        (max([Score]) for [Course] in([数学],[物理],[英语],[语文]))b 生成格式:/*
    Student 数学          物理          英语          语文          总成绩
    ------- ----------- ----------- ----------- ----------- -----------
    李四      77          85          65          65          292
    张三      87          90          82          78          337(2 行受影响)
    */go--2、列转行
    --> --> (Roy)生成測試數據
     
    if not object_id('Class') is null
        drop table Class
    Go
    Create table Class([Student] nvarchar(2),[数学] int,[物理] int,[英语] int,[语文] int)
    Insert Class
    select N'李四',77,85,65,65 union all
    select N'张三',87,90,82,78
    Go--2000:动态:declare @s nvarchar(4000)
    select @s=isnull(@s+' union all ','')+'select [Student],[Course]='+quotename(Name,'''')--isnull(@s+' union all ','') 去掉字符串@s中第一个union all
    +',[Score]='+quotename(Name)+' from Class'
    from syscolumns where ID=object_id('Class') and Name not in('Student')--排除不转换的列
    order by Colid
    exec('select * from ('+@s+')t order by [Student],[Course]')--增加一个排序生成静态:
    select * 
    from (select [Student],[Course]='数学',[Score]=[数学] from Class union all 
    select [Student],[Course]='物理',[Score]=[物理] from Class union all 
    select [Student],[Course]='英语',[Score]=[英语] from Class union all 
    select [Student],[Course]='语文',[Score]=[语文] from Class)t 
    order by [Student],[Course]go
    --2005:动态:declare @s nvarchar(4000)
    select @s=isnull(@s+',','')+quotename(Name)
    from syscolumns where ID=object_id('Class') and Name not in('Student') 
    order by Colid
    exec('select Student,[Course],[Score] from Class unpivot ([Score] for [Course] in('+@s+'))b')go
    select 
        Student,[Course],[Score] 
    from 
        Class 
    unpivot 
        ([Score] for [Course] in([数学],[物理],[英语],[语文]))b生成格式:
    /*
    Student Course Score
    ------- ------- -----------
    李四      数学      77
    李四      物理      85
    李四      英语      65
    李四      语文      65
    张三      数学      87
    张三      物理      90
    张三      英语      82
    张三      语文      78(8 行受影响)
    */
      

  2.   


    select 区域 , 
    A=max(case when col2='A' THEN COL3 ELSE 0 END),
    B=max(case when col2='B' THEN COL3 ELSE 0 END),
    C=max(case when col2='C' THEN COL3 ELSE 0 END),
    D=max(case when col2='D' THEN COL3 ELSE 0 END)
    FROM TB
    GROUP BY 区域 
      

  3.   

    -------------------------------------------
    --  Author : liangCK 小梁
    --  Comment: 小梁 爱 兰儿
    --  Date   : 2009-09-10 14:11:23
    -------------------------------------------
     
    --> 生成测试数据: @tb
    DECLARE @tb TABLE (区域 VARCHAR(4),名称 VARCHAR(1),值 INT)
    INSERT INTO @tb
    SELECT '上海','A',16 UNION ALL
    SELECT '上海','B',8 UNION ALL
    SELECT '上海','C',19 UNION ALL
    SELECT '上海','D',30 UNION ALL
    SELECT '北京','A',19 UNION ALL
    SELECT '北京','B',5 UNION ALL
    SELECT '北京','C',7 UNION ALL
    SELECT '北京','D',18 UNION ALL
    SELECT '深圳','A',62 UNION ALL
    SELECT '深圳','B',38 UNION ALL
    SELECT '深圳','C',29--SQL查询如下:SELECT *
    FROM @tb 
        PIVOT(SUM(值) FOR 名称 IN(A,B,C,D)) AS pvt/*
    区域   A           B           C           D
    ---- ----------- ----------- ----------- -----------
    北京   19          5           7           18
    上海   16          8           19          30
    深圳   62          38          29          NULL(3 row(s) affected)
    */
      

  4.   

    ------------------------------------
    -- Author: flystone 
    -- Version:V1.001  
    -- Date:2009-09-10 14:10:27
    -------------------------------------- Test Data: ta
    If object_id('ta') is not null 
        Drop table ta
    Go
    Create table ta(col1 nvarchar(2),col2 nvarchar(1),col3 int)
    Go
    Insert into ta
     select '上海','A',16 union all
     select '上海','B',8 union all
     select '上海','C',19 union all
     select '上海','D',30 union all
     select '北京','A',19 union all
     select '北京','B',5 union all
     select '北京','C',7 union all
     select '北京','D',18 union all
     select '深圳','A',62 union all
     select '深圳','B',38 union all
     select '深圳','C',29 
    Go
    --Start
    declare @s varchar(8000)
    select @s = isnull(@s+',','')+'['+col2+']=max(case when col2='''+col2+''' then col3 else 0 end)' from (select distinct col2 from ta) aexec('select col1,'+@s+' from ta group by col1')
    --Result:
    /*
    col1 A           B           C           D           
    ---- ----------- ----------- ----------- ----------- 
    北京   19          5           7           18
    上海   16          8           19          30
    深圳   62          38          29          0
    */
    --End 
      

  5.   


    if object_id('TB')is not null drop table TB
    go 
    create table TB(区域 varchar(10) ,COL2 varchar(20) ,COL3 INT)
    insert into TB  SELECT 
    '上海', 'A', 16  UNION ALL SELECT
    '上海', 'B', 8 UNION ALL SELECT
    '上海', 'C', 19 UNION ALL SELECT
    '上海', 'D', 30 UNION ALL SELECT
    '北京', 'A', 19 UNION ALL SELECT
    '北京', 'B', 5 UNION ALL SELECT
    '北京', 'C', 7 UNION ALL SELECT
    '北京', 'D', 18 UNION ALL SELECT
    '深圳',  'A', 62 UNION ALL SELECT
    '深圳',  'B', 38 UNION ALL SELECT
    '深圳',  'C', 29 select 区域 , 
    A=max(case when col2='A' THEN COL3 ELSE 0 END),
    B=max(case when col2='B' THEN COL3 ELSE 0 END),
    C=max(case when col2='C' THEN COL3 ELSE 0 END),
    D=max(case when col2='D' THEN COL3 ELSE 0 END)
    FROM TB
    GROUP BY 区域 区域         A           B           C           D
    ---------- ----------- ----------- ----------- -----------
    北京         19          5           7           18
    上海         16          8           19          30
    深圳         62          38          29          0(3 行受影响)
      

  6.   

    ----------------------------------------------------------------
    -- Author  :fredrickhu(我是小F,向高手学习)
    -- Date    :2009-09-10 14:09:39
    -- Version:
    --      Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86) 
    -- Nov 24 2008 13:01:59 
    -- Copyright (c) 1988-2005 Microsoft Corporation
    -- Developer Edition on Windows NT 5.2 (Build 3790: Service Pack 1)
    --
    ----------------------------------------------------------------
    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    go 
    create table [tb]([col1] varchar(4),[col2] varchar(1),[col3] int)
    insert [tb]
    select '上海','A',16 union all
    select '上海','B',8 union all
    select '上海','C',19 union all
    select '上海','D',30 union all
    select '北京','A',19 union all
    select '北京','B',5 union all
    select '北京','C',7 union all
    select '北京','D',18 union all
    select '深圳','A',62 union all
    select '深圳','B',38 union all
    select '深圳','C',29
    --------------开始查询--------------------------
    declare @s nvarchar(4000)
    set @s=''
    Select     @s=@s+','+quotename([col2])+'=max(case when [col2]='+quotename([col2],'''')+' then [col3] else 0 end)'
    from tb group by[col2]
    exec('select [col1]'+@s+' from tb group by [col1]')
    ----------------结果----------------------------
    /* col1 A           B           C           D
    ---- ----------- ----------- ----------- -----------
    北京   19          5           7           18
    上海   16          8           19          30
    深圳   62          38          29          0(3 行受影响)*/
      

  7.   

    if object_ID('TB') IS NOT NULL DROP TABLE TB
      create table tb(a nvarchar(10),b varchar(10),value decimal(14,2))
    insert tb
    select  '上海', 'A', 16 union all select 
    '上海', 'B', 8     union all select 
    '上海', 'C', 19 union all select 
    '上海', 'D', 30 union all select 
    '北京',   'A', 19 union all select 
    '北京',   'B', 5 union all select 
    '北京',   'C', 7 union all select 
    '北京',   'D', 18 union all select 
    '深圳',  'A', 62 union all select 
    '深圳',  'B', 38 union all select 
    '深圳',  'C', 29 
    declare @sql nvarchar(4000)
    SET @sql=N'select [a]'   --初始化变量必须
    select @sql=@sql+N','+
                      QUOTENAME(b,N'')+
                        N'=max(
                                case when [b]='+quotename(b,N'''')
                                +N' then value else 0 end)'
                        from tb group by b 
           print @sql                      
       
    exec(@sql+N' from tb group by a')
    /*
    a A B C D
    北京 19.00 5.00 7.00 18.00
    上海 16.00 8.00 19.00 30.00
    深圳 62.00 38.00 29.00 0.00
    */
      

  8.   

    if object_ID('TB') IS NOT NULL DROP TABLE TB
      create table tb(a nvarchar(10),b varchar(10),value decimal(14,2))
    insert tb
    select  '上海', 'A', 16 union all select 
    '上海', 'B', 8     union all select 
    '上海', 'C', 19 union all select 
    '上海', 'D', 30 union all select 
    '北京',   'A', 19 union all select 
    '北京',   'B', 5 union all select 
    '北京',   'C', 7 union all select 
    '北京',   'D', 18 union all select 
    '深圳',  'A', 62 union all select 
    '深圳',  'B', 38 union all select 
    '深圳',  'C', 29 
    declare @sql nvarchar(4000)
    SET @sql=N'select [a]'   --初始化变量必须
    select @sql=@sql+N','+
                      QUOTENAME(b,N'')+
                        N'=max(
                                case when [b]='+quotename(b,N'''')
                                +N' then value else 0 end)'
                        from tb group by b 
           print @sql                      
       
    exec(@sql+N' from tb group by a')/*
    select [a],[A]=max(
                                case when [b]='A' then value else 0 end),[B]=max(
                                case when [b]='B' then value else 0 end),[C]=max(
                                case when [b]='C' then value else 0 end),[D]=max(
                                case when [b]='D' then value else 0 end)
                                */
    /*
    a A B C D
    北京 19.00 5.00 7.00 18.00
    上海 16.00 8.00 19.00 30.00
    深圳 62.00 38.00 29.00 0.00
    */
      

  9.   

    这两天学了不少东西 哈哈 create table tb(区域 nvarchar(20),col nvarchar(20),num int)insert into tb 
    select '上海' ,'A', 16  union all
    select '上海' ,'B', 8  union all
    select '上海' ,'C', 19  union all
    select '上海' ,'D', 30  union all
    select '北京'   , 'A', 19  union all
    select '北京'    ,'B', 5  union all
    select '北京'    ,'C', 7  union all
    select '北京'    ,'D', 18  union all
    select '深圳'  ,'A', 62  union all
    select '深圳'  ,'B', 38  union all
    select '深圳'  ,'C', 29 declare @sql varchar(8000)
    set @sql = 'select 区域 '
    select @sql = @sql + ' , max(case col when '''+ col+''' then num else 0 end) [' + col + ']'
    from (select distinct col from tb) as a
    set @sql = @sql + ' from tb group by 区域'print @sql
    exec(@sql) 
      

  10.   

    --> 生成测试数据表:tbIF NOT OBJECT_ID('[tb]') IS NULL
    DROP TABLE [tb]
    GO
    CREATE TABLE [tb]([区域] nvarchar(2),[col1] nvarchar(1),[col2] int)
    INSERT [tb]
    SELECT N'上海',N'A',16 UNION ALL
    SELECT N'上海',N'B',8 UNION ALL
    SELECT N'上海',N'C',19 UNION ALL
    SELECT N'上海',N'D',30 UNION ALL
    SELECT N'北京',N'A',19 UNION ALL
    SELECT N'北京',N'B',5 UNION ALL
    SELECT N'北京',N'C',7 UNION ALL
    SELECT N'北京',N'D',18 UNION ALL
    SELECT N'深圳',N'A',62 UNION ALL
    SELECT N'深圳',N'B',38 UNION ALL
    SELECT N'深圳',N'C',29
    GO
    --SELECT * FROM [tb]-->SQL查询如下:
    DECLARE @s VARCHAR(max)
    SELECT @s=ISNULL(@s+',','')+QUOTENAME(col1) FROM tb GROUP BY col1
    SELECT @s='
    SELECT * 
    FROM tb 
    PIVOT (MAX(col2) FOR col1 IN('+@s+'))b'
    EXEC(@s)
    /*
    区域   A           B           C           D
    ---- ----------- ----------- ----------- -----------
    北京   19          5           7           18
    上海   16          8           19          30
    深圳   62          38          29          NULL(3 行受影响)
    */
      

  11.   


     select * from 
      ta pivot
      (sum(col3) for col2 in (A,B,C))
      as pvt