比较两个具有相同列名但不同日期列名称的表格

发布于 2025-01-28 04:41:45 字数 1629 浏览 3 评论 0原文

ID1DT
X12022-04-10
A22022-04-10
A12022-04-10
X12022-05-10
X22022-04-10
Y22022-04-10
A -06 -10

表B

ID1DT
A12022-04-10
C32022-04-10
A12022-05-10
L12022-05-10
B12022-04-10 Y2 2022-04-10
Y22022-04-10
x12022-06-10
Z12022-05-10

具有日期值,但列名称不同(表A的'dt'和表B的“日期”)

所需的输出:

ID1日期注意:a和b
x12022-04-10
A2 A22022-04-10
X22022-04-10

更新1: 笔记: 1)Y2,A1不在期望的输出中,因为它们都存在于两个表中 2)C3不在期望的输出中,因为它存在于表B

ID1中,但我只想从表A中的“ ID1”值来自表B中,该表A中不在表B中,日期为2022-04-10。<<<<<<<< /强>

到目前为止采取的步骤: 从Where DT =“ 2022-04-10”中选择ID1,除了从B选择ID1 结果(它运行很长时间并定时出现,不确定其由于数据数量或查询是错误的)是否会使用较小的表格进行检查。

我无法使用左内接合,因为日期列不同(日期与DT)。

任何帮助都将不胜感激。

I have table A

id1dt
x12022-04-10
a22022-04-10
a12022-04-10
x12022-05-10
x22022-04-10
y22022-04-10
y12022-05-10
x12022-06 -10

Table B

id1dt
a12022-04-10
c32022-04-10
a12022-05-10
l12022-05-10
b12022-04-10
y22022-04-10
x12022-06-10
z12022-05-10

Note: A and B has date values but different column names ( 'dt' for Table A and 'date' for table B)

Desired output:

id1DATE
x12022-04-10
a22022-04-10
x22022-04-10

Update 1:
note:
1)y2,a1 are not in Desired output because they exists in both tables
2)c3 is not in desired output because it exists in Table B

id1 exists in both tables, but i want "id1" values only from Table A which are not in Table B for the date 2022-04-10.

steps taken so far:
select id1 from A where dt="2022-04-10" EXCEPT select id1 from B
result ( its running for a long time and timing out, not sure its because of Volume of Data or the query is wrong) will check with smaller tables.

I can't use Left Inner Join because Date columns are different ( date vs dt).

Any help would be much appreciated.

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

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

发布评论

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

评论(3

赢得她心 2025-02-04 04:41:45

我不完全确定您的输出。但是我认为您想要这样的东西:

bind_rows(
  anti_join(rename(a,date=dt) %>% filter(date=="2022-04-10"),b %>% filter(date=="2022-04-10")),
  anti_join(b %>% filter(date=="2022-04-10"),rename(a,date=dt) %>% filter(date=="2022-04-10"))
)

输出:

   id1       date
1:  x1 2022-04-10
2:  a2 2022-04-10
3:  x2 2022-04-10
4:  y2 2022-04-10
5:  a1 2022-04-10
6:  c3 2022-04-10
7:  b1 2022-04-10

I'm not completely sure of your output. But I think you want something like this:

bind_rows(
  anti_join(rename(a,date=dt) %>% filter(date=="2022-04-10"),b %>% filter(date=="2022-04-10")),
  anti_join(b %>% filter(date=="2022-04-10"),rename(a,date=dt) %>% filter(date=="2022-04-10"))
)

Output:

   id1       date
1:  x1 2022-04-10
2:  a2 2022-04-10
3:  x2 2022-04-10
4:  y2 2022-04-10
5:  a1 2022-04-10
6:  c3 2022-04-10
7:  b1 2022-04-10
一杯敬自由 2025-02-04 04:41:45

您可以通过在左连接和右连接表之间应用Union语句来做到这一点。从这些连接到您的目标是那些由于没有匹配而其第二个表值为null的行:

SELECT tableA.id1,
       tableA.dt
FROM      tableA 
LEFT JOIN tableB
       ON tableA.id1 = tableB.id1 
      AND tableA.dt = tableB.dt
WHERE tableB.id1 IS NULL
  AND tableA.dt = '2022-04-10' 

UNION 

SELECT tableB.id1,
       tableB.dt
FROM      tableB 
LEFT JOIN tableA
       ON tableB.id1 = tableA.id1 
      AND tableB.dt = tableA.dt
WHERE tableA.id1 IS NULL
  AND tableB.dt = '2022-04-10'

找到SQL小提琴在这里


编辑

仅从表A中获得结果:

SELECT tableA.id1,
       tableA.dt
FROM      tableA 
LEFT JOIN tableB
       ON tableA.id1 = tableB.id1 
      AND tableA.dt = tableB.dt
WHERE tableB.id1 IS NULL
  AND tableA.dt = '2022-04-10' 

仅来自表B?

SELECT tableB.id1,
       tableB.dt
FROM      tableB 
LEFT JOIN tableA
       ON tableB.id1 = tableA.id1 
      AND tableB.dt = tableA.dt
WHERE tableA.id1 IS NULL
  AND tableB.dt = '2022-04-10'

You can do it by applying a UNION statement between a left joined and a right joined table. From these joins your targets are those rows whose second table values is NULL because of no match:

SELECT tableA.id1,
       tableA.dt
FROM      tableA 
LEFT JOIN tableB
       ON tableA.id1 = tableB.id1 
      AND tableA.dt = tableB.dt
WHERE tableB.id1 IS NULL
  AND tableA.dt = '2022-04-10' 

UNION 

SELECT tableB.id1,
       tableB.dt
FROM      tableB 
LEFT JOIN tableA
       ON tableB.id1 = tableA.id1 
      AND tableB.dt = tableA.dt
WHERE tableA.id1 IS NULL
  AND tableB.dt = '2022-04-10'

Find the SQL Fiddle here.


EDIT

Want results only from table A:

SELECT tableA.id1,
       tableA.dt
FROM      tableA 
LEFT JOIN tableB
       ON tableA.id1 = tableB.id1 
      AND tableA.dt = tableB.dt
WHERE tableB.id1 IS NULL
  AND tableA.dt = '2022-04-10' 

Only from table B?

SELECT tableB.id1,
       tableB.dt
FROM      tableB 
LEFT JOIN tableA
       ON tableB.id1 = tableA.id1 
      AND tableB.dt = tableA.dt
WHERE tableA.id1 IS NULL
  AND tableB.dt = '2022-04-10'
捎一片雪花 2025-02-04 04:41:45

这也有效

with A as (
select distinct id1
from Table_A
where dt = '2022-04-10'
)

, B as (
select distinct id1
from  Table_B
where date = '2022-04-10')

select id1 from A
where id1 not in (select id1 from B)

This worked as well

with A as (
select distinct id1
from Table_A
where dt = '2022-04-10'
)

, B as (
select distinct id1
from  Table_B
where date = '2022-04-10')

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