簡體   English   中英

SQL Server:如何計算給定日期范圍內的行數?

[英]SQL Server : How to count rows within a given date range?

我有一張桌子:

ID     StartDate       EndDate

1      2016-01-01      2016-01-03
2      2016-01-01      2016-01-01
3      2016-01-01      2016-01-01
4      2016-01-02      2016-01-02

我想生成第二個表,該表指示一年中的每一天,在開始日期和結束日期之間的時間段內包含該日歷日期的行數,如下所示:

WorkDate        NumActive

2016-01-01      3
2016-01-02      2
2016-01-03      1

目前我正在嘗試:

SELECT      COUNT(ID)
FROM        TableOne
WHERE       StartDate   >=  WorkDate
        and EndDate     <=  WorkDate

這給了我一年中每一天的零。

感謝任何幫助 - 我是通過 StackOverflow 自學的,在這里我有點不知所措。

這將為您提供表中所有不同 StartDate 的計數。 復制它以對 EndDate 執行相同的操作。 由於 StartDate 和 EndDate 可能不同,我不確定您計算日期范圍的真正意圖是什么? 這是否意味着該范圍內的每一天都是 +1? 請澄清。

Select
    StartDate
    ,Count(*) over (partition by StartDate) as 'cntStartDates'
From TableOne
Group By StartDate

這會給你結果。 假設 #cal 作為 TableOne

;with tal as (
select distinct number from master.dbo.spt_values 
where number between 1 and 365
)
, c2 as (
select DATEADD(d, number - 1, '2016-01-01') dt
from tal
)
select dt AS WorkDate, COUNT(*) AS NumActive
from c2 inner join #cal on c2.dt between #cal.StartDate and #cal.EndDate
group by dt
order by 1

如果要測試,請使用以下查詢:

create table #cal (id int, StartDate date, EndDate date)

insert into #cal values 
(1, '2016-01-01', '2016-01-03'),
(2, '2016-01-01', '2016-01-01'),
(3, '2016-01-01', '2016-01-01'),
(4, '2016-01-02', '2016-01-02')

結果

+-------------------------+-----------+
|        WorkDate         | NumActive |
+-------------------------+-----------+
| 2016-01-01 00:00:00.000 |         3 |
| 2016-01-02 00:00:00.000 |         2 |
| 2016-01-03 00:00:00.000 |         1 |
+-------------------------+-----------+

如果帖子已經回答了問題,請“標記為答案”

您的 where 子句條件錯誤。 請看我的內嵌評論

CREATE TABLE #TempWorkDate (Id INT, StartDate DATETIME, EndDate DATETIME)
GO

INSERT INTO #TempWorkDate
SELECT 1, DATEADD(D, +1, GETDATE()), DATEADD(D, +4, GETDATE()) UNION ALL
SELECT 1, DATEADD(D, +1, GETDATE()), DATEADD(D, +1, GETDATE()) UNION ALL
SELECT 1, DATEADD(D, +1, GETDATE()), DATEADD(D, +1, GETDATE()) UNION ALL
SELECT 1, DATEADD(D, +2, GETDATE()), DATEADD(D, +2, GETDATE()) 
GO

-- This query won't work, because the condition is wrong
SELECT * FROM #TempWorkDate
WHERE StartDate >= '2016-05-02' AND EndDate <= '2016-05-02'

-- The below query returns the result
SELECT * FROM #TempWorkDate
WHERE StartDate <= '2016-05-02' AND EndDate >= '2016-05-02'

DROP TABLE #TempWorkDate

嘗試這個,

declare @cal table (id int, StartDate date, EndDate date)

insert into @cal values 
(1, '2016-01-01', '2016-01-03'),
(2, '2016-01-01', '2016-01-01'),
(3, '2016-01-01', '2016-01-01'),
(4, '2016-01-02', '2016-01-02')

;with t(id,dts) as (select id,startDate from @cal union all 
select t.id,dateadd(day,1,dts) from t join @cal t1 on t.id=t1.id where t.dts<t1.EndDate)

select dts WorkDate,count(dts) NumActive from t group by dts

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM