现有库存表 a 编号 数量  有500条记录
  盘存表b  编号 数量    有480 条数据(也有可能盘点数量超过库存表的数量,比如说盘点有501个记录)
如何通过表a 和表b 查询出这次盘点的货多货少?

解决方案 »

  1.   

    select id,case when a.库存量>b.盘存量 then '货多'
                     when a.库存量=b.盘存量 then '无差错'
                     when a.库存量>b.盘存量 then '货少' end
    from (select id ,库存量=count(*) from a)s join
         (select id ,盘存量=count(*) from b)t on s.id=t.id
      

  2.   

    货多货少的关系不明白,如果反了就自己改过来.select isnull(m.编号 , n.编号) 编号 ,
           case when isnull(m.数量,0) - isnull(n.数量,0) = 0 then '无差错'
                when isnull(m.数量,0) - isnull(n.数量,0) > 0 then '货少'
                when isnull(m.数量,0) - isnull(n.数量,0) < 0 then '货多'
           end '盘点情况'
    from
    (select 编号 , sum(数量) 数量 from a group by 编号) m
    full join
    (select 编号 , sum(数量) 数量 from b group by 编号) n
    on m.编号 = n.编号
    order by 编号
      

  3.   


    select isnull(m.编号 , n.编号) 编号 ,
           case when isnull(m.数量,0) - isnull(n.数量,0) = 0 then '无差错'
                when isnull(m.数量,0) - isnull(n.数量,0) > 0 then '货少'
                when isnull(m.数量,0) - isnull(n.数量,0) < 0 then '货多'
           end '盘点情况',
           isnull(m.数量,0) - isnull(n.数量,0) 数量
    from
    (select 编号 , sum(数量) 数量 from a group by 编号) m
    full join
    (select 编号 , sum(数量) 数量 from b group by 编号) n
    on m.编号 = n.编号
    order by 编号
      

  4.   

    ------------如果两表中的编号(bh)字段记录不唯一,则先汇总-------------
    create table a(bh varchar(10), total int)
    insert into a
    select 
    '001',1001 union all select
    '001',801 union all select
    '002', 879 union all select
    '003', 30;create table b(bh varchar(10), total int);
    insert into b
    select 
    '001',991 union all select
    '001',991 union all select
    '002', 881 union all select
    '003', 26;select bh '编号', (case when sum(total)>0 then '盘赢 '+convert(varchar(10),sum(total))+' 件'
                     when sum(total)=0 then '准确' 
     else '盘亏 '+convert(varchar(10),-sum(total))+' 件' end)
                as '盘点结果'
    from (
    select a.bh, a.total from a
    union all
    select b.bh, -b.total from b) t
    group by t.bh
    -----------------------------------
    编号  盘点结果
    001   盘亏 180 件
    002   盘亏 2 件
    003   盘赢 4 件
      

  5.   

    --呵呵:整错了
    ------------如果两表中的编号(bh)字段记录不唯一,则先汇总-------------
    create table a(bh varchar(10), total int)
    insert into a
    select 
    '001',1001 union all select
    '001',801 union all select
    '002', 879 union all select
    '003', 30;create table b(bh varchar(10), total int);
    insert into b
    select 
    '001',991 union all select
    '001',991 union all select
    '002', 881 union all select
    '003', 26;select bh '编号', (case when sum(total)>0 then '盘亏 '+convert(varchar(10),sum(total))+' 件'
                     when sum(total)=0 then '准确' 
     else '盘赢 '+convert(varchar(10),-sum(total))+' 件' end)
                as '盘点结果'
    from (
    select a.bh, a.total from a
    union all
    select b.bh, -b.total from b) t
    group by t.bh
    -----------------------------------
    编号  盘点结果
    001 盘赢 180 件
    002 盘赢 2 件
    003 盘亏 4 件
      

  6.   

    ------------如果两表中的编号(bh)字段记录不唯一,则先汇总-------------
    create table a(bh varchar(10), total int)
    insert into a
    select 
    '001',1001 union all select
    '001',801 union all select
    '002', 879 union all select
    '003', 30 union all select
    '004', 27;create table b(bh varchar(10), total int);
    insert into b
    select 
    '001',991 union all select
    '001',991 union all select
    '002', 881 union all select
    '003', 26 union all select
    '004', 27;select bh '编号', (case when sum(total)>0 then '盘亏 '+convert(varchar(10),sum(total))+' 件'
                     when sum(total)=0 then '准确' 
     else '盘赢 '+convert(varchar(10),-sum(total))+' 件' end)
                as '盘点结果'
    from (
    select a.bh, a.total from a
    union all
    select b.bh, -b.total from b) t
    group by t.bh
    -----------------------------------
    编号  盘点结果
    001   盘赢 180 件
    002   盘赢 2 件
    003   盘亏 4 件
    004   准确