[英]Compare two columns on one table row by row
我有两个这样的桌子。
表格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 ...等
我相信您可以通过以下一种方法做到这一点:
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.