bill_head表
no_sys no_work site_id set_id 
------------------------------
1      5113005   201   997
2      5113006   202   998
3      5113009   209   999no_sys 单据系统编号
no_work 单据编号
site_id 机构编号
set_id 设备编号
****************************
bill_detail表
no_sys item_sys
--------------
1      509
1      488
1      511
2      131
2      990
3      332no_sys 单据系统编号
item_sys 项目编号要查询出同一机构(bill_head.site_id相同)对同一台设备(bill_head.set_id相同)制作的不同单据(bill_head.no_sys不同)中,处理项目列表完全相同(bill_detail.item_sys)的单据。本人写的SQL效率很低,麻烦各位高手帮个忙!
一定及时给分!

解决方案 »

  1.   

    以下是我的方法,先创建一个视图create or replace view_1 
    as 
    select no_sys,no_work from bill_head a,bill_head b,
    (select item_sys from bill_head ,bill_detail where bill_head.no_sys = bill_detail.no_sys) cwhere 
    a.set_id = b.set_id and a.no_sys <> b.no_sysselect no_work from view_1,bill_detail c,bill_detail d where view_1.no_sys = c.no_sys and view_1.no_sys = d.no_sys and c.item_sys = d.item_sys ;
      

  2.   

    以这个为准
    create or replace view_1 
    as 
    select no_sys,no_work from bill_head a,bill_head b,
    where 
    a.set_id = b.set_id and a.no_sys <> b.no_sys------------------------------------------
    select no_work from view_1,bill_detail c,bill_detail d where view_1.no_sys = c.no_sys and view_1.no_sys = d.no_sys and c.item_sys = d.item_sys ;
      

  3.   

    先将两表合并在分组求重复的就可以了!
    select a.site_id , a.set_id , b.item_sys
      from bill_head a ,
           bill_detail b 
     where a.no_sys = b.no_sys 
    group by a.site_id , a.set_id , b.item_sys
    having count(*) > 1 ;
    这样出来的结果就是
    同一机构(bill_head.site_id相同)对
    同一台设备(bill_head.set_id相同)制作的
    完全相同(bill_detail.item_sys)的单据。select a.no_sys , a.site_id , a.set_id , b.item_sys
      from bill_head a ,
           bill_detail b 
     where a.no_sys = b.no_sys 
    group by a.no_sys  , a.site_id , a.set_id , b.item_sys
    having count(*) > 1 ;
    这样出来的结果就是
    同一机构(bill_head.site_id相同)对
    同一台设备(bill_head.set_id相同)制作的
    同一单据(bill_head.no_sys相同)的
    完全相同(bill_detail.item_sys)的单据。两个结果集的差就是你想的结果是不??语句如下:select * from (
    select a.no_sys , a.site_id , a.set_id , b.item_sys
      from bill_head a ,
           bill_detail b 
     where a.no_sys = b.no_sys 
    group by a.no_sys  , a.site_id , a.set_id , b.item_sys
    having count(*) > 1 
    minus
    select a.site_id , a.set_id , b.item_sys
      from bill_head a ,
           bill_detail b 
     where a.no_sys = b.no_sys 
    group by a.site_id , a.set_id , b.item_sys);
      

  4.   

    先将两表合并在分组求重复的就可以了!
    select a.site_id , a.set_id , b.item_sys
      from bill_head a ,
           bill_detail b 
     where a.no_sys = b.no_sys 
    group by a.site_id , a.set_id , b.item_sys
    having count(*) > 1 ;
    这样出来的结果就是
    同一机构(bill_head.site_id相同)对
    同一台设备(bill_head.set_id相同)制作的
    完全相同(bill_detail.item_sys)的单据。select a.no_sys , a.site_id , a.set_id , b.item_sys
      from bill_head a ,
           bill_detail b 
     where a.no_sys = b.no_sys 
    group by a.no_sys , a.site_id , a.set_id , b.item_sys
    having count(*) > 1 ;
    这样出来的结果就是
    同一机构(bill_head.site_id相同)对
    同一台设备(bill_head.set_id相同)制作的
    同一单据(bill_head.no_sys相同)的
    完全相同(bill_detail.item_sys)的单据。两个结果集的差就是你想的结果是不??语句如下:select * from (
    select a.no_sys , a.site_id , a.set_id , b.item_sys
      from bill_head a ,
           bill_detail b 
     where a.no_sys = b.no_sys 
    group by a.no_sys , a.site_id , a.set_id , b.item_sys
    having count(*) > 1 ;
    minus
    select a.site_id , a.set_id , b.item_sys
      from bill_head a ,
           bill_detail b 
     where a.no_sys = b.no_sys 
    group by a.site_id , a.set_id , b.item_sys);
      

  5.   

    最后的语句写反了,这里改一下!
    select * from (
    select a.site_id , a.set_id , b.item_sys
      from bill_head a ,
           bill_detail b 
     where a.no_sys = b.no_sys 
    group by a.site_id , a.set_id , b.item_sys
    having count(*) > 1 ;
    minus
    select a.no_sys , a.site_id , a.set_id , b.item_sys
      from bill_head a ,
           bill_detail b 
     where a.no_sys = b.no_sys 
    group by a.no_sys , a.site_id , a.set_id , b.item_sys
    having count(*) > 1);