--如果一对一的话是
select a.* , b.yfdx , b.yflx , b.yfje from 表一  a ,表二 b where a.orderid = b.orderid--如果一对多的话是
--sql 静态,指同一个orderid最多有2两个(或N个)
select m.* , 
max(case n.px when 1 then yfdx end) yfdx1,
max(case n.px when 1 then yflx end) yfdx1,
max(case n.px when 1 then yfje end) yfje1,
max(case n.px when 1 then yfdx end) yfdx2,
max(case n.px when 1 then yflx end) yfdx2,
max(case n.px when 1 then yfje end) yfje3
from 表一 m,
(select * , px = (select count(*) from 表二 where orderno = t.oderno and id < t.id) + 1 from 表二 t) n
where m.orderid = n.orderid
group by m.orderid , m.OrderNo , m.ContSize , m.ContClass , m.contno--sql动态, 指同一个orderid数量不定。
declare @sql varchar(8000)
set @sql = 'select m.orderid , m.OrderNo , m.ContSize , m.ContClass , m.contno '
select @sql = @sql + ' , max(case n.px when ''' + cast(px as varchar) + ''' then yfdx else 0 end) [yfdx' + cast(px as varchar) + ']'
                   + ' , max(case n.px when ''' + cast(px as varchar) + ''' then yfjx else 0 end) [yfjx' + cast(px as varchar) + ']'
                   + ' , max(case n.px when ''' + cast(px as varchar) + ''' then yfje else 0 end) [yfje' + cast(px as varchar) + ']'
from (select distinct px from (select * , px = (select count(*) from 表二 where orderno = t.oderno and id < t.id) + 1 from 表二 t) o) as a
set @sql = @sql + ' from 表一 m ,(select * , px = (select count(*) from 表二 where orderno = t.oderno and id < t.id) + 1 from 表二 t) n where m.orderid = n.orderid group by m.orderid , m.OrderNo , m.ContSize , m.ContClass , m.contno'
exec(@sql) 

解决方案 »

  1.   

    --以上可能存在手误,具体的方法见下:/*
    标题:普通行列转换(version 2.0)
    作者:爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开)
    时间:2008-03-09
    地点:广东深圳
    说明:普通行列转换(version 1.0)仅针对sql server 2000提供静态和动态写法,version 2.0增加sql server 2005的有关写法。问题:假设有张学生成绩表(tb)如下:
    姓名 课程 分数
    张三 语文 74
    张三 数学 83
    张三 物理 93
    李四 语文 74
    李四 数学 84
    李四 物理 94
    想变成(得到如下结果): 
    姓名 语文 数学 物理 
    ---- ---- ---- ----
    李四 74   84   94
    张三 74   83   93
    -------------------
    */create table tb(姓名 varchar(10) , 课程 varchar(10) , 分数 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--SQL SERVER 2000 静态SQL,指课程只有语文、数学、物理这三门课程。(以下同)
    select 姓名 as 姓名 ,
      max(case 课程 when '语文' then 分数 else 0 end) 语文,
      max(case 课程 when '数学' then 分数 else 0 end) 数学,
      max(case 课程 when '物理' then 分数 else 0 end) 物理
    from tb
    group by 姓名--SQL SERVER 2000 动态SQL,指课程不止语文、数学、物理这三门课程。(以下同)
    declare @sql varchar(8000)
    set @sql = 'select 姓名 '
    select @sql = @sql + ' , max(case 课程 when ''' + 课程 + ''' then 分数 else 0 end) [' + 课程 + ']'
    from (select distinct 课程 from tb) as a
    set @sql = @sql + ' from tb group by 姓名'
    exec(@sql) --SQL SERVER 2005 静态SQL。
    select * from (select * from tb) a pivot (max(分数) for 课程 in (语文,数学,物理)) b--SQL SERVER 2005 动态SQL。
    declare @sql varchar(8000)
    select @sql = isnull(@sql + '],[' , '') + 课程 from tb group by 课程
    set @sql = '[' + @sql + ']'
    exec ('select * from (select * from tb) a pivot (max(分数) for 课程 in (' + @sql + ')) b')---------------------------------/*
    问题:在上述结果的基础上加平均分,总分,得到如下结果:
    姓名 语文 数学 物理 平均分 总分 
    ---- ---- ---- ---- ------ ----
    李四 74   84   94   84.00  252
    张三 74   83   93   83.33  250
    */--SQL SERVER 2000 静态SQL。
    select 姓名 姓名,
      max(case 课程 when '语文' then 分数 else 0 end) 语文,
      max(case 课程 when '数学' then 分数 else 0 end) 数学,
      max(case 课程 when '物理' then 分数 else 0 end) 物理,
      cast(avg(分数*1.0) as decimal(18,2)) 平均分,
      sum(分数) 总分
    from tb
    group by 姓名--SQL SERVER 2000 动态SQL。
    declare @sql varchar(8000)
    set @sql = 'select 姓名 '
    select @sql = @sql + ' , max(case 课程 when ''' + 课程 + ''' then 分数 else 0 end) [' + 课程 + ']'
    from (select distinct 课程 from tb) as a
    set @sql = @sql + ' , cast(avg(分数*1.0) as decimal(18,2)) 平均分 , sum(分数) 总分 from tb group by 姓名'
    exec(@sql) --SQL SERVER 2005 静态SQL。
    select m.* , n.平均分 , n.总分 from
    (select * from (select * from tb) a pivot (max(分数) for 课程 in (语文,数学,物理)) b) m,
    (select 姓名 , cast(avg(分数*1.0) as decimal(18,2)) 平均分 , sum(分数) 总分 from tb group by 姓名) n
    where m.姓名 = n.姓名--SQL SERVER 2005 动态SQL。
    declare @sql varchar(8000)
    select @sql = isnull(@sql + ',' , '') + 课程 from tb group by 课程
    exec ('select m.* , n.平均分 , n.总分 from
    (select * from (select * from tb) a pivot (max(分数) for 课程 in (' + @sql + ')) b) m , 
    (select 姓名 , cast(avg(分数*1.0) as decimal(18,2)) 平均分 , sum(分数) 总分 from tb group by 姓名) n
    where m.姓名 = n.姓名')drop table tb ------------------
    ------------------/*
    问题:如果上述两表互相换一下:即表结构和数据为:
    姓名 语文 数学 物理
    张三 74  83  93
    李四 74  84  94
    想变成(得到如下结果): 
    姓名 课程 分数 
    ---- ---- ----
    李四 语文 74
    李四 数学 84
    李四 物理 94
    张三 语文 74
    张三 数学 83
    张三 物理 93
    --------------
    */create table tb(姓名 varchar(10) , 语文 int , 数学 int , 物理 int)
    insert into tb values('张三',74,83,93)
    insert into tb values('李四',74,84,94)
    go--SQL SERVER 2000 静态SQL。
    select * from
    (
     select 姓名 , 课程 = '语文' , 分数 = 语文 from tb 
     union all
     select 姓名 , 课程 = '数学' , 分数 = 数学 from tb
     union all
     select 姓名 , 课程 = '物理' , 分数 = 物理 from tb
    ) t
    order by 姓名 , case 课程 when '语文' then 1 when '数学' then 2 when '物理' then 3 end--SQL SERVER 2000 动态SQL。
    --调用系统表动态生态。
    declare @sql varchar(8000)
    select @sql = isnull(@sql + ' union all ' , '' ) + ' select 姓名 , [课程] = ' + quotename(Name , '''') + ' , [分数] = ' + quotename(Name) + ' from tb'
    from syscolumns 
    where name! = N'姓名' and ID = object_id('tb') --表名tb,不包含列名为姓名的其它列
    order by colid asc
    exec(@sql + ' order by 姓名 ')--SQL SERVER 2005 动态SQL。
    select 姓名 , 课程 , 分数 from tb unpivot (分数 for 课程 in([语文] , [数学] , [物理])) t--SQL SERVER 2005 动态SQL,同SQL SERVER 2000 动态SQL。--------------------
    /*
    问题:在上述的结果上加个平均分,总分,得到如下结果:
    姓名 课程   分数
    ---- ------ ------
    李四 语文   74.00
    李四 数学   84.00
    李四 物理   94.00
    李四 平均分 84.00
    李四 总分   252.00
    张三 语文   74.00
    张三 数学   83.00
    张三 物理   93.00
    张三 平均分 83.33
    张三 总分   250.00
    ------------------
    */select * from
    (
     select 姓名 as 姓名 , 课程 = '语文' , 分数 = 语文 from tb 
     union all
     select 姓名 as 姓名 , 课程 = '数学' , 分数 = 数学 from tb
     union all
     select 姓名 as 姓名 , 课程 = '物理' , 分数 = 物理 from tb
     union all
     select 姓名 as 姓名 , 课程 = '平均分' , 分数 = cast((语文 + 数学 + 物理)*1.0/3 as decimal(18,2)) from tb
     union all
     select 姓名 as 姓名 , 课程 = '总分' , 分数 = 语文 + 数学 + 物理 from tb
    ) t
    order by 姓名 , case 课程 when '语文' then 1 when '数学' then 2 when '物理' then 3 when '平均分' then 4 when '总分' then 5 enddrop table tb
      

  2.   

    select * , px = (select count(*) from 表二 where orderno = t.oderno and id < t.id) + 1 from 表二 t这里什么意思?这个语句肯定没办法运行的啊!表一表二关联的只有ORDERID一样
      

  3.   

     一对一的话是: 
    select a.orderid ,a.orderno ,a.contsize  ,a.contclass ,a.contno 
    ,sum(yfdx) as yfdx, sum(yflx) as yflx,  sum(yfje) as yfje
    from a
    left join b on a.orderid=b.orderid
    group by  a.orderid ,a.orderno ,a.contsize  ,a.contclass ,a.contno 
      

  4.   

    select * , px = (select count(*) from 表二 where orderid = t.orderid and id < t.id) + 1 
    from 表二 t 
    px是排名的一对多的话肯定要用到动态sql语句
      

  5.   

    我一楼写错个字段.(把oderno换成orderid)
    --如果一对一的话是
    select a.* , b.yfdx , b.yflx , b.yfje from 表一  a ,表二 b where a.orderid = b.orderid--如果一对多的话是
    --sql 静态,指同一个orderid最多有2两个(或N个)
    select m.* , 
    max(case n.px when 1 then yfdx end) yfdx1,
    max(case n.px when 1 then yflx end) yfdx1,
    max(case n.px when 1 then yfje end) yfje1,
    max(case n.px when 1 then yfdx end) yfdx2,
    max(case n.px when 1 then yflx end) yfdx2,
    max(case n.px when 1 then yfje end) yfje3
    from 表一 m,
    (select * , px = (select count(*) from 表二 where orderid = t.orderid and id < t.id) + 1 from 表二 t) n
    where m.orderid = n.orderid
    group by m.orderid , m.OrderNo , m.ContSize , m.ContClass , m.contno--sql动态, 指同一个orderid数量不定。
    declare @sql varchar(8000)
    set @sql = 'select m.orderid , m.OrderNo , m.ContSize , m.ContClass , m.contno '
    select @sql = @sql + ' , max(case n.px when ''' + cast(px as varchar) + ''' then yfdx else 0 end) [yfdx' + cast(px as varchar) + ']'
                       + ' , max(case n.px when ''' + cast(px as varchar) + ''' then yfjx else 0 end) [yfjx' + cast(px as varchar) + ']'
                       + ' , max(case n.px when ''' + cast(px as varchar) + ''' then yfje else 0 end) [yfje' + cast(px as varchar) + ']'
    from (select distinct px from (select * , px = (select count(*) from 表二 where orderid = t.orderid and id < t.id) + 1 from 表二 t) o) as a
    set @sql = @sql + ' from 表一 m ,(select * , px = (select count(*) from 表二 where orderid = t.orderid and id < t.id) + 1 from 表二 t) n where m.orderid = n.orderid group by m.orderid , m.OrderNo , m.ContSize , m.ContClass , m.contno'
    exec(@sql) 
      

  6.   


    declare @sql varchar(8000)
    set @sql = 'select m.orderid , m.orderno , m.ContSize , m.ContClass , m.contno '
    select @sql = @sql + ' , max(case n.px when ''' + cast(px as varchar) + ''' then yfdx else 0 end) [yfdx' + cast(px as varchar) + ']'
                       + ' , max(case n.px when ''' + cast(px as varchar) + ''' then yflx else 0 end) [yflx' + cast(px as varchar) + ']'
                       + ' , max(case n.px when ''' + cast(px as varchar) + ''' then yfje else 0 end) [yfje' + cast(px as varchar) + ']'
    from (select distinct px from (select * , px = (select count(*) from planyf where orderid = t.orderid and yfid < t.yfid) + 1 from planyf t) o) as a
    set @sql = @sql + ' from planorder m ,(select * , px = (select count(*) from planyf where orderid = t.orderid and yfid < t.yfid) + 1 from planyf t) n where m.orderid = n.orderid and m.orderid=2 group by m.orderid , m.OrderNo , m.ContSize , m.ContClass , m.contno'
    exec(@sql) 按照这个是可以运行了,但是因为YFDX这个字段是汉字,提示错误是
    服务器: 消息 245,级别 16,状态 1,行 1
    将 nvarchar 值 '郎杰' 转换为数据类型为 int 的列时发生语法错误。
    我郁闷了
      

  7.   

    但是因为YFDX这个字段是汉字,提示错误是 
    ------------是字段里面的内容是汉字
      

  8.   


    declare @sql varchar(8000)
    set @sql = 'select m.orderid , m.orderno , m.ContSize , m.ContClass , m.contno '
    select @sql = @sql + ' , max(case n.px when ''' + cast(px as varchar) + ''' then yfdx else '''' end) [yfdx' + cast(px as varchar) + ']'
                       + ' , max(case n.px when ''' + cast(px as varchar) + ''' then yflx else 0 end) [yflx' + cast(px as varchar) + ']'
                       + ' , max(case n.px when ''' + cast(px as varchar) + ''' then yfje else 0 end) [yfje' + cast(px as varchar) + ']'
    from (select distinct px from (select * , px = (select count(*) from planyf where orderid = t.orderid and yfid < t.yfid) + 1 from planyf t) o) as a
    set @sql = @sql + ' from planorder m ,(select * , px = (select count(*) from planyf where orderid = t.orderid and yfid < t.yfid) + 1 from planyf t) n where m.orderid = n.orderid and m.orderid=2 group by m.orderid , m.OrderNo , m.ContSize , m.ContClass , m.contno'
    exec(@sql) 
      

  9.   


    不好意思,继续问下
    你写的这个是可以运行了,而且是可以查询出来的,但是我现在又面临一个问题我先把里面查询出来的数据给看下
    6 C20071009006 20 GP TWCU2031240 .0000 .0000 .0000 .0000 .0000 .0000 郎杰 奖金 16.0000 .0000 王海涛 奖金 8.0000 .0000
    7 C20071009007 20 GP DJLU2412846 .0000 .0000 .0000 .0000 常贵华 提箱费 50.0000 .0000 常贵华 奖金 8.0000 .0000 姜继彬 奖金 16.0000 .0000
    8 C20071009008 20 GP SNTU2019662 .0000 .0000 .0000 .0000 王善芳 奖金 10.0000 .0000 姜继彬 提箱费 50.0000 .0000 姜继彬 奖金 6.0000 .0000
    9 C20071009009 40 GP TGHU4941650 .0000 .0000 .0000 .0000 沛龙车队 运费 500.0000 .0000 郎杰 提箱费 75.0000 .0000 郎杰 奖金 10.0000 .0000
    11 C20071009011 20 GP SKLU2541048 .0000 .0000 .0000 .0000 王善芳 奖金 10.0000 .0000 常贵华 提箱费 50.0000 .0000 常贵华 奖金 4.0000 .0000
    12 C20071009012 20 GP TGHU3904061 .0000 .0000 .0000 .0000 .0000 .0000 王存京 奖金 12.0000 .0000 张国动 奖金 16.0000 .0000不知道是否好看,可以发现里面有奖金、提箱费用等,我想把提箱费的列同一列,奖金列同一列,如果还有其他的也是同一列