选择带有 ID 和上次日期验证的查询

发布于 2024-12-15 18:31:30 字数 954 浏览 1 评论 0原文

使用 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 技术交流群。

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

发布评论

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

评论(2

身边 2024-12-22 18:31:30

尝试一下:

Select * from table2 t2 where t2.date > (select max(date) from table1 t1 where t1.id = t2.id)

编辑:

SELECT * FROM Table2 t2
WHERE t2.Date > 
(
    SELECT ISNULL(maxdate, 0) FROM 
    (
        SELECT MAX(t1.Date) as maxdate FROM Table1 t1 WHERE t1.ID = t2.ID
    ) t
)

Try it:

Select * from table2 t2 where t2.date > (select max(date) from table1 t1 where t1.id = t2.id)

Edited:

SELECT * FROM Table2 t2
WHERE t2.Date > 
(
    SELECT ISNULL(maxdate, 0) FROM 
    (
        SELECT MAX(t1.Date) as maxdate FROM Table1 t1 WHERE t1.ID = t2.ID
    ) t
)
缘字诀 2024-12-22 18:31:30

看来您在这里需要的是反连接。例如,您可以使用 LEFT JOIN & 来实现反连接。 IS NULL 检查:

SELECT
  t2.columns
FROM table2 t2
  LEFT JOIN table1 t1 ON t2.ID = t1.ID AND t2.Date <= t1.Date
WHERE t1.ID IS NULL

另一种方法是使用 NOT EXISTS

SELECT
  columns
FROM table2 t2
WHERE NO EXISTS (
  SELECT *
  FROM table1 t1
  WHERE t2.ID = t1.ID
    AND t2.Date <= t1.Date
)

Seems like an anti-join is what you need here. You could implement an anti-join, for instance, with LEFT JOIN & IS NULL check:

SELECT
  t2.columns
FROM table2 t2
  LEFT JOIN table1 t1 ON t2.ID = t1.ID AND t2.Date <= t1.Date
WHERE t1.ID IS NULL

Another way to do the same would be to use NOT EXISTS:

SELECT
  columns
FROM table2 t2
WHERE NO EXISTS (
  SELECT *
  FROM table1 t1
  WHERE t2.ID = t1.ID
    AND t2.Date <= t1.Date
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文