SQL Server 2008 本月迄今
希望这个问题很容易回答。
我正在处理一个需要 MTD 数据的表。 使用
MONTH (@monthtodate)= 11
我们的一位 SQL 人员告诉我在 SQL Server Management Studio 的参数列表中 Where @monthtodate
is set to GetDate()
。因此,他说,“理论上”,它应该选择月份 (11),然后获取今天并返回这两个日期之间的所有请求的数据。但我认为这是不正确的。
在查看我的数据时,我开始认为它只是返回 11 月份整个月份的数据,而不仅仅是 MTD。我想,从技术上来说,任何有 0 的东西都不会被计算。但这仅仅意味着代码写得不好,对吗?
在您看来,这是返回 MTD 数据的更好方法吗:
production_date <= @today and Production_Date >= DATEADD(mm, DATEDIFF(mm, 0, @today), 0)
提前感谢大家!
Hopefully this will be an easy one to answer.
I am working on a table that requires MTD data. One of our SQL guys told me to use
MONTH (@monthtodate)= 11
Where @monthtodate
is set to GetDate()
in the parameter list in SQL Server Management Studio. So in "theory", he says, it should select the month (11) and then get today and return all the requested data in between those two dates. But I'm thinking this isn't correct.
In looking at my data I'm starting to think that It's just returning data for the whole month of November instead of just MTD. I guess, technically, anything that has 0 won't be calculated. However that just means it's poorly written code correct?
In your opinions, would this be the better way to return MTD data:
production_date <= @today and Production_Date >= DATEADD(mm, DATEDIFF(mm, 0, @today), 0)
Thanks in advance everyone!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我是这样做的。这应该适用于几乎任何版本的 SQL Server。
需要注意的一件重要事情是:从一开始,就应该始终建立一个代表“现在”(当前时刻)的单一值。如果您的查询中目前没有一致的值,那么当您的查询执行时,您最终会得到一些信息,以致它在运行时跨越日期边界。没有什么比向某人收取他们上个月已经支付的费用更好的了。最糟糕的是,像这样的边缘情况错误很难被开发人员或 QA 发现,因为两者都不太可能在 12 月 31 日 11:59 起作用。
代码:
Here's how I do it. This should work on pretty much any version of SQL Server.
One important thing to note: at the outset, one should always establish a single value that represents 'now', the current moment in time. If you do not have a consistent value for now in your query, you will eventually get bit when your query is executed such that it crosses a date boundary whilst in-flight. Nothing like billing somebody for something they already paid for last month. Worst, edge-case bugs like that are difficult to catch, either by developers or by QA, since neither is likely to be working, say, at 11:59 on December 31.
The code:
如果您询问从性能角度来看这两个查询中哪一个更好:
那么第一个就是,因为它会使用 Production_Date 上的索引(如果有)。但是,它们应该返回相同的结果。
If you are asking which of these 2 queries is better from a performance standpoint:
Then the first one would be, since it will use the index on Production_Date if there is one. However, they should both return the same results.