选择带有 ID 和上次日期验证的查询
使用 SQL Server 2000
表 1:
ID LastDate
001 20090101
003 20090501
004 20090302
...
table2:
ID Date Value
001 20090101 100
001 20090102 200
001 20090103 200
002 20090101 350
002 20090302 500
003 20090501 200
003 20090502 250
004 20090302 400
...
从 table2 我想选择 id 和日期,日期应从 table1 的下一个日期开始显示。
查询如下:
Select * from table2 where date > table1.last on table1.id = table2.id
预期输出:
ID Date Value
001 20090102 200
001 20090103 200
002 20090101 350
002 20090302 500
003 20090502 250
...
从上面的输出中,
id 001 is displaying from the next date of table1.lastdate
id 002 is displaying the same date because 002 is not in table1
id 003 is displaying from the next date of table1.lastdate
id 004 is not displaying because the same date is available on table1.lastdate
我想对上述条件进行查询。
Using SQL Server 2000
table 1:
ID LastDate
001 20090101
003 20090501
004 20090302
...
table2:
ID Date Value
001 20090101 100
001 20090102 200
001 20090103 200
002 20090101 350
002 20090302 500
003 20090501 200
003 20090502 250
004 20090302 400
...
From the table2 I want to Select id and date, date should display from next date onwards from table1.
Query like:
Select * from table2 where date > table1.last on table1.id = table2.id
Expected Output:
ID Date Value
001 20090102 200
001 20090103 200
002 20090101 350
002 20090302 500
003 20090502 250
...
From the above output,
id 001 is displaying from the next date of table1.lastdate
id 002 is displaying the same date because 002 is not in table1
id 003 is displaying from the next date of table1.lastdate
id 004 is not displaying because the same date is available on table1.lastdate
I want to make a query for the above condition.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试一下:
编辑:
Try it:
Edited:
看来您在这里需要的是反连接。例如,您可以使用
LEFT JOIN
& 来实现反连接。IS NULL
检查:另一种方法是使用
NOT EXISTS
:Seems like an anti-join is what you need here. You could implement an anti-join, for instance, with
LEFT JOIN
&IS NULL
check:Another way to do the same would be to use
NOT EXISTS
: