所以我有2张桌子。在第一个表中,我需要将 ID 与最小和最大日期时间相关联,我使用了
SELECT id, min (datetime) as earlytime, max(datetime) as latesttime
FROM table1
group by id
结果,我得到了大约 5k 的 ID 和 2 个时间戳。
我需要使用此信息从 table2 中获取具有 ID 的行,其中每个 ID 的日期时间分别在它们的 earlytime 和 latesttime 之间。或者只是 ID 和时间小于最新时间的行。
仍然无法弄清楚如何做到这一点。
想出了类似的东西
SELECT ID, source, amount, type, datetime
FROM table2
WHERE EXISTS (SELECT ID, min (datetime) as earlytime, max (datetime) as latetime
FROM table1
group by ID)
但我猜它只显示了与 table1 中的 ID 匹配的任何行
任何人都可以帮助我吗?
也许您可以加入并获得如下结果
select A.*
from table2 A
join
(
SELECT id, min (datetime) as earlytime, max(datetime) as latesttime
FROM table1
group by id
)B
on A.id=B.id and
B.timecol between earlytime and latesttime
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句