SQL语句中怎么把列的值横向显示,因为列的值是动态的. customernane salesamont monthtext 
aaa          1000      1 
bbb          1000      1 
ccc          1000      1 
我要得到结果为: 
                  customername            total 
monthtext    aaa        bbb      ccc    
  1          1000        1000      1000    3000 但我的customername的值可能还有其他的.怎么写sql语句

解决方案 »

  1.   

    行列互转:http://blog.csdn.net/htl258/archive/2009/03/02/3947993.aspx
      

  2.   

    http://topic.csdn.net/u/20090923/08/f918661d-e68c-447d-9563-755b61f05509.html
      

  3.   

    行列转换  
    sum(case..when ...then ..end)
      

  4.   

    --sql 2000
    declare @sql varchar(8000)
    set @sql = 'select monthtext '
    select @sql = @sql + ' , max(case customernane when ''' + customernane + ''' then salesamont  else 0 end) [' + customernane + ']'
    from (select distinct customernane from tb) as a
    set @sql = @sql + ' , sum(salesamont) total from tb group by monthtext'
    exec(@sql) 
      

  5.   

    ----------------------------------------------------------------
    -- Author  :fredrickhu(我是小F,向高手学习)
    -- Date    :2009-09-23 15:17:17
    -- 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]([customernane] varchar(3),[salesamont] int,[monthtext] int)
    insert [tb]
    select 'aaa',1000,1 union all
    select 'bbb',1000,1 union all
    select 'ccc',1000,1
    --------------开始查询--------------------------
    declare @sql varchar(8000) 
    set @sql = 'select monthtext ' 
    select @sql = @sql + ' , max(case customernane when ''' + customernane + ''' then salesamont  else 0 end) [' + customernane + ']' 
    from (select distinct customernane from tb) as a 
    set @sql = @sql + ' , sum(salesamont) total from tb group by monthtext' 
    exec(@sql) 
    ----------------结果----------------------------
    /* monthtext   aaa         bbb         ccc         total
    ----------- ----------- ----------- ----------- -----------
    1           1000        1000        1000        3000(1 行受影响)
    */
      

  6.   

    create table tb(customernane varchar(10) , salesamont int,monthtext int)
    insert into tb values('aaa' ,         1000  ,    1 )
    insert into tb values('bbb' ,         1000  ,    1 )
    insert into tb values('ccc' ,         1000  ,    1 )
    go--sql 2000
    declare @sql varchar(8000)
    set @sql = 'select monthtext '
    select @sql = @sql + ' , max(case customernane when ''' + customernane + ''' then salesamont  else 0 end) [' + customernane + ']'
    from (select distinct customernane from tb) as a
    set @sql = @sql + ' , sum(salesamont) total from tb group by monthtext'
    exec(@sql) drop table tb/*
    monthtext   aaa         bbb         ccc         total       
    ----------- ----------- ----------- ----------- ----------- 
    1           1000        1000        1000        3000
    */
      

  7.   

    --------------------------------------------------------------------------
    --  Author : htl258(Tony)
    --  Date   : 2009-09-23 15:18:41
    --  Version:Microsoft SQL Server 2008 (SP1) - 10.0.2531.0 (Intel X86) 
    --          Mar 29 2009 10:27:29 
    --          Copyright (c) 1988-2008 Microsoft Corporation
    --          Enterprise Evaluation Edition on Windows NT 5.1 <X86> (Build 2600: Service Pack 2)
    --------------------------------------------------------------------------
    --> 生成测试数据表:tbIF NOT OBJECT_ID('[tb]') IS NULL
    DROP TABLE [tb]
    GO
    CREATE TABLE [tb]([customernane] NVARCHAR(10),[salesamont] INT,[monthtext] INT)
    INSERT [tb]
    SELECT 'aaa',1000,1 UNION ALL
    SELECT 'bbb',1000,1 UNION ALL
    SELECT 'ccc',1000,1
    GO
    --SELECT * FROM [tb]-->SQL查询如下:
    DECLARE @s VARCHAR(8000)
    SELECT @s=ISNULL(@s,'select [monthtext]')+',max(case [customernane] when '''+[customernane]+''' then [salesamont] else 0 end)['+[customernane]+']'
    FROM tb GROUP BY [customernane]
    EXEC(@s+' from tb group by [monthtext]')
    /*
    monthtext   aaa         bbb         ccc
    ----------- ----------- ----------- -----------
    1           1000        1000        1000(1 行受影响)
    */
      

  8.   

    create table #tb (customernane varchar(10),salesamont int, monthtext int)
    insert #tb values('aaa',1000,      1 )
    insert #tb values('bbb',1000,      1 )
    insert #tb values('ccc',1000 ,     1 )
    --查询
    declare @col varchar(200),@sql varchar(300)
    select @col=isnull(@col+',','')+'['+customernane+']' from #tb group by customernaneset @sql='SELECT monthtext, '+@col+'
        FROM
            (SELECT monthtext,salesamont,customernane
                FROM #tb) AS SourceTable
        PIVOT
            (sum(salesamont)
                FOR customernane IN ('+@col+')
            ) AS PivotTable;'
    exec (@SQL)
    /*
    monthtext   aaa         bbb         ccc
    ----------- ----------- ----------- -----------
    1           1000        1000        1000
    */
      

  9.   

    ---2005
    ----------------------------------------------------------------
    -- Author  :fredrickhu(我是小F,向高手学习)
    -- Date    :2009-09-23 15:17:17
    -- 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]([customernane] varchar(3),[salesamont] int,[monthtext] int)
    insert [tb]
    select 'aaa',1000,1 union all
    select 'bbb',1000,1 union all
    select 'ccc',1000,1
    --------------开始查询--------------------------
    declare @sql varchar(8000)
    select @sql = isnull(@sql + '],[' , '') + customernane from tb group by customernane
    set @sql = '[' + @sql + ']'
    exec ('select * from (select * from tb) a pivot (max(salesamont) for customernane in (' + @sql + ')) b')
    ----------------结果----------------------------
    /* monthtext   aaa         bbb         ccc         total
    ----------- ----------- ----------- ----------- -----------
    1           1000        1000        1000        3000(1 行受影响)
    */
      

  10.   

    -- =========================================
    -- -----------t_mac 小编-------------------
       --------------------希望有天成为大虾---- 
    -- =========================================IF OBJECT_ID('tb') IS NOT NULL
    DROP TABLE tb
    GO
    CREATE TABLE tb( customernane varchar(10),salesamont int ,monthtext int )
    go
    insert tb SELECT 
    'aaa',          1000 ,     1 UNION ALL SELECT 
    'bbb',          1000 ,     1 UNION ALL SELECT 
    'ccc',          1000 ,     1 
    go
    declare @s nvarchar(4000)
    select @s=isnull(@s+',','')+' max(case when customernane='''+customernane +''' then salesamont else 0 end) as '+customernane
    from tb 
    exec('select monthtext, '+@s+' , sum(salesamont) as total from tb group by monthtext ' )
    /*
    monthtext   aaa         bbb         ccc         total
    ----------- ----------- ----------- ----------- -----------
    1           1000        1000        1000        3000*/
    go
    练练~
      

  11.   

    select customername,salesamont,monthtext
    from (select monthtext,aaa,bbb,ccc from Table) P
    Unpivot (salesamont for customername in (aaa,bbb,ccc))
    as UNPVT
      

  12.   

    -- =========================================
    -- -----------t_mac 小编-------------------
       --------------------希望有天成为大虾---- 
    -- =========================================IF OBJECT_ID('tb') IS NOT NULL
    DROP TABLE tb
    GO
    CREATE TABLE tb( customernane varchar(10),salesamont int ,monthtext int )
    go
    insert tb SELECT 
    'aaa',          1000 ,     1 UNION ALL SELECT 
    'bbb',          1000 ,     1 UNION ALL SELECT 
    'ccc',          1000 ,     1 union all select 
    'ccc',          1000 ,     2
    go
    declare @s varchar(8000)
    select @s=ISNULL(@s+',','')+ customernane from tb group by customernane
    print @s
    exec ('select p.*,total from tb pivot (max(salesamont) for customernane in ( '+@s+' ) )p 
    join (select SUM(salesamont) as total ,monthtext from tb group by monthtext) k 
    on p.monthtext=k.monthtext')/*
    monthtext   aaa         bbb         ccc         total
    ----------- ----------- ----------- ----------- -----------
    1           1000        1000        1000        3000
    2           NULL        NULL        1000        1000*/
    go
      

  13.   

    行列转换。e用case when ...语句。然后求和就行了。