简体   繁体   中英

Compare two columns on one table row by row

I have two table like this;

Table1;

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

Table2;

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

And table1 has 3 million rows and table2 has 500 thousand rows. I want to compare columnA and column2...

Ok how to compare ? Like this; Table1 row1:

    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

Table1 row2:

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

Table1 row3:

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

and etc.... the end of the table1.. I hope its clear.

I want to do this in sql.. plsql... etc.

I believe you can do this in just one statement:

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

You may be able to get better performance from issuing three different statements however:

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM