两个表中的串联列不应匹配

发布于 2024-10-16 20:16:02 字数 1001 浏览 3 评论 0原文

我只是用外行的话来说,因为我是一个完全的菜鸟:

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

安穩 2024-10-23 20:16:02

为了更容易理解,我省略了 dblink。
因为,第一个选择 B 中的行等于 A 中的员工编号以及 A 中的销售额,因此它们的串联值也将相等(如果您认为这不是真的,请提供一些测试数据)。

SELECT * from firsttable A
LEFT JOIN secondtable B where 
    (A.employee_number = B.employee_number AND a.salery != b.salery) OR 
    (A.salery = B.salery AND A.employee_number != B.employee_number)

如果您在处理包含空值的行时遇到问题,您也可以尝试如下操作:
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).

SELECT * from firsttable A
LEFT JOIN secondtable B where 
    (A.employee_number = B.employee_number AND a.salery != b.salery) OR 
    (A.salery = B.salery AND A.employee_number != B.employee_number)

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))

浸婚纱 2024-10-23 20:16:02

我认为您正在寻找类似的东西。

(样本数据)

create table A (
    employee_number integer primary key,
    salary integer not null
);

create table B (
    employee_number integer primary key,
    salary integer not null
);

insert into A values 
(1, 20000),
(2, 30000),
(3, 20000);  -- This row isn't in B

insert into B values
(1, 20000),  -- Combination in A
(2, 20000),  -- Individual values in A
(3, 50000);  -- Only emp number in A

select A.employee_number, A.salary
from A
where (A.employee_number, A.salary) NOT IN (select employee_number, salary from B)
  and  A.employee_number IN (select employee_number from B)
  and  A.salary IN (select salary from B)

输出:3、20000

I think you're looking for something along these lines.

(Sample data)

create table A (
    employee_number integer primary key,
    salary integer not null
);

create table B (
    employee_number integer primary key,
    salary integer not null
);

insert into A values 
(1, 20000),
(2, 30000),
(3, 20000);  -- This row isn't in B

insert into B values
(1, 20000),  -- Combination in A
(2, 20000),  -- Individual values in A
(3, 50000);  -- Only emp number in A

select A.employee_number, A.salary
from A
where (A.employee_number, A.salary) NOT IN (select employee_number, salary from B)
  and  A.employee_number IN (select employee_number from B)
  and  A.salary IN (select salary from B)

output: 3, 20000

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文