计算在日期范围内出现的次数

时间:2017-04-19 08:44:35

标签: sql sql-server sql-server-2008 datetime

我想不出一个好的方法来说明这个问题,如果已经被问到这个问题就可以正常搜索。

我正在寻找SQL 2008 R2中的一种方法来计算两个日期时间值之间的下午6点的次数。

例如'2017-04-17 19:00:00''2017-04-19 17:00:00' 6 pm之间只发生一次虽然时间跨越了3个不同的日子。

'2017-04-17 18:00:00''2017-04-19 18:00:00'之间,它会发生3次,同时也会跨越3天。

这是一个非常愚蠢的表达我想要的插图。

timecount(hh, 6, min(datefield), max(datefield))

谢谢

7 个答案:

答案 0 :(得分:1)

这将给你每小时和出现次数:

select datepart(hh, DateColumn) as TheHours, count(*) as occurs
from MyTable
where DateColumn between @SomeDate and @SomeOtherDate
group by datepart(hh, DateColumn)

或者只是下午6点:

select count(*)
from MyTable
where datepart(hh, DateColumn) = 18
and DateColumn between @SomeDate and @SomeOtherDate

答案 1 :(得分:1)

DECLARE 
    @Time time = '18:00',
    @From datetime = '2017-04-17 18:00:00',
    @To datetime = '2017-04-19 18:00:00'


SELECT 
    CASE 
        -- Same date
        WHEN DATEDIFF(DAY, @From, @To) = 0 THEN 
            CASE WHEN CAST(CAST(@From AS date) AS datetime) + @Time BETWEEN @From AND @To THEN 1 ELSE 0 END
        -- Not same date
        WHEN @From <= @To THEN
            CASE WHEN @Time >= CAST(@From AS time) THEN 1 ELSE 0 END 
            + DATEDIFF(DAY, @From, @To) - 1
            + CASE WHEN @Time <= CAST(@To AS time) THEN 1 ELSE 0 END 
        -- Invalid range
        ELSE 0
    END AS CountOfTime

答案 2 :(得分:1)

要计算的简单查询:

DECLARE @StartDate datetime = '2017-04-17 18:00:00'

DECLARE @EndDate datetime = '2017-04-19 18:00:00'

SELECT 
   CASE 
      WHEN CAST(@StartDate AS time) <= '18:00' AND CAST(@EndDate AS time) >= '18:00' 
             THEN datediff(day, @StartDate, @EndDate) + 1
      WHEN CAST(@StartDate AS time) <= '18:00' AND CAST(@EndDate AS time) < '18:00' 
             THEN datediff(day, @StartDate, @EndDate)      
      WHEN CAST(@StartDate AS time) > '18:00' AND CAST(@EndDate AS time) >= '18:00' 
             THEN datediff(day, @StartDate, @EndDate)
      ELSE datediff(day, @StartDate, @EndDate) - 1
   END AS TotalCount

答案 3 :(得分:0)

尝试使用以下公式我尝试过不同的方案并且它可以正常工作,如果我错过任何方案而不按照您的要求工作,请告诉我。

DECLARE @firstDate Datetime='17-Apr-2017 17:00:00'
DECLARE @secondDate Datetime='17-Apr-2017 18:59:00'


SELECT
    CASE WHEN DATEDIFF(day,@firstDate,@secondDate)=0
    THEN IIF(CAST(@firstDate AS TIME) <='18:00' AND DATEPART(hh,@secondDate) >=18,1,0)
    ELSE 
        CASE WHEN 
            (
                CAST(@firstDate AS TIME) <='18:00' AND CAST(@secondDate AS TIME) <'18:00' 
                OR
                CAST(@firstDate AS TIME) >'18:00' AND CAST(@secondDate AS TIME) >='18:00' 
            )
            THEN DATEDIFF(day,@firstDate,@secondDate)
            WHEN CAST(@firstDate AS TIME) <='18:00' AND CAST(@secondDate AS TIME) >='18:00' THEN DATEDIFF(day,@firstDate,@secondDate)+1 
            ELSE DATEDIFF(day,@firstDate,@secondDate)-1
            END
    END AS TotalCount

