【发布时间】:2017-04-09 01:20:03
【问题描述】:
我需要查找丢失的行,但是,我需要返回两个表中的数据。我查了谷歌,但没有找到类似的问题。
TableA
thetime real-time
1 1 pm
2 5 pm
3 7 pm
4 9 pm
5 11 pm
Table2
thedate transaction_num thetime
1/1/2000 111 1
1/1/2000 111 4
1/1/2000 111 5
2/1/2000 111 2
2/1/2000 111 4
2/1/2000 222 1
2/1/2000 222 5
我需要从 Table2 中选择在 Table1 中没有时间的日期和 transaction_num,因此 select 语句的结果应该包含不在 table2 中的缺失时间的日期和交易编号:
thedate transaction_num thetime
1/1/2000 111 2
1/1/2000 111 3
2/1/2000 111 1
2/1/2000 111 3
2/1/2000 111 5
2/1/2000 222 2
2/1/2000 222 3
2/1/2000 222 4
这是我拥有的代码,但它给了我一个多部分绑定错误:
select t2.thedate, t2.transaction_num, t1.thetime
from table2 t2
where not exists(select t1.thetime
from table1 t1
where t2.thetime = t1.thetime)
有谁知道如何解决这个问题或可以指出我的答案? 缺少行的堆栈溢出中的大多数问题都涉及从一个表返回数据,但我需要它用于 2 个表。
谢谢
【问题讨论】:
-
如果您在 t2 中明确检查在 t1 中未找到
thetime的行,您希望t1.thetime出现在您的 select 子句中吗? -
我不确定我是否理解。
thetime2、3 和 1(来自您的预期结果)确实出现在您从Table A提供的示例中。你能解释一下吗? -
如果您加入
t2.thetime = t1.thetime,那么为什么不在您的选择语句中使用t2.thetime? -
如果我使用 t2.thetime,我将获得我已经拥有的时间。我需要每个交易和日期没有的时间。
-
我需要交易和日期,但对于没有出现在 table2 中的时间。例如,日期 1/1/2000 的事务 111 的时间 = 2 确实出现在表 2 中,所以我必须让它出现在我的结果中
标签: sql sql-server missing-data