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

SQL Server - Validation against duplicate values in reverse in two columns [Example Inside](SQL Server - 针对两列中的重复值进行反向验证 [内部示例])
本文介绍了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 - 针对两列中的重复值进行反向验证 [内部示例]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Query with t(n) and multiple cross joins(使用 t(n) 和多个交叉连接进行查询)
Unpacking a binary string with TSQL(使用 TSQL 解包二进制字符串)
Max rows in SQL table where PK is INT 32 when seed starts at max negative value?(当种子以最大负值开始时,SQL 表中的最大行数其中 PK 为 INT 32?)
Inner Join and Group By in SQL with out an aggregate function.(SQL 中的内部连接和分组依据,没有聚合函数.)
Add a default constraint to an existing field with values(向具有值的现有字段添加默认约束)
SQL remove from running total(SQL 从运行总数中删除)