SQL - 设置差异并获取不属于差异一部分的字段

发布于 2024-11-05 08:16:10 字数 925 浏览 0 评论 0原文

我有一个基本上执行类似以下操作的查询:

select a, b, c
from tab
where tab.state = 'A'
minus
select a, b, c
from tab
where tab.state = 'B'

在此示例中,abc 是该表的关键字段。 state 也是键的一部分,我正在尝试查找状态 A 中有记录而不是状态 B 的情况。还有另一个字段(不在键中)我我想报告,不同州的同一条记录可能会有所不同。示例:

a  b  c  state  value
---------------------
1  1  1  A      12
1  2  2  A      1002
1  3  9  A      43
1  1  1  B      17.34
1  2  2  B      1002

在本例中,我对键为 1,3,9 且状态为 A 的行感兴趣。我还想获取 value 的值code> 列,但如果我尝试:

select a, b, c, value
from tab
where tab.state = 'A'
minus
select a, b, c, value
from tab
where tab.state = 'B'

我会返回两行:

a  b  c    value
----------------
1  1  1      12
1  3  9      43

基本上,我希望结果集中有 value,但不参与 minus 。我觉得我在这里错过了一些明显的东西,但也许我只是太累了而无法得到它......;)

I have a query that basically performs something like this:

select a, b, c
from tab
where tab.state = 'A'
minus
select a, b, c
from tab
where tab.state = 'B'

In this example, a, b, and c are the key fields of this table. state is also a part of the key, and I'm trying to find situations where there is a record in state A and not in state B. There is another field (not in the key) that I'd like to report on, value, that might be different for the same record in different states. Example:

a  b  c  state  value
---------------------
1  1  1  A      12
1  2  2  A      1002
1  3  9  A      43
1  1  1  B      17.34
1  2  2  B      1002

In this case, I'm interested in the row whose key is 1,3,9 where state is A. I'd also like to get the value of the value column, but if I try:

select a, b, c, value
from tab
where tab.state = 'A'
minus
select a, b, c, value
from tab
where tab.state = 'B'

What I would get returned is two rows:

a  b  c    value
----------------
1  1  1      12
1  3  9      43

Basically, I want to have value in the result set, but not participate in the minus. I feel like I'm missing something obvious here, but maybe I'm just too tired to get it... ;)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

死开点丶别碍眼 2024-11-12 08:16:11

执行此操作的明显方法如下:

select a, b, c, value
from tab
where tab.state = 'A' and not exists (
  select 1                          -- let the optimizer do its thing
  from tab ti
  where tab.state = 'B' and ti.a=tab.a and ti.b=tab.b and ti.c=tab.c)

如果数据可以有双精度,我什至会在外部查询中添加一个 distinct

The obvious way to do this is like this:

select a, b, c, value
from tab
where tab.state = 'A' and not exists (
  select 1                          -- let the optimizer do its thing
  from tab ti
  where tab.state = 'B' and ti.a=tab.a and ti.b=tab.b and ti.c=tab.c)

I would even add a distinct in the outer query if the data can have doubles.

笑脸一如从前 2024-11-12 08:16:11

您可以将 state = 'A' 的所有行与 state = 'B' 的匹配行连接起来......

SELECT t1.a, t1.b, t1.c, t1.value, t2.value v2
FROM (SELECT a, b, c, value FROM tab WHERE state = 'A') t1
     LEFT JOIN (SELECT a, b, c, value FROM tab WHERE state = 'B') t2
            ON t1.a = t2.a AND t1.b = t2.b AND t1.c = t2.c

然后选择没有的行匹配:

 SELECT a, b, c, value
 FROM ( /* previous query */ )
 WHERE v2 IS NULL

You can join all rows where state = 'A' with the matching ones with state = 'B'...

SELECT t1.a, t1.b, t1.c, t1.value, t2.value v2
FROM (SELECT a, b, c, value FROM tab WHERE state = 'A') t1
     LEFT JOIN (SELECT a, b, c, value FROM tab WHERE state = 'B') t2
            ON t1.a = t2.a AND t1.b = t2.b AND t1.c = t2.c

...and then pick the rows where there were no match:

 SELECT a, b, c, value
 FROM ( /* previous query */ )
 WHERE v2 IS NULL
伴梦长久 2024-11-12 08:16:11
SELECT a,
  b,
  c,
  value
FROM tab tab1
INNER JOIN
  (SELECT a, b, c FROM tab WHERE tab.state = 'A'
  MINUS
  SELECT a, b, c FROM tab WHERE tab.state = 'B'
  ) tab2
ON tab1.a  = tab2.a
AND tab1.b = tab2.b
AND tab1.c = tab2.c 

我相信上面的代码可以解决问题。

SELECT a,
  b,
  c,
  value
FROM tab tab1
INNER JOIN
  (SELECT a, b, c FROM tab WHERE tab.state = 'A'
  MINUS
  SELECT a, b, c FROM tab WHERE tab.state = 'B'
  ) tab2
ON tab1.a  = tab2.a
AND tab1.b = tab2.b
AND tab1.c = tab2.c 

I believe above code would do the trick.

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