SQL 获取两天而不是午夜的每日总计的语句

SQL Statement to get Daily Totals across two days instead of Midnight

我最近写了一个查询来获取一个月内的每日总计。查询工作得很好,但它会在午夜到午夜。我需要找到从 7:15 上午到第二天 7:15 上午的总数。我们 运行 从 7:15 开始三班倒。

SELECT CONVERT(datetime,CAST(t_stamp AS DATE)), COUNT(bottles) AS 'Good Bottles'
FROM bottles
WHERE t_stamp
    BETWEEN starrt
    AND end
GROUP BY CAST(t_stamp AS DATE)
ORDER BY CAST(t_stamp AS DATE) ASC

只需减去一个偏移量。一个简单的方法是:

SELECT CAST(DATEADD(minute, -(7 * 60 + 15), t_stamp) AS DATE),
       COUNT(bottles) AS GoodBottles
FROM bottles
WHERE t_stamp BETWEEN starrt AND end
GROUP BY CAST(DATEADD(minute, -(7 * 60 + 15), t_stamp) AS DATE)
ORDER BY CAST(DATEADD(minute, -(7 * 60 + 15), t_stamp) AS DATE) ASC;

请注意,end 是一个非常糟糕的列名,因为它是 SQL 关键字。