获取过去一个月、一天和过去特定 24 小时内的记录

Get records for past month, day and a specific 24 hour period in the past

目前我有:

SELECT timeslot_start_time
    , name
    , description
    , etc
FROM Table1
WHERE Timeslot_start_time >= '2022-02-01' 
ORDER BY Timeslot_start_time

Q1。有没有办法 table 只自动获取从今天算起一个月前的数据,而不是手动执行此操作?

Q2。此外,我的数据格式为:22-02-15 21:45:00,每 15 分钟读取一次数据。有没有一种方法可以创建一个列(/table 或最好的列)来自动过滤过去 24 小时?

Q3。此外,是否可以从特定时间过滤过去 24 小时。例如过滤最后 24 小时到晚上 10 点,所以晚上 10 点到晚上 10 点。

我希望所有这些都有意义

可以使用基本日期算法查询您的数据:

gets data that is a month old from today automatically?

“一个月”含糊不清(例如过去 30 天、上个月的所有日期等)。但最简单的定义是使用 dateadd 并让 SQL 服务器决定:

WHERE timeslot_start_time >= DATEADD(MONTH, -1, CURRENT_TIMESTAMP)

that filters only the last 24hours automatically?

WHERE timeslot_start_time >= DATEADD(DAY, -1, CURRENT_TIMESTAMP)

is it possible to filter the last 24hrs but from a specific time. For example filtering the last 24hrs up to 10pm, So 10pm-10pm.

DECLARE @d2 DATETIME = DATETIMEFROMPARTS(
    DATEPART(YEAR, CURRENT_TIMESTAMP),
    DATEPART(MONTH, CURRENT_TIMESTAMP),
    DATEPART(DAY, CURRENT_TIMESTAMP),
    22,
    0,
    0,
    0
);
IF @d2 > CURRENT_TIMESTAMP
    -- if 10pm today is in the future then use yesterday
    SET @d2 = DATEADD(DAY, -1, @d2);

SELECT ...
WHERE timeslot_start_time >= DATEADD(DAY, -1, @d2)
AND   timeslot_start_time <  @d2