表名 Table字段 库存id
客户名称
数量
入厂
出厂
数据如下:库存id   客户名称   数量     入厂    出厂
988      北京现代    8       1        0     ‘这条记录代表入厂8个
988      北京现代    8       0        1     ‘这条记录代表出厂8个
988      北京现代    3       0        1     ‘这条记录代表出厂8个
986      北京奔驰    2       0        1     ‘这条记录代表出厂2个我的要求是 如果同样一个库存id  入厂和出厂数量一致  查询就不显示记录.上面的查询结果应该是
库存id   客户名称   数量     入厂      出厂
986      北京奔驰    2       0        1    
988      北京现代    3       0        1   

解决方案 »

  1.   

    select * from tb a
    where not exists
    (select 1 from tb b where a.库存id=b.库存id 
     and a.数量=b.数量 and (a.入厂=b.出厂 or b.入厂=a.出厂))
     
      

  2.   


    declare @table table (库存id int,客户名称 varchar(8),数量 int,入厂 int,出厂 int)
    insert into @table
    select 988,'北京现代',8,1,0 union all
    select 988,'北京现代',8,0,1 union all
    select 988,'北京现代',3,0,1 union all
    select 986,'北京奔驰',2,0,1select a.* from @table a left join @table b on 
    a.库存id=b.库存id and a.客户名称=b.客户名称
    and a.数量=b.数量 and a.入厂=b.出厂 and a.出厂=b.入厂
    where b.库存id is null
    /*
    库存id        客户名称     数量          入厂          出厂
    ----------- -------- ----------- ----------- -----------
    988         北京现代     3           0           1
    986         北京奔驰     2           0           1
    */
    这个表设计的不太规范...
      

  3.   

    --借用叶子的数据,感谢啊
    create table #tb(库存id int,客户名称 varchar(8),数量 int,入厂 int,出厂 int)
    insert into #tb
    select 988,'北京现代',8,1,0 union all
    select 988,'北京现代',8,0,1 union all
    select 988,'北京现代',3,0,1 union all
    select 986,'北京奔驰',2,0,1select * from #tb a
    where not exists
    (select 1 from #tb b where a.库存id=b.库存id 
     and a.数量=b.数量 and (a.入厂=b.出厂 or b.入厂=a.出厂))/*库存id        客户名称     数量          入厂          出厂          
    ----------- -------- ----------- ----------- ----------- 
    988         北京现代     3           0           1
    986         北京奔驰     2           0           1(所影响的行数为 2 行)
    */
      

  4.   

    select
      *
    from
      tb t
    where
      not exists(select 1 from tb where 库存id =t.库存id  and 客户名称=t.客户名称 and 数量=t.数量 and 入厂=t.出厂)
      

  5.   

    select * from #tb a
    where not exists
    (select 1 from #tb b where a.库存id=b.库存id 
     and a.数量=b.数量 and (a.入厂=b.出厂 and b.入厂=a.出厂))
      

  6.   

    http://topic.csdn.net/u/20110711/22/130b134b-983d-4f45-ad02-9af378c567ef.html
      

  7.   


    USE SSISTest;
    GO
    SET NOCOUNT ON
    IF OBJECT_ID(N'A',N'U')IS NOT NULL DROP TABLE A
    GOCREATE TABLE A--创建测试数据表
    (
    Storeid int   not null,
    Name nvarchar(100),
    Number int ,
    InF int,
    OutF int
    )
    INSERT INTO A--插入测试数据
    select 988 ,'北京现代', 8, 1, 0 union all-- ‘这条记录代表入厂8个
    ---------------------------------
    select 988, '北京现代', 3, 0, 1 union all --‘这条记录代表出厂3个
    select 988, '北京现代', 5, 0, 1 union all-- ‘这条记录代表出厂5个
    -----------------------------------
    select 988, '北京现代', 2, 1, 0 union all-- ‘这条记录代表入厂2个select 986, '北京奔驰', 2, 0, 1 -- ‘这条记录代表出厂2个go;with cte1 as
    (
    select 
    Storeid,
    case when OutF=1 then (0-Number)
    else Number end as Number,
    Inf,
    OutF,
    row_number()over(partition by Storeid order by getdate()) as RN
    from A
    )
    ,cte2 as
    (
    select sum(Number) as TheSum,Storeid from cte1 
    group by Storeid
    )
    select 
    cte1.Storeid,
    case when cte1.Number<0 then (0-cte1.Number)
    else cte1.Number end as Number,
    cte1.InF,
    cte1.OutF
    from cte2
    inner join cte1 on cte2.TheSum=cte1.Number and cte1.Storeid=cte2.Storeid
    /*
    Storeid     Number      InF         OutF
    ----------- ----------- ----------- -----------
    986         2           0           1
    988         2           1           0
    */