按日期时间分组忽略时间部分
是否可以编写一个按 datetime
数据类型分组但忽略时间部分(例如小时和分钟)的 Microsoft SQL 查询?
Is it possible to write a Microsoft SQL query that will group by a datetime
data-type but ignoring the time part such as the hour and minute?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用的是 SQL Server 2008,这很简单。
对于以前的版本,您可以使用
但是,这些都无法利用这样一个事实:按
datetime
排序的YourCol
上的索引也将按date 排序
因此使用流聚合而不进行排序操作。在 SQL Server 2008+ 上,您可能会考虑索引
(date,time)
而不是datetime
来促进此类查询。要么简单地将其存储为两个单独的组件,并可能提供一个重新组合各部分的计算列(
datetime2
的存储方式与date
和time< /code> 因此,除非附加列将
NULL_BITMAP
推送到新字节,否则这不会消耗更多空间。)。或者,您可以将其合并存储在基表中,并让索引使用将其拆分的计算列。
此方法的示例
If you are on SQL Server 2008 this is simple.
For previous versions you can use
However neither of these will be able to leverage the fact that an index on
YourCol
that is ordered bydatetime
will also be ordered bydate
and thus use a stream aggregate without a sort operation.On SQL Server 2008+ you might consider indexing
(date,time)
rather thandatetime
to facilitate this type of query.Either by simply storing it as two separate components and possibly providing a calculated column that recombines the parts (a
datetime2
is stored the same as adate
and atime
so this won't consume any more space except for if the additional column pushes theNULL_BITMAP
onto a new byte.).Or alternatively you could store it combined in the base table and have the indexes use calculated columns that split it out.
An example of this approach
如果您有一个大表并且希望分组快速发生,则不能依赖于表达式进行分组。
在表上创建一个额外的日期时间列,并用触发器填充它,该触发器根据您的“真实”日期计算基准日期:
然后在
EffectiveDate
上放置一个索引,分组(和搜索)将会很多快点。If you have a large table and want grouping to happen fast, you cannot rely grouping on an expression.
Create an additional datetime column on your table and have it filled by a trigger that calculates the base date from you "real" date:
Then put an index on
EffectiveDate
and grouping (and searching) will be a lot faster.