【问题标题】:SQL Server how to get Count of a field for the current Quarter from Date?SQL Server如何从日期获取当前季度的字段计数?
【发布时间】:2013-07-11 06:36:04
【问题描述】:

我很难组合 SQL 语句。场景/要求是这样的:我需要获取当前季度的字段计数。

SELECT COUNT(fieldname) AS Hits 
FROM tablename 
WHERE DatePosted = (Current Quarter)

我想获得当前季度的点击次数/计数。

这可能吗?

【问题讨论】:

  • 当前季度如何定义?一年四分之二吗?

标签: sql-server tsql date


【解决方案1】:
select count(fieldname) as Hits 
from tablename
where datepart(q,DatePosted) = datepart(q,getdate())
and year(DatePosted) = year(getdate())

【讨论】:

  • +1 用于使用内置选项。很多时候,我们都在“重新发明轮子”。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-06
  • 2016-05-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多