阿表结构如下:jh varchar2(16)   cshrq  date xch  varchar2(10)
c100-52           2008-05-11  F121
c100-52           2008-05-11  F32
c100-52           2008-05-11  F172
c98-98            2008-02-23  F21
c100-52           2007-08-25  F32
c100-52           2006-04-21  F32
c100-52           2006-04-21  F172
c98-98            2006-06-09  F21
c98-98            2006-06-09  F152查询条件是对比2008年度及2006年度,找到所有jh和xch对应相等的所有记录,如上例可查到的正确结果为:
c100-52           2008-05-11  F32  
c100-52           2008-05-11  F172
c100-52           2006-04-21  F32 
c100-52           2006-04-21  F172 
c98-98            2008-02-23  F21
c98-98            2006-06-09  F21

解决方案 »

  1.   

    select m.* from tb m where exists (select 1 from
    (select jh , xch , to_char(cshrq,'YYYY') yy from tb where to_char(cshrq,'YYYY') in ('2006','2008') group by jh , xch , to_char(cshrq,'YYYY') having count(*) > 1) n 
    where n.jh = m.jh and n.xch = m.xch and to_char(n.cshrq,'YYYY') = to_char(m.cshrq,'YYYY')
    )
      

  2.   


    --试下
    select * from table_name t where exists(
    select jh,xch,count(1) from table_name t1 where t.jh=t1.jh and t.xch=t1.xch and to_char(t1.cshrq,'yyyy') in('2006','2008') group by jh,xch having count(1)>1) and to_char(t.cshrq,'yyyy') in('2006','2008');
      

  3.   

    create table tb(jh varchar2(16) , cshrq  date ,xch  varchar2(10))
    insert into tb values('c100-52'  ,        to_date('2008-05-11','YYYY-MM-DD HH24:MI:SS') , 'F121') 
    insert into tb values('c100-52'  ,        to_date('2008-05-11','YYYY-MM-DD HH24:MI:SS') , 'F32') 
    insert into tb values('c100-52'  ,        to_date('2008-05-11','YYYY-MM-DD HH24:MI:SS') , 'F172') 
    insert into tb values('c98-98'   ,        to_date('2008-02-23','YYYY-MM-DD HH24:MI:SS') , 'F21') 
    insert into tb values('c100-52'  ,        to_date('2007-08-25','YYYY-MM-DD HH24:MI:SS') , 'F32') 
    insert into tb values('c100-52'  ,        to_date('2006-04-21','YYYY-MM-DD HH24:MI:SS') , 'F32') 
    insert into tb values('c100-52'  ,        to_date('2006-04-21','YYYY-MM-DD HH24:MI:SS') , 'F172') 
    insert into tb values('c98-98'   ,        to_date('2006-06-09','YYYY-MM-DD HH24:MI:SS') , 'F21') 
    insert into tb values('c98-98'   ,        to_date('2006-06-09','YYYY-MM-DD HH24:MI:SS') , 'F152')select m.*  from tb m where exists (select 1 from
    (select distinct jh , xch from tb where to_char(cshrq,'YYYY') in ('2006','2008') group by jh , xch  having count(*) = 2) n 
    where n.jh = m.jh and n.xch = m.xch)drop table tb 
      

  4.   

    to_dawugui:语句执行通过,但不是我要的结果!
      

  5.   

    select jh, cshrq, xch
      from (select jh,
                   cshrq,
                   xch,
                   row_number() over(partition by jh, xch order by cshrq) rn
              from a)
     where rn >= 2
      

  6.   

    --你的意思是2007年的那条数据不要?
    create table tb(jh varchar2(16) , cshrq  date ,xch  varchar2(10))
    insert into tb values('c100-52'  ,        to_date('2008-05-11','YYYY-MM-DD HH24:MI:SS') , 'F121') 
    insert into tb values('c100-52'  ,        to_date('2008-05-11','YYYY-MM-DD HH24:MI:SS') , 'F32') 
    insert into tb values('c100-52'  ,        to_date('2008-05-11','YYYY-MM-DD HH24:MI:SS') , 'F172') 
    insert into tb values('c98-98'   ,        to_date('2008-02-23','YYYY-MM-DD HH24:MI:SS') , 'F21') 
    insert into tb values('c100-52'  ,        to_date('2007-08-25','YYYY-MM-DD HH24:MI:SS') , 'F32') 
    insert into tb values('c100-52'  ,        to_date('2006-04-21','YYYY-MM-DD HH24:MI:SS') , 'F32') 
    insert into tb values('c100-52'  ,        to_date('2006-04-21','YYYY-MM-DD HH24:MI:SS') , 'F172') 
    insert into tb values('c98-98'   ,        to_date('2006-06-09','YYYY-MM-DD HH24:MI:SS') , 'F21') 
    insert into tb values('c98-98'   ,        to_date('2006-06-09','YYYY-MM-DD HH24:MI:SS') , 'F152')select m.*  from tb m where exists (select 1 from
    (select distinct jh , xch from tb where to_char(cshrq,'YYYY') in ('2006','2008') group by jh , xch  having count(*) = 2) n 
    where n.jh = m.jh and n.xch = m.xch) and to_char(cshrq,'YYYY') in ('2006','2008') order by m.jh , m.xch
    drop table tb 
      

  7.   

    应该是这样select jh, cshrq, xch
      from a
     where (jh, xch) in
           (select jh, xch
              from (select jh, xch, count(*) cu from a group by jh, xch)
             where cu > 1)
      

  8.   

    8楼语句执行结果:
    5 第四油矿 C50 四矿三工区 10C60-126 F172 2008-4-21 9
    6 第四油矿 C50 四矿三工区 10C60-126 F172 2008-6-8 10
    23 第四油矿 C55 四矿一工区 10C74-132 F21 2006-9-22
    24 第四油矿 C55 四矿一工区 10C74-132 F21 2006-9-22
    出现这两对不符条件的结果,其他都正确!
      

  9.   

    select distinct n.*  from (select * from tb where to_char(cshrq, 'yyyy')= '2008') m,
                    (select * from tb where to_char(cshrq, 'yyyy')= '2006') n
    where m.jh = n.jh and m.xch =n.xch 
    union all
    select distinct m.*  from (select * from tb where to_char(cshrq, 'yyyy')= '2008') m,
                    (select * from tb where to_char(cshrq, 'yyyy')= '2006') n
    where m.jh = n.jh and m.xch =n.xch试一下这个,我测过好像没问题
      

  10.   

    select * from
    (
      select jh,cshrq,xch from yourtable a
       where to_char(cshrq,'YYYY')='2008'
         and exists(select * from yourtable where jh=a.jh and xch=a.xch and to_char(cshrq,'YYYY')='2006')
      union all
      select jh,cshrq,xch from yourtable a
       where to_char(cshrq,'YYYY')='2006'
         and exists(select * from yourtable where jh=a.jh and xch=a.xch and to_char(cshrq,'YYYY')='2008')
    )
    order by jh,cshrq desc,xch
      

  11.   

    with temp_data as(
        select jh, cshrq, xch, rownum num
          from table_name tn
         where to_char(tn.cshrq, 'yyyy') in ('2008', '2006')
     )
     
     select td.jh, td.cshrq, td.xch
       from temp_data td, temp_data td2
      where td.jh = td2.jh
        and td.xch = td2.xch
        and td.num <> td2.num