如何使SQL Server返回落在单独表中定义的多个时间(或任何值)范围中的任何一个范围内的记录?


0

下面的代码澄清了标题中的问题。什么可以放在WHERE使其工作?

CREATE TABLE #PickedTimes
(StartTime smalldatetime,
 EndTime smalldatetime)

INSERT INTO #PickedTimes
VALUES  ('2019-01-25 16:05', '2019-01-25 17:05'),
        ('2019-01-25 19:05', '2019-01-25 20:05')
--Each row is a time range. There would be more in the real situation, and it is desirable to define them this way.

SELECT * FROM #PickedTimes

-- No questions up to here.

SELECT TransactionID, Timestamp
FROM Transactions
WHERE Timestamp -- SOMETHING WITH #PickedTimes. How to make it pick any of the time ranges in #PickedTimes?

Answers:


0

可能你需要这个:

SELECT TransactionID, Timestamp
FROM Transactions INNER JOIN #PickedTimes
WHERE Transactions.Timestamp >= #PickedTimes.StartTime 
    AND Transactions.Timestamp <= #PickedTimes.EndTime
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.