逐行比较一个表上的两列

时间:2014-07-28 13:39:50

标签: sql

我有两张这样的桌子;

表1;

    ColumnA    ColumnB    ColumnC   .............
    -------    -------     
     1.003       .. 
     0.423       ..
     3           ..
     2.09        ..
     122         ..
     1.1         ..
      ..         .. 
      ..         ..
      ..         ..

表2;

 Column1     Column2
 -------     -------
   ..        2.399
   ..        1.012412
   ..        4.3323
   ..        6
   ..        0
   ..        30000
   ..         .. 
   ..         .. 
   ..         .. 

table1有300万行,table2有50万行。我想比较columnA和column2 ......

好的如何比较?像这样; 表1第1行:

    1.003>2.399  then set val1=val1+1 OR 1.003=2.399 then set val2=val2+1 OR 
1.003<2.399 then set val3=val3+1 

    1.003>1.012412 then set val1=val1+1 OR  1.003=1.012412 then set val2=val2+1 OR
1.003<1.012412 then set val3=val3+1 

    1.003>4.3323  then set val1=val1+1 OR 1.003=4.3323 then set val2=val2+1 OR
1.003<4.3323 then set val3=val3+1

    1.003>6  then set val1=val1+1 OR 1.003=6 then set val2=val2+1 OR 
1.003<6 then set val3=val3+1

    1.003>0 then set val1=val1+1 OR 1.003=0 then set val2=val2+1 OR 
1.003<0 then set val3=val3+1

    1.003>30000 then set val1=val1+1 OR 1.003=30000 then set val2=val2+1 OR 
1.003<30000 then set val3=val3+1
                     ........................
                     ........................the end of the table2

表1第2行:

0.423>2.399  then set val1=val1+1 OR 0.423=2.399 then set val2=val2+1 OR 
0.423<2.399 then set val3=val3+1 

    0.423>1.012412 then set val1=val1+1 OR  0.423=1.012412 then set val2=val2+1 OR
0.423<1.012412 then set val3=val3+1 

    0.423>4.3323  then set val1=val1+1 OR 0.423=4.3323 then set val2=val2+1 OR
0.423<4.3323 then set val3=val3+1

    0.423>6  then set val1=val1+1 OR 0.423=6 then set val2=val2+1 OR 
0.423<6 then set val3=val3+1

    0.423>0 then set val1=val1+1 OR 0.423=0 then set val2=val2+1 OR 
0.423<0 then set val3=val3+1

    0.423>30000 then set val1=val1+1 OR 0.423=30000 then set val2=val2+1 OR 
0.423<30000 then set val3=val3+1
                     ........................
                     ........................the end of the table2

表1第3行:

3>2.399  then set val1=val1+1 OR 3=2.399 then set val2=val2+1 OR 
3<2.399 then set val3=val3+1 

    3>1.012412 then set val1=val1+1 OR  3=1.012412 then set val2=val2+1 OR
3<1.012412 then set val3=val3+1 

    3>4.3323  then set val1=val1+1 OR 3=4.3323 then set val2=val2+1 OR
3<4.3323 then set val3=val3+1

    3>6  then set val1=val1+1 OR 3=6 then set val2=val2+1 OR 
3<6 then set val3=val3+1

    3>0 then set val1=val1+1 OR 3=0 then set val2=val2+1 OR 
3<0 then set val3=val3+1

    3>30000 then set val1=val1+1 OR 3=30000 then set val2=val2+1 OR 
3<30000 then set val3=val3+1
                     ........................
                     ........................the end of the table2

等......表1的结尾..我希望它清楚。

我想在sql中执行此操作.. plsql ...等。

1 个答案:

答案 0 :(得分:0)

我相信你可以用一个陈述来做到这一点:

SELECT COUNT(CASE WHEN t1.columnA>t2.column2 THEN 1 ELSE NULL END) val1,
       COUNT(CASE WHEN t1.columnA=t2.column2 THEN 1 ELSE NULL END) val2,
       COUNT(CASE WHEN t1.columnA<t2.column2 THEN 1 ELSE NULL END) val3
  FROM table1 t1
  JOIN table2 t2

然而,您可以通过发出三种不同的陈述来获得更好的表现:

SELECT COUNT(*) FROM table1 t1 JOIN table2 t2 ON t1.columnA < t2.column2;
SELECT COUNT(*) FROM table1 t1 JOIN table2 t2 ON t1.columnA = t2.column2;
SELECT COUNT(*) FROM table1 t1 JOIN table2 t2 ON t1.columnA > t2.column2;