两个表中的串联列不应匹配
我只是用外行的话来说,因为我是一个完全的菜鸟:
我有 2 个表 A 和 B,都有 2 个感兴趣的列,即:员工编号和薪水。
我想要做的是从 A 中提取雇员编号和薪水的“组合”行,这些行不存在于 B 中,但是雇员编号和薪水中的每一个都应该存在于两者中。
我希望在以下两个条件下做到这一点(请原谅错误的功能 名字..这只是为了“雄辩地”提出问题):
1.)A.unique(employee_number)存在于B.unique(employee_number)和A.unique(薪水)中 存在于 B.unique(salary)
2.) A.concat(employee_number,salary) <> B.concat(employee_number,salary)
注意:A 和 B 位于不同的数据库中,因此我希望使用 dblink 来执行此操作。
这就是我尝试做的:
SELECT distinct * FROM dblink('dbname=test1 port=5432
host=test01 user=user password=password','SELECT employee_number,salary, employee_number||salary AS ENS FROM empsal.A')
AS A(employee_number int8, salary integer, ENS numeric)
LEFT JOIN empsalfull.B B on B.employee_number = A.employee_number AND B.salary = A.salary
WHERE A.ENS not in (select distinct employee_number || salary from empsalfull.B)
但结果是错误的,因为我使用电子表格进行了交叉检查,但没有得到相同的结果。
任何帮助将不胜感激。谢谢。
I'll just put this in layman's terms since I'm a complete noobie:
I have 2 tables A and B, both having 2 columns of interest namely: employee_number and salary.
What I am looking to do is to extract rows of 'combination' of employee_number and salary from A that are NOT present in B, but each of employee_number and salary should be present in both.
I am looking to doing it with the 2 following conditions(please forgive the wrong function
names.. this is just to present the problem 'eloquently'):
1.) A.unique(employee_number) exists in B.unique(employee_number) AND A.unique(salary)
exists in B.unique(salary)
2.) A.concat(employee_number,salary) <> B.concat(employee_number,salary)
Note: A and B are in different databases, so I'm looking to use dblink to do this.
This is what I tried doing:
SELECT distinct * FROM dblink('dbname=test1 port=5432
host=test01 user=user password=password','SELECT employee_number,salary, employee_number||salary AS ENS FROM empsal.A')
AS A(employee_number int8, salary integer, ENS numeric)
LEFT JOIN empsalfull.B B on B.employee_number = A.employee_number AND B.salary = A.salary
WHERE A.ENS not in (select distinct employee_number || salary from empsalfull.B)
but it turned out to be wrong as I had it cross-checked by using spreadsheets and I don't get the same result.
Any help would be greatly appreciated. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了更容易理解,我省略了 dblink。
因为,第一个选择 B 中的行等于 A 中的员工编号以及 A 中的销售额,因此它们的串联值也将相等(如果您认为这不是真的,请提供一些测试数据)。
如果您在处理包含空值的行时遇到问题,您也可以尝试如下操作:
AND (a.salery != b.salery OR (a.salery IS NULL AND b.salery IS NOT NULL) 或 (a.salery IS NOT
NULL 和 b.salery IS NULL))
For easier understanding I left out the dblink.
Because, the first one selects lines in B that equal the employeenumber in A as well as the salery in A, so their concatenated values will equal as well (if you expect this to not be true, please provide some test data).
If you have troubles with lines containing nulls, you might also try somthing like this:
AND (a.salery != b.salery OR (a.salery IS NULL AND b.salery IS NOT NULL) or (a.salery IS NOT
NULL and b.salery IS NULL))
我认为您正在寻找类似的东西。
(样本数据)
输出:3、20000
I think you're looking for something along these lines.
(Sample data)
output: 3, 20000