I have the following results
Date | EmployeeID
2015-11-18 | 1
2015-11-18 | 1
2015-11-18 | 1
2015-11-19 | 1
2015-11-19 | 1
2015-11-20 | 1
2015-11-20 | 1
2015-11-20 | 1
2015-11-25 | 1
But given a range of dates (2015-11-15 - 2015-11-30) I want to display something like this
Date | NbEmployees
2015-11-15 | 0
2015-11-16 | 0
2015-11-17 | 0
2015-11-18 | 3
2015-11-19 | 2
2015-11-20 | 3
2015-11-21 | 0
2015-11-22 | 0
2015-11-23 | 0
2015-11-24 | 0
2015-11-25 | 1
2015-11-26 | 0
2015-11-27 | 0
2015-11-28 | 0
2015-11-29 | 0
2015-11-30 | 0
I've using this approach by I only get the values from the table with data
DECLARE @StartDate DATE = '2015-11-15 00:00:00', @EndDate DATE = '2015-11-30 23:59:00'
DECLARE @CurrentDate DATE = @StartDate
DECLARE @DateRange TABLE (CurrentDate DATETIME)
WHILE(@CurrentDate <= @EndDate)
BEGIN
INSERT INTO @DateRange VALUES(@CurrentDate)
SET @CurrentDate = DATEADD(DAY, 1, @CurrentDate)
END
SELECT r.CurrentDate, COUNT(EmployeeID)
FROM Employee e
RIGHT JOIN @DateRange r ON e.HireDate = r.Date
Results:
Date | NbEmployees
2015-11-18 | 3
2015-11-19 | 2
2015-11-20 | 3
2015-11-25 | 1