比较两个具有相同列名但不同日期列名称的表格
有
ID1 | DT |
---|---|
X1 | 2022-04-10 |
A2 | 2022-04-10 |
A1 | 2022-04-10 |
X1 | 2022-05-10 |
X2 | 2022-04-10 |
Y2 | 2022-04-10 |
| 表 |
我 | A -06 -10 |
表B
ID1 | DT |
---|---|
A1 | 2022-04-10 |
C3 | 2022-04-10 |
A1 | 2022-05-10 |
L1 | 2022-05-10 |
B1 | 2022-04-10 Y2 2022-04-10 |
Y2 | 2022-04-10 |
x1 | 2022-06-10 |
Z1 | 2022-05-10 |
具有日期值,但列名称不同(表A的'dt'和表B的“日期”)
所需的输出:
ID1日期 | 注意:a和b |
---|---|
x1 | 2022-04-10 |
A2 A2 | 2022-04-10 |
X2 | 2022-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
id1 | dt |
---|---|
x1 | 2022-04-10 |
a2 | 2022-04-10 |
a1 | 2022-04-10 |
x1 | 2022-05-10 |
x2 | 2022-04-10 |
y2 | 2022-04-10 |
y1 | 2022-05-10 |
x1 | 2022-06 -10 |
Table B
id1 | dt |
---|---|
a1 | 2022-04-10 |
c3 | 2022-04-10 |
a1 | 2022-05-10 |
l1 | 2022-05-10 |
b1 | 2022-04-10 |
y2 | 2022-04-10 |
x1 | 2022-06-10 |
z1 | 2022-05-10 |
Note: A and B has date values but different column names ( 'dt' for Table A and 'date' for table B)
Desired output:
id1 | DATE |
---|---|
x1 | 2022-04-10 |
a2 | 2022-04-10 |
x2 | 2022-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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不完全确定您的输出。但是我认为您想要这样的东西:
输出:
I'm not completely sure of your output. But I think you want something like this:
Output:
您可以通过在左连接和右连接表之间应用
Union
语句来做到这一点。从这些连接到您的目标是那些由于没有匹配而其第二个表值为null的行:找到SQL小提琴在这里。
编辑
仅从表A中获得结果:
仅来自表B?
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:Find the SQL Fiddle here.
EDIT
Want results only from table A:
Only from table B?
这也有效
This worked as well