OrderBy date但包含来自" half"的回复前一天

时间:2015-09-22 05:51:08

标签: sql tsql date datetime

SELECT cast(Ord.regdate AS date) AS Date,
       COUNT(Ord.Ordernumber) AS [Backlog of line],

  (SELECT CASE
              WHEN CAST(SUM(Ord.Qty) AS INT) IS NULL THEN 0
              ELSE CAST(SUM(Ord.Qty) AS INT)
          END) AS [Backlog of Qty]
FROM Orders [Ord]
WHERE Ord.regdate < CAST(CAST(CAST(DATEADD(DAY,-1,GETDATE()) AS date) AS varchar(10)) + ' 12:00' AS datetime)
GROUP BY cast(Ord.regdate AS date)
ORDER BY cast(Ord.regdate AS date) DESC    

日期:积压行:数量积压

2015-09-20:10:50

2015-09-21:5:25

这里的问题是,当我们到达第二天时......如果订单是在2015-09-21 12:00之后完成的,那么21将获得更多记录......

当我们运行它时,它只记录前一天比较早的记录,并按日期对它们进行分组。

我的问题是...... 我希望2015-09-22的日期能够在2015-09-21 12:00之后显示并记录2015-09-22的12:00 ...等等

1 个答案:

答案 0 :(得分:0)

这应该有效。它首先将regdate改变了12个小时。

SELECT Ord.regdate AS Date,
       COUNT(Ord.Ordernumber) AS [Backlog of line],

  (SELECT CASE
              WHEN CAST(SUM(Ord.Qty) AS INT) IS NULL THEN 0
              ELSE CAST(SUM(Ord.Qty) AS INT)
          END) AS [Backlog of Qty]
FROM (
    Select Ordernumber, Qty, CAST(DATEADD(HOUR, 12, regdate) as date) as regdate
    From Orders 
    WHERE regdate < CAST(CAST(CAST(DATEADD(DAY,-1,GETDATE()) AS date) AS varchar(10)) + ' 12:00' AS datetime)
) [Ord]
GROUP BY Ord.regdate
ORDER BY Ord.regdate DESC