表一(table1)      
   jsd1        khd1         gddm1        no1      
 上海证券公司    工商银行    0006           6          
 上海证券公司    工商银行    0003           2      
上海证券公司    工商银行    008            8                          
上海证券公司     工商银行   004             3      
  上海证券公司   工商银行    009            9      
 表二(table2)      
   jsd2          khd2      gddm2        no2      
 上海证券公司     工商银行   0002          1          
 上海证券公司     工商银行   0003          2      
 上海证券公司     工商银行   004           3      
       
         现在要将表一和表二中的数据进行核对,统计,      
表一中gddm1,    no1和表二中gddm2    no2数据相同是有证明,有担保的数据(字段名:YZYD)      
表一中gddm1,    no1有而表二中gddm2    no2没有是有证明,无担保的数据,(字段名:YZWD)      
表一中gddm1,    no1无而表二中gddm2    no2有是无证明,有担保的数据,(字段名:WZYD)      
   
     现在要求写一条存储过程,将表一和表二中的数据进行核对,统计      
,将核对,统计后的数据,插到如下    表三(table3)中的临时表中(效果如下:)      
     表三(table3)      
     jsd             khd         YZYD        YZWD        WZYD      
     上海证券公司     工商银行     2            3            1      
   
         要定义YZYD                YZWD                WZYD三个变量先吧?      
         那位朋友看到也请帮忙解决一下啦,急

解决方案 »

  1.   

    insert into table3
    select jsd1, khd1, max(YZYD), max(YZWD), max(WZYD) from (
    select jsd1, khd1, count(*) YZYD, null YZWD, null WZYD from table1 t1
    where exists (select 1 from table2 t2 where t1.jsd1=t2.jsd2 and t1.khd1=t2.khd2)
    group by jsd1, khd1
    union all
    select jsd1, khd1, null YZYD, count(*) YZWD, null WZYD from table1 t1
    where not exists (select 1 from table2 t2 where t1.jsd1=t2.jsd2 and t1.khd1=t2.khd2)
    group by jsd1, khd1
    union all
    select jsd2, khd2, null YZYD, null YZWD, count(*) WZYD from table2 t2
    where not exists (select 1 from table1 t1 where t1.jsd1=t2.jsd2 and t1.khd1=t2.khd2)
    group by jsd2, khd2
    )
    group by jsd1, khd1
      

  2.   

    你好, zzwind5,这是执行你的语句后的结果, 
       JSD        KHD              YZYD       YZWD       WZYD
    ---------- ---------- ---------- ---------- ----------
    上海证券公司    工商银行                                 3
    上海证券公司   工商银行                       5    
     而我希望是这样的结果,  jsd             khd         YZYD        YZWD        WZYD      
     上海证券公司     工商银行     2            3            1       你那样没有一次性的得到统计结果,我看还是定义几个变量,在存储过程里实现好一点,
      

  3.   


     什么语言?,oracle数据库,用的是PLSQL Developer写存储过程呀1
      

  4.   

    那是SQL语言 是SQL server里面的