开发者

SQL Server 2005 statements returning the same results

Why are these two statements returning the same results? I'm looking at the total number of orders and the sum of the prices for some publications. The first statement gets records from the past 24 hours and the second one is supposed to show the same sums but from the start of the month to the current date. When I look at the g开发者_JAVA技巧ridviews to which they are filling, it looks like BOTH of them are displaying the sums from the past 24 hours. Anything noticeable?

SELECT pubName AS Publication, COUNT(*) AS Total, '$' + CONVERT(VARCHAR(12), SUM(CAST(price AS DECIMAL))) AS Price FROM [SecureOrders] WHERE DateTime >= DATEADD(day, -1, GETDATE()) GROUP BY pubName

SELECT pubName AS Publication, COUNT(*) AS Total, '$' + CONVERT(VARCHAR(12), SUM(CAST(price AS DECIMAL))) AS Price FROM [SecureOrders] WHERE DateTime >= DATEADD(DAY, 1-DAY(GETDATE()), DATEDIFF(DAY, 0, GETDATE())) GROUP BY pubName


IF one of them is supposed to be the past 24 hours, you should be looking at hours.

SELECT
    pubName AS Publication
    ,COUNT(*) AS Total
    ,'$' + CONVERT(VARCHAR(12), SUM(CAST(price AS DECIMAL))) AS Price
FROM [SecureOrders] 
WHERE DateTime >= DATEADD(hh, -24, GETDATE())
GROUP BY pubName

Otherwise the dates are perfectly fine in both...assuming the DateTime column is in fact DateTime I would check the datasources for the grid.


Not sure why you need the DateDiff clause. Maybe I'm missing something.

SELECT pubName AS Publication, COUNT(*) AS Total, '$' + CONVERT(VARCHAR(12), SUM(CAST(price AS DECIMAL))) AS Price FROM [SecureOrders] WHERE DateTime >= DATEADD(DAY, 1-DAY(GETDATE()), GETDATE()) GROUP BY pubName

Edit: Ignore the above, I was being dense(r).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