简体   繁体   中英

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

I have a table:

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

I want to generate a second table which indicates, for every day of the year, the number of rows which include that calendar date within the period between the start and end dates, something like this:

WorkDate        NumActive

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

Currently I am trying:

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

This is giving me zeros for every day of the year.

Any help is appreciated - I'm self-taught via StackOverflow and I'm a bit over my head here.

This will give you a count of all the distinct StartDate's that are in the table. Copy it to do the same for EndDate. Since StartDate and EndDate can be different, I am not sure what your real intention is for counting a date range ? Does that mean it would be a +1 for every day in the range ? Clarify please.

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

This will give you the result. Assume #cal as 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

If you want to test, use below queries:

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')

Result

+-------------------------+-----------+
|        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 |
+-------------------------+-----------+

Please "Mark as Answer" if a post has answered the question

Your where clause having wrong condition. Please see my inline comments

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

try this,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM