SQL Server - 针对两列中的重复值进行反向验证 [内部示例]

时间:2023-02-06
本文介绍了SQL Server - 针对两列中的重复值进行反向验证 [内部示例]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试实施验证以检查是否以相反的顺序输入了相同的外汇汇率值.用户将填充由三列组成的电子表格:

I am trying to implement validation to check whether the same FX rate value has been entered in reverse order. Users would populate a spreadsheet consisting of three columns:

  • 来自货币
  • 到货币
  • 评价

用户将数据添加到电子表格中以通过系统导入,以下是数据外观的模拟示例:

The user would add data into a spreadsheet to import through the system, below is a mock example of how the data would look:

我已经有了验证的结构,我只是想知道如何编写 select 语句以返回所有具有两个方向的行(如本例中的前两行),因为它们是相同的货币,只是顺序相反.

I have the structure of the validation in place, I am just wondering how I would write a select statement in order to return all rows which have both directions (like the first two rows in this example), due to them being the same currency, just in reverse order.

推荐答案

嗯,你可以使用:

select fx.*
from fx
where exists (select 1
              from fx fx2
              where fx2.fromc = fx.toc and fx2.toc = tx.fromc and fx2.rate = fx.rate
             );

实际上,您可以扩展此检查以确保这些值接近于彼此的算术倒数.因为十进制值有一些松弛,我建议:

Actually, you might extend this check to be sure the values come close to being arithmetic inverses of each other. Because there is some slack with decimal values, I would suggest:

select fx.*
from fx
where exists (select 1
              from fx fx2
              where fx2.fromc = fx.toc and fx2.toc = tx.fromc and
                    abs(fx2.rate * fx.rate - 1) > 0.001
             );

这会检查产品的偏差是否超过千分之一.您想要的确切阈值取决于您的需求.

This checks that the product is off by more than one thousandth. The exact threshold you want depends on your needs.

这篇关于SQL Server - 针对两列中的重复值进行反向验证 [内部示例]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:在sql中从当前时间获取过去24小时 下一篇:SQL:使用来自具有相同列名的两个表中的数据......等等

相关文章

最新文章