我有一个表'Device',其中包含安装日期和客户:
Id | 设备名称 | 已安装客户 | 1 | 2021-10-10 | 1 | 2
---|---|---|---|
B | 2021-10-10 | 1 | |
3 | 2021-10-20 | 2 | |
2021-10-30 | 3 |
Test this:
SELECT t1.DeviceName,
t1.Installed InstalledFrom,
t2.Installed InstalledTo,
t1.Customer
FROM Device t1
LEFT JOIN Device t2 ON t1.DeviceName = t2.DeviceName
AND t1.Installed < t2.Installed
WHERE NOT EXISTS ( SELECT NULL
FROM Device t3
WHERE t1.DeviceName = t3.DeviceName
AND t1.Installed < t3.Installed
AND t3.Installed < t2.Installed )