共有4个表:【表1】、【表2】、【表3】 和【表4】
==================================== 【表1】字段及数据: 
------------------------------------ 
编号    货号      数值 
001      A1        200 
002      B1        150 
003      C2        199 
001      D1        200 
------------------------------------ 【表2】字段及数据: 
------------------------------------ 
编号      姓名      性别      年龄 
001      张三      男          20 
002      李丽      女          22 
003      王冲      男          18 
004      anni      女          35 
------------------------------------ 【表3】字段及数据: 
------------------------------------ 
货号      数值  
A1        10 
C2        50 
------------------------------------ 【表4】字段及数据
------------------------------------
编号      数值
001       5
002       10
------------------------------------【问题】分类合计【表2】中各[编号]字段在【表1】中的[数值]合计,并比对【表3】中的货号,如果在【表3】中存在与【表1】中相同的货号则替换成【表3】中该货号所对应的数值;最后结果要减去【表4】中所对应[编号]的数值。结果如下:【结果】 
---------------------------------------------- 
编号      姓名      性别      年龄      数值  
001       张三       男        20       205 
002       李丽       女        22       140 
003       王冲       男        18       50 
004       anni       女        35       0 
---------------------------------------------- 求SQL语句,请高手帮忙!谢谢! 

解决方案 »

  1.   

    --> 测试数据:[t1]
    if object_id('[t1]') is not null drop table [t1]
    create table [t1]([编号] varchar(3),[货号] varchar(2),[数值] int)
    insert [t1]
    select '001','A1',200 union all
    select '002','B1',150 union all
    select '003','C2',199 union all
    select '001','D1',200
    if object_id('t2') is not null drop table t2
    create table t2([编号] varchar(3),[姓名] varchar(4),[性别] varchar(2),[年龄] int)
    insert t2
    select '001','张三','男',20 union all
    select '002','李丽','女',22 union all
    select '003','王冲','男',18 union all
    select '004','anni','女',35if object_id('[t3]') is not null drop table [t3]
    create table [t3]([货号] varchar(2),[数值] int)
    insert [t3]
    select 'A1',10 union all
    select 'C2',50
    -------------------查询开始-----------
    select c.* ,isnull(t.数值,0) as 数值 from t2 c
    left join(
    select a.编号,
    sum(case when a.货号=b.货号 then b.数值 else a.数值 end  ) as 数值 from t1 a
    left join t3 b on a.货号 = b.货号 
    group by a.编号) t
    on c.编号=t.编号/*
    编号   姓名   性别   年龄          合计
    ---- ---- ---- ----------- -----------
    001  张三   男    20          210
    002  李丽   女    22          150
    003  王冲   男    18          50
    004  anni 女    35          0(4 行受影响)
    */
      

  2.   

    -------------------查询开始-----------
    select c.* ,isnull(t.数值,0) as 数值 from t2 c
    left join
    (
    select a.编号,
    sum(case 
    when a.货号=b.货号 then b.数值 
    when a.编号=d.编号 then a.数值+d.数值 
    else a.数值
    end  ) as 数值 from t1 a
    left join t3 b on a.货号 = b.货号 
    left join t4 d on a.编号 = d.编号 
    group by a.编号
    ) t
    on c.编号=t.编号
    /*
    编号   数值
    ---- -----------
    001  215
    002  160
    003  50(3 行受影响)
    */
      

  3.   

    + 号变成-号
    --> 测试数据:[t1]
    if object_id('[t1]') is not null drop table [t1]
    go
    create table [t1]([编号] varchar(3),[货号] varchar(2),[数值] int)
    insert [t1]
    select '001','A1',200 union all
    select '002','B1',150 union all
    select '003','C2',199 union all
    select '001','D1',200if object_id('t2') is not null drop table t2
    go
    create table t2([编号] varchar(3),[姓名] varchar(4),[性别] varchar(2),[年龄] int)
    insert t2
    select '001','张三','男',20 union all
    select '002','李丽','女',22 union all
    select '003','王冲','男',18 union all
    select '004','anni','女',35if object_id('[t3]') is not null drop table [t3]
    go
    create table [t3]([货号] varchar(2),[数值] int)
    insert [t3]
    select 'A1',10 union all
    select 'C2',50--> 测试数据:[t4]
    if object_id('[t4]') is not null drop table [t4]
    go
    create table [t4]([编号] varchar(3),[数值] int)
    insert [t4]
    select '001',5 union all
    select '002',10select * from [t4]
    -------------------查询开始-----------
    select c.* ,isnull(t.数值,0) as 数值 from t2 c
    left join
    (
    select a.编号,
    sum(case 
    when a.货号=b.货号 then b.数值 
    when a.编号=d.编号 then a.数值-d.数值 
    else a.数值
    end  ) as 数值 from t1 a
    left join t3 b on a.货号 = b.货号 
    left join t4 d on a.编号 = d.编号 
    group by a.编号
    ) t
    on c.编号=t.编号
    /*
    编号   姓名   性别   年龄          数值
    ---- ---- ---- ----------- -----------
    001  张三   男    20          205
    002  李丽   女    22          140
    003  王冲   男    18          50
    004  anni 女    35          0(4 行受影响)*/
      

  4.   

    -->Title:生成測試數據
    -->Author:wufeng4552【水族杰纶】
    -->Date :2009-09-01 13:50:06
     
    if not object_id('Td') is null
    drop table Td
    Go
    Create table Td([编号] nvarchar(3),[数值] int)
    Insert Td
    select N'001',5 union all
    select N'002',10
    Goif not object_id('Tb') is null
    drop table Tb
    Go
    Create table Tb([编号] nvarchar(3),[姓名] nvarchar(4),[性别] nvarchar(1),[年龄] int)
    Insert Tb
    select N'001',N'张三',N'男',20 union all
    select N'002',N'李丽',N'女',22 union all
    select N'003',N'王冲',N'男',18 union all
    select N'004',N'anni',N'女',35
    Go
    if object_id('[TC]') is not null drop table [TC]
    create table [TC]([货号] varchar(2),[数值] int)
    insert [TC]
    select 'A1',10 union all
    select 'C2',50
    if object_id('[TA]') is not null drop table [TA]
    create table [TA]([编号] varchar(3),[货号] varchar(2),[数值] int)
    insert [TA]
    select '001','A1',200 union all
    select '002','B1',150 union all
    select '003','C2',199 union all
    select '001','D1',200
    select B.*,
          [数值]=isnull(t.[数值] ,0)-isnull(d.[数值],0)
    from TB B 
    left join(
    select 编号,[数值]=sum(isnull(C.[数值],A.[数值])) 
    from TA A 
    left join TC C on A.货号=C.货号 group by 编号)t 
    on B.编号=t.编号
    left join td d on d.[编号]=b.[编号]
    /*
    编号   姓名   性别   年龄          数值
    ---- ---- ---- ----------- -----------
    001  张三   男    20          205
    002  李丽   女    22          140
    003  王冲   男    18          50
    004  anni 女    35          0*/
      

  5.   

    恩!~~今天我真是太冤枉了!以前从没写过,这样的类型的SQL语句!
      

  6.   


    tony 哥 教我两招吧
      

  7.   


    declare @t1 table( 编号 varchar(10) , 货号 varchar(10) , 数值 decimal(10,2))
    insert into @t1 values('001','A1',200) 
    insert into @t1 values('002','B1',150) 
    insert into @t1 values('003','C2',199) 
    insert into @t1 values('001','D1',200) declare @t2 table( 编号 varchar(10) , 姓名 varchar(10) , 性别 varchar(10), 年龄 int)
    insert into @t2 values('001','张三','男',20 ) 
    insert into @t2 values('002','李丽','女',22 )
    insert into @t2 values('003','王冲','男',18 )
    insert into @t2 values('004','anni','女',35 )
    declare @t3 table( 货号 varchar(10) , 数值 decimal(10,2))insert into @t3 values('A1',10) 
    insert into @t3 values('C2',50) 
    insert into @t3 values('C5',50) declare @t4 table( 编号 varchar(10), 数值 decimal(10,2))
    insert into @t4 values('001',5) 
    insert into @t4 values('002',10) 
    select a.*, isnull(d.数值,0) - isnull(e.数值,0)  as 数值  from @t2 a
    left outer join(select b.编号,
    sum( case when c.数值 is null then b.数值 else c.数值 end) as 数值
     from @t1 b 
    left outer join @t3 c on  c.货号= b.货号
    group by 编号 )d on d.编号 = a.编号
    left outer join @t4 e on e.编号 = a.编号