答案 4 :(得分:0)

使用CTE

尝试以下脚本
DECLARE @F_DATE     AS DATETIME =   '2017-04-17 19:00:00'
        ,@T_DATE    AS DATETIME =   '2017-04-19 17:00:00'


;WITH CTE
AS  (
    SELECT  (CASE WHEN DATEPART(HH,@F_DATE) <= 18
                THEN @F_DATE 
                ELSE (CASE WHEN  DATEDIFF(DAY,@F_DATE,@T_DATE) > 0 
                        THEN DATEADD(DAY,1,@F_DATE) END)
             END)   AS  CDATE

    UNION ALL

    SELECT  DATEADD(DAY,1,CDATE)
    FROM    CTE
    WHERE   DATEADD(DAY,1,CDATE)    <=  @T_DATE
)

SELECT COUNT(CDATE) DATE_COUNT 
FROM CTE
OPTION ( MAXRECURSION 0 )

答案 5 :(得分:0)

以下是两个日期时间之间每隔6点的计数:

DECLARE @StartDate datetime
DECLARE @EndDate datetime
set @StartDate = '2017-04-17 19:00:00'
set @EndDate = '2017-04-19 17:00:00'

;WITH cte1 (S) AS (
SELECT 1 FROM (VALUES (1), (1), (1), (1), (1), (1), (1), (1), (1), (1)) n (S)
),
cte2 (S) AS (SELECT 1 FROM cte1 AS cte1 CROSS JOIN cte1 AS cte2),
cte3 (S) AS (SELECT 1 FROM cte1 AS cte1 CROSS JOIN cte2 AS cte2)

select count(datepart(hour,result)) as count from
(SELECT TOP (DATEDIFF(hour, @StartDate, @EndDate) + 1)
        result = DATEADD(hour, ROW_NUMBER() OVER(ORDER BY S) - 1, @StartDate)
FROM cte3) as res where datepart(hour,result) = 18

以下是两个日期时间之间的下午6点的详细视图:

DECLARE @StartDate datetime
DECLARE @EndDate datetime
set @StartDate = '2017-04-17 19:00:00'
set @EndDate = '2017-04-19 17:00:00'

;WITH cte1 (S) AS (
SELECT 1 FROM (VALUES (1), (1), (1), (1), (1), (1), (1), (1), (1), (1)) n (S)
),
cte2 (S) AS (SELECT 1 FROM cte1 AS cte1 CROSS JOIN cte1 AS cte2),
cte3 (S) AS (SELECT 1 FROM cte1 AS cte1 CROSS JOIN cte2 AS cte2)

select result,datepart(hour,result)  from
(SELECT TOP (DATEDIFF(hour, @StartDate, @EndDate) + 1)
        result = DATEADD(hour, ROW_NUMBER() OVER(ORDER BY S) - 1, @StartDate)
FROM cte3) as res where datepart(hour,result) = 18

答案 6 :(得分:0)

此处提供任何日期范围之间的计数

declare @time datetime='06:00:00'

declare @startDate datetime='04/20/2017 05:00:00'

declare @enddate datetime='04/21/2017 05:00:00'

SELECT 
  case 
    WHEN datediff(ss,@time, convert(time(0),@startDate)) <=0  and   datediff(ss,@time, convert(time(0),@enddate)) >=0

     THEN datediff(dd,@startDate,@enddate) +1 
WHEN   (datediff(ss,@time, convert(time(0),@startDate)) <=0  and 
datediff(ss,@time, convert(time(0),@enddate)) <=0

        OR
        datediff(ss,@time, convert(time(0),@startDate))> 0  and 

datediff(ss,@time, convert(time(0),@enddate)) >0

        OR

        datediff(ss,@time, convert(time(0),@startDate))> 0  and datediff(ss,@time, convert(time(0),@enddate)) <=0
        )
then  datediff(dd,@startDate,@enddate) 
ELSE  
datediff(dd,@startDate,@enddate)-1
END