0
EmployeeID    RecordID         DateRecord
1               1         2/19/2013 12:00:00 AM
1               2         2/21/2013 12:00:00 AM
1               3         2/23/2013 12:00:00 AM
1               4         2/27/2013 12:00:00 AM
1               5         3/3/2013 12:00:00 AM
2               11        3/10/2013 12:00:00 AM
2               12        3/14/2013 12:00:00 AM
1               14        3/16/2013 12:00:00 AM

如何计算天数?

2013 年 2 月的示例,其中“19、21、23、27”应计为“4”天.. ??

我找到了这个方法..

SELECT DATEPART(yy, Daterecord),
   DATEPART(mm, Daterecord),
   DATEPART(dd, Daterecord),
   COUNT(*)

FROM Records

GROUP BY DATEPART(yy, Daterecord),
     DATEPART(mm, Daterecord),
     DATEPART(dd, Daterecord)

并导致..

2013    2   19  1    
2013    2   21  1    
2013    2   23  1    
2013    2   27  1    
2013    3   3   1    
2013    3   10  1    
2013    3   14  1    
2013    3   16  1    

它只是得到具体的日期,但没有计算每个月的总天数..帮助我..请

4

6 回答 6

1

我改了几个名字,你不会介意的

WITH Emp_CTE AS (

    SELECT EmployeeID ,DATEPART(yy, Daterecord) AS years,
       DATEPART(mm, Daterecord) AS months
      -- DATEPART(dd, Daterecord) AS days


    FROM testTrial
    )
    SELECT COUNT(months) AS noOfMonths ,* FROM Emp_CTE GROUP BY  months,EmployeeID,years

SqlFiddle

于 2013-10-04T13:36:18.133 回答
0

try this...

    declare @date2 nvarchar(max)
    set @date2 = (select getdate())
    select DateDiff(Day,@date2,DateAdd(month,1,@date2))
于 2014-01-09T07:29:26.230 回答
0

您的初始查询几乎是正确的,只需要从分组中删除 DATEPART(dd, Daterecord) 就可以了。添加 HAVING 子句以查找 2 月份的记录:

SELECT
    DATEPART(yy, Daterecord),
    DATEPART(mm, Daterecord),   
    COUNT(1)
FROM
    Records
GROUP BY
    DATEPART(yy, Daterecord),
    DATEPART(mm, Daterecord)
HAVING
    DATEPART(yy, eCreationTime) = 2013
AND DATEPART(mm, Daterecord) = 2
于 2013-10-04T13:46:19.950 回答
0

让你试试这个: -

1:找出我们当前所在月份的天数

DECLARE @dt datetime
SET     @dt = getdate()

SELECT @dt AS [DateTime],
   DAY(DATEADD(mm, DATEDIFF(mm, -1, @dt), -1)) AS [Days in Month]Solution 

2:查找给定月-年组合中的天数

DECLARE @y int, @m int
SET     @y = 2012
SET     @m = 2

SELECT @y AS [Year],
   @m AS [Month],
   DATEDIFF(DAY,
            DATEADD(DAY, 0, DATEADD(m, ((@y - 1900) * 12) + @m - 1, 0)),
            DATEADD(DAY, 0, DATEADD(m, ((@y - 1900) * 12) + @m, 0))
       ) AS [Days in Month]
于 2013-10-04T12:18:53.017 回答
0

建议的代码中没有“年月”??

试试这个

select
           datename(month,daterecord) as [Month]
         , year(DateRecord)           as [Year]
         , count(distinct DateRecord ) as day_count
         , count(distinct dateadd(day, datediff(day,0, DateRecord ), 0)) as daytime_count
from your_table
where ( DateRecord >= '20130201' and DateRecord < '20130301' )
group by
           datename(month,daterecord)
         , year(DateRecord)

请注意,仅当字段 [DateRecord] 的时间超过上午 12:00 时,才需要列 [daytime_count](即,它“修剪”时间,因此您在上午 12:00 处理日期)

关于日期范围选择:许多人会觉得使用“介于”是解决方案,但事实并非如此,最安全最可靠的方法如上所示。请注意,较高的日期是 3 月 1 日,但我们要求的信息少于 3 月 1 日,因此我们不必担心闰年,也不必担心小时和分钟。

请参阅:BETWEEN 和魔鬼有什么共同点?

于 2013-10-04T12:27:07.883 回答
0

如果您的表被称为 Employee 那么这将起到作用:

select convert(varchar, DateRecord, 112)/ 100, count(*)
  from Employee
 group by convert(varchar, DateRecord, 112)/ 100
于 2013-10-04T12:29:31.757 回答