table
单号   物品编号    重量
001      201        3
001      202        4
001      203        8
002      201        1
002      202        9
002      203        8
003      201        2
003      202        3
003      203        7求SQL语句:输入单号后,查询出与该单号物品编号相同且对应的物品重量在该重量正负值N(可手工任意输入)范围的单号。如输入单号001,可以查询出003单号,因为003单与与001单号的物品相同,且003单号中每种物品的重量与001单号中对应的物品重量在正负1范围内。

解决方案 »

  1.   

    select a.* from tb a
    join 
    (select 物品编号 from tb where 单号='001') b
    on a.单号=b.单号
    where abs(a.重量-b.重量)<=1 and a.单号!=b.单号
      

  2.   

    修正关联字段
    select a.* from tb a
    join 
    (select 物品编号 from tb where 单号='001') b
    on a.物品编号=b.物品编号
    where abs(a.重量-b.重量)<=1 and a.单号!=b.单号
      

  3.   

    ----------------------------------------------------------------
    -- Author  :fredrickhu(小F,向高手学习)
    -- Date    :2012-07-03 14:53:43
    -- Version:
    --      Microsoft SQL Server 2008 R2 (RTM) - 10.50.1617.0 (Intel X86) 
    -- Apr 22 2011 11:57:00 
    -- Copyright (c) Microsoft Corporation
    -- Enterprise Edition on Windows NT 6.1 <X64> (Build 7600: ) (WOW64)
    --
    ----------------------------------------------------------------
    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    go 
    create table [tb]([单号] varchar(3),[物品编号] int,[重量] int)
    insert [tb]
    select '001',201,3 union all
    select '001',202,4 union all
    select '001',203,8 union all
    select '002',201,1 union all
    select '002',202,9 union all
    select '002',203,8 union all
    select '003',201,2 union all
    select '003',202,3 union all
    select '003',203,7
    --------------开始查询--------------------------
    select * from tb t where exists(select 1 from tb where  物品编号=t.物品编号 and abs(t.重量-重量)=1 and 单号='001' ) 
    ----------------结果----------------------------
    /* 单号   物品编号        重量
    ---- ----------- -----------
    003  201         2
    003  202         3
    003  203         7(3 行受影响)
    */
      

  4.   

    if object_id('[tb]') is not null drop table [tb]
    go 
    create table [tb]([单号] varchar(3),[物品编号] int,[重量] int)
    insert [tb]
    select '001',201,3 union all
    select '001',202,4 union all
    select '001',203,8 union all
    select '002',201,1 union all
    select '002',202,9 union all
    select '002',203,8 union all
    select '003',201,2 union all
    select '003',202,3 union all
    select '003',203,7select a.* from tb a
    join 
    (select * from tb where 单号='001') b
    on a.物品编号=b.物品编号
    where abs(a.重量-b.重量)<=1 and a.单号!=b.单号
    /**
    单号   物品编号        重量
    ---- ----------- -----------
    002  203         8
    003  201         2
    003  202         3
    003  203         7(4 行受影响)
    **/--只要单号
    select distinct a.单号 from tb a
    join 
    (select * from tb where 单号='001') b
    on a.物品编号=b.物品编号
    where abs(a.重量-b.重量)<=1 and a.单号!=b.单号
    /**
    单号
    ----
    002
    003(2 行受影响)
    **/
      

  5.   


    --> 测试数据:[test]
    if object_id('[test]') is not null 
    drop table [test]
    create table [test](
    [单号] varchar(3),
    [物品编号] int,
    [重量] int
    )
    go
    insert [test]
    select '001',201,3 union all
    select '001',202,4 union all
    select '001',203,8 union all
    select '002',201,1 union all
    select '002',202,9 union all
    select '002',203,8 union all
    select '003',201,2 union all
    select '003',202,3 union all
    select '003',203,7
    go
    declare @DH varchar(5)
    set @DH='001'
    select * from test a
    where exists(select 1 from test b 
    where 单号=@DH and 
    a.物品编号=b.物品编号 and ABS(a.重量-b.重量)=1)
    /*
    单号 物品编号 重量
    003 201 2
    003 202 3
    003 203 7
    */