select  a.baogaodanhao,tuoyunren,tiji,b.shoukuanxiangmu,shoukuanjine,b.huilv,c.fukuanxiangmu,fukuanjine,c.huilv 
from tuoyun a , yingshou b ,yingfu c
where a.yewubaogaodanhao = b.baogaodanhao and b.baogaodanhao = c.baogaodanhao原数据效果报告单号 托运人  体积 收款项目 收款金额 汇率 付款项目 付款金额 汇率09RL02  宜昌外运 240  包干费   24000.00   1  陆运运费  5000.00   109RL02  宜昌外运 240  包干费   24000.00   1  报关费    2000.00   109RL02  宜昌外运 240  包干费   24000.00   1  口岸费    1000.00   109RL03  武汉外运 160  仓租费   10000.00   1  力资费    5000.00   109RL03  武汉外运 160  制单费   5000.00    1  力资费    5000.00   109RL03  武汉外运 160  报关费   600.00   6.88 力资费    5000.00   1
要达到的效果报告单号 托运人  体积 收款项目 收款金额 汇率 付款项目 付款金额 汇率09RL02  宜昌外运 240  包干费   24000.00   1  陆运运费  5000.00   109RL02  宜昌外运                          1  报关费    2000.00   109RL02  宜昌外运                          1  口岸费    1000.00   109RL03  武汉外运 160  仓租费   10000.00   1  力资费    5000.00   109RL03  武汉外运      制单费   5000.00    1  09RL03  武汉外运      报关费   600.00   6.88 

解决方案 »

  1.   

    顶,用数据库标志函数可以解决,遇到相同的就用null代替
      

  2.   

    declare @tb table (报告单号 nvarchar(10),托运人 nvarchar(10),体积 int,收款项目 nvarchar(10),
                       收款金额 money,汇率 int,付款项目 nvarchar(10),付款金额 money ,汇率1 int)
    insert into @tb select '09rl02','宜昌外运',240,'包干费',24000.00,1,'陆运运费',5000.00,1
         union all  select '09rl02','宜昌外运',240,'包干费',24000.00,1,'报关费',2000.00,1
         union all  select '09rl02','宜昌外运',240,'包干费',24000.00,1,'口岸费',1000.00,1
         union all  select '09rl03','武汉外运',240,'仓租费',10000.00,1,'力资费',5000.00,1
         union all  select '09rl03','武汉外运',240,'制单费',24000.00,1,'力资费',5000.00,1
         union all  select '09rl03','武汉外运',240,'报关费',24000.00,6.88,'力资费',5000.00,1
    select 报告单号,托运人,体积= case when exists(select * from @tb where a.收款项目=收款项目 and 付款金额<a.付款金额 ) 
       then null else 体积  end ,收款项目= case when exists(select * from @tb where a.收款项目=收款项目 and 付款金额<a.付款金额 ) 
         then null  else 收款项目 end ,收款金额= case when exists(select * from @tb where a.收款项目=收款项目 and 付款金额<a.付款金额 ) 
             then null else 收款金额  end , 汇率,付款项目= case when exists(select * from @tb where a.付款项目=付款项目 and 收款项目>a.收款项目 ) 
                  then null else 付款项目  end , 付款金额 = case when exists(select * from @tb where a.付款项目=付款项目 and 收款项目>a.收款项目 ) 
                      then null else 付款金额  end , 汇率1= case when exists(select * from @tb where a.付款项目=付款项目 and 收款项目>a.收款项目) 
                         then null else 汇率1 end    from @tb a order by 报告单号,  收款金额 desc
      

  3.   

    declare @tb table (报告单号 nvarchar(10),托运人 nvarchar(10),体积 int,收款项目 nvarchar(10),
                       收款金额 money,汇率 int,付款项目 nvarchar(10),付款金额 money ,汇率1 int)
    insert into @tb select '09rl02','宜昌外运',240,'包干费',24000.00,1,'陆运运费',5000.00,1
         union all  select '09rl02','宜昌外运',240,'包干费',24000.00,1,'报关费',2000.00,1
         union all  select '09rl02','宜昌外运',240,'包干费',24000.00,1,'口岸费',1000.00,1
         union all  select '09rl03','武汉外运',240,'仓租费',10000.00,1,'力资费',5000.00,1
         union all  select '09rl03','武汉外运',240,'制单费',24000.00,1,'力资费',5000.00,1
         union all  select '09rl03','武汉外运',240,'报关费',24000.00,6.88,'力资费',5000.00,1
    select 报告单号,托运人,体积= case when exists(select * from @tb where a.收款项目=收款项目 and 付款金额<a.付款金额 ) 
       then null else 体积  end ,收款项目= case when exists(select * from @tb where a.收款项目=收款项目 and 付款金额<a.付款金额 ) 
         then null  else 收款项目 end ,收款金额= case when exists(select * from @tb where a.收款项目=收款项目 and 付款金额<a.付款金额 ) 
             then null else 收款金额  end , 汇率,付款项目= case when exists(select * from @tb where a.付款项目=付款项目 and 收款项目>a.收款项目 ) 
                  then null else 付款项目  end , 付款金额 = case when exists(select * from @tb where a.付款项目=付款项目 and 收款项目>a.收款项目 ) 
                      then null else 付款金额  end , 汇率1= case when exists(select * from @tb where a.付款项目=付款项目 and 收款项目>a.收款项目) 
                         then null else 汇率1 end    from @tb a order by 报告单号,  收款金额 desc
      

  4.   

    declare @tb table (报告单号 nvarchar(10),托运人 nvarchar(10),体积 int,收款项目 nvarchar(10),
                       收款金额 money,汇率 int,付款项目 nvarchar(10),付款金额 money ,汇率1 int)
    insert into @tb select '09rl02','宜昌外运',240,'包干费',24000.00,1,'陆运运费',5000.00,1
         union all  select '09rl02','宜昌外运',240,'包干费',24000.00,1,'报关费',2000.00,1
         union all  select '09rl02','宜昌外运',240,'包干费',24000.00,1,'口岸费',1000.00,1
         union all  select '09rl03','武汉外运',240,'仓租费',10000.00,1,'力资费',5000.00,1
         union all  select '09rl03','武汉外运',240,'制单费',24000.00,1,'力资费',5000.00,1
         union all  select '09rl03','武汉外运',240,'报关费',24000.00,6.88,'力资费',5000.00,1
    select 报告单号,托运人,体积= case when exists(select * from @tb where a.收款项目=收款项目 and 付款金额<a.付款金额 ) 
       then null else 体积  end ,收款项目= case when exists(select * from @tb where a.收款项目=收款项目 and 付款金额<a.付款金额 ) 
         then null  else 收款项目 end ,收款金额= case when exists(select * from @tb where a.收款项目=收款项目 and 付款金额<a.付款金额 ) 
             then null else 收款金额  end , 汇率,付款项目= case when exists(select * from @tb where a.付款项目=付款项目 and 收款项目>a.收款项目 ) 
                  then null else 付款项目  end , 付款金额 = case when exists(select * from @tb where a.付款项目=付款项目 and 收款项目>a.收款项目 ) 
                      then null else 付款金额  end , 汇率1= case when exists(select * from @tb where a.付款项目=付款项目 and 收款项目>a.收款项目) 
                         then null else 汇率1 end    from @tb a order by 报告单号,  收款金额 desc
      

  5.   

    方法較笨,能實現效果而已。等高手出現吧
    declare @tb table ( id int  identity primary key,
    aaa nvarchar(10),bbb nvarchar(10),cc int,dd nvarchar(10),
                       ee money,gg int,ff nvarchar(10),mm money ,oo1 int)
    insert into @tb select '09rl02','宜昌',240,'包干',24000.00,1,'陸運',5000.00,1
         union all  select '09rl02','宜昌',240,'包干',24000.00,1,'報關',2000.00,1
         union all  select '09rl02','宜昌',240,'包干',24000.00,1,'口岸',1000.00,1
         union all  select '09rl03','武漢',240,'倉租',10000.00,1,'力資',5000.00,1
         union all  select '09rl03','武漢',240,'制單',24000.00,1,'力資',5000.00,1
         union all  select '09rl03','武漢',240,'報送',24000.00,6.88,'力資',5000.00,1select  aaa,bbb,cc= case when  cc=isnull((select cc from @tb  where id=a.id-1   and  aaa=a.aaa ),0) then 0 else cc  end
      ,dd= case when  dd=isnull((select dd from @tb  where id=a.id-1   and  aaa=a.aaa ),'') then '' else dd  end  
    , ee=case when  ee=isnull((select ee from @tb  where id=a.id-1   and  aaa=a.aaa ),0) then 0 else ee  end  from @tb a