google “sqlserver 行转列”
或者写个存储过程,通过游标遍历委托方,拼接sql,组成你想要的数据集

解决方案 »

  1.   

    简单的行转列,你自己改改
    create table tb(name varchar(10) , kch varchar(10) , fshu int)
    insert into tb values('张三' , '语文' , 74)
    insert into tb values('张三' , '数学' , 83)
    insert into tb values('张三' , '物理' , 93)
    insert into tb values('李四' , '语文' , 74)
    insert into tb values('李四' , '数学' , 84)
    insert into tb values('李四' , '物理' , 94)
    GO--姓名 语文 数学 物理 
    --李四 74   84   94
    --张三 74   83   93
    select name as 姓名 ,
      max(case kch when '语文' then fshu else 0 end) 语文,
      max(case kch when '数学' then fshu else 0 end) 数学,
      max(case kch when '物理' then fshu else 0 end) 物理
    from tb
    group by NAME
      

  2.   

    自定义一张表
    把读出来的DataTable解析到自定义的表中
      

  3.   


    Consignor              票数      应收               应付                 利润         进出口
    青岛川汇国际物流有限公司 28 6851.0000 4153.6400 2697.3600 0
    青岛正乐食品有限公司 30 2438.0000 536.3600 1901.6400 0
    青岛正乐食品有限公司 30 720.0000 139.9200 580.0800 1
    万华化学集团股份有限公司 8 840.0000 186.5600 653.4400 0
    青岛中远国际货运有限公司 253 98838.0000 26353.2800 72484.7200 0
    青岛中远国际货运有限公司 253 6020.0000 146.6400 5873.3600 1
    青岛中远国际货运有限公司济南分公司 18 2398.3200 373.1200 2025.2000 0
      

  4.   

    自定义一张表
    把读出来的DataTable解析到自定义的表中
      

  5.   

    create table #temp
    (
    Consignor varchar(100),
    票数 int,
    应收 decimal(18,4),
    应付 decimal(18,4),
    利润 decimal(18,4),
    进出口 varchar(1)
    )
    --Consignor              票数      应收               应付                 利润         进出口
    select  '青岛川汇国际物流有限公司', 28 ,6851.0000, 4153.6400, 2697.3600, '0'
    union all
    select '青岛正乐食品有限公司' ,30, 2438.0000, 536.3600, 1901.6400, '0'
    union all
    select '青岛正乐食品有限公司', 30 ,720.0000 ,139.9200 ,580.0800 ,'1'
    select '万华化学集团股份有限公司', 8, 840.0000, 186.5600, 653.4400, '0'
    union all
    select '青岛中远国际货运有限公司' ,253 ,98838.0000 ,26353.2800 ,72484.7200 ,'0'
    union all 
    select '青岛中远国际货运有限公司' ,253, 6020.0000, 146.6400, 5873.3600 ,'1'
    union all
    select '青岛中远国际货运有限公司济南分公司', 18 ,2398.3200, 373.1200 ,2025.2000 ,'0'
      

  6.   

    这样?--create table #temp
    --(
    --Consignor varchar(100),
    --票数 int,
    --应收 decimal(18,4),
    --应付 decimal(18,4),
    --利润 decimal(18,4),
    --进出口 varchar(1)
    --)
    --INSERT INTO #temp
    ----Consignor              票数      应收               应付                 利润         进出口
    --select  '青岛川汇国际物流有限公司',    28    ,6851.0000,    4153.6400,    2697.3600,    '0'
    --union all
    --select '青岛正乐食品有限公司'    ,30,    2438.0000,    536.3600,    1901.6400,    '0'
    --union all
    --select '青岛正乐食品有限公司',    30    ,720.0000    ,139.9200    ,580.0800    ,'1'
    --UNION ALL 
    --select '万华化学集团股份有限公司',    8,    840.0000,    186.5600,    653.4400,    '0'
    --union all
    --select '青岛中远国际货运有限公司'    ,253    ,98838.0000    ,26353.2800    ,72484.7200    ,'0'
    --union all 
    --select '青岛中远国际货运有限公司'    ,253,    6020.0000,    146.6400,    5873.3600    ,'1'
    --union all
    --select '青岛中远国际货运有限公司济南分公司',    18    ,2398.3200,    373.1200    ,2025.2000    ,'0'--SELECT consignor,票数,利润,进出口  FROM #tempdeclare @s nvarchar(MAX)
    DECLARE @s1 NVARCHAR(max)
    set @s=''
    SET @s1=''
    Select     @s=@s+','+quotename(consignor)+'=sum(case when [consignor]='+quotename(consignor,'''')+' and [进出口]='+quotename(进出口,'''')+' then [票数] else 0 end)'
    --+','+quotename(consignor)+'=sum(case when [consignor]='+quotename(consignor,'''')+' and [进出口]='+quotename(进出口,'''')+' then [利润] else 0 end)'
    from #temp group by [进出口],consignor
    Select     @s1=@s1+','+quotename(consignor)+'=sum(case when [consignor]='+quotename(consignor,'''')+' and [进出口]='+quotename(进出口,'''')+' then [利润] else 0 end)'
    --+','+quotename(consignor)+'=sum(case when [consignor]='+quotename(consignor,'''')+' and [进出口]='+quotename(进出口,'''')+' then [利润] else 0 end)'
    from #temp group by [进出口],consignor
    exec('select [进出口],''票数''  '+@s+' from #temp group by [进出口] union all select [进出口],''利润'''+@s1+' from #temp group by [进出口] order by 进出口 ')/*
    进出口       青岛川汇国际物流有限公司                            青岛正乐食品有限公司                              青岛中远国际货运有限公司                            青岛中远国际货运有限公司济南分公司                       万华化学集团股份有限公司                            青岛正乐食品有限公司                              青岛中远国际货运有限公司
    ---- ---- --------------------------------------- --------------------------------------- --------------------------------------- --------------------------------------- --------------------------------------- --------------------------------------- ---------------------------------------
    0    利润   5394.7200                               3803.2800                               72484.7200                              2025.2000                               653.4400                                0.0000                                  0.0000
    0    票数   56.0000                                 60.0000                                 253.0000                                18.0000                                 8.0000                                  0.0000                                  0.0000
    1    利润   0.0000                                  0.0000                                  0.0000                                  0.0000                                  0.0000                                  1160.1600                               5873.3600
    1    票数   0.0000                                  0.0000                                  0.0000                                  0.0000                                  0.0000                                  60.0000                                 253.0000*/
      

  7.   

    我能 补问个问题吗?
    sql 列转行 遇到 的值 有 括号怎么办 啊 
      

  8.   


    declare @s nvarchar(MAX)
    DECLARE @s1 NVARCHAR(max)
    set @s=''
    SET @s1=''
    Select     @s=@s+','+quotename(consignor)+'=MAX(case when [consignor]='+quotename(consignor,'''')+' and [进出口]='+quotename(进出口,'''')+' then [票数] else 0 end)'
    --+','+quotename(consignor)+'=sum(case when [consignor]='+quotename(consignor,'''')+' and [进出口]='+quotename(进出口,'''')+' then [利润] else 0 end)'
    from #temp group by [进出口],consignor
    Select     @s1=@s1+','+quotename(consignor)+'=MAX(case when [consignor]='+quotename(consignor,'''')+' and [进出口]='+quotename(进出口,'''')+' then [利润] else 0 end)'
    --+','+quotename(consignor)+'=sum(case when [consignor]='+quotename(consignor,'''')+' and [进出口]='+quotename(进出口,'''')+' then [利润] else 0 end)'
    from #temp group by [进出口],consignor
    exec('select [进出口],''票数''  '+@s+' from #temp group by [进出口] union all select [进出口],''利润'''+@s1+' from #temp group by [进出口] order by 进出口 ')