Normalmente, sugeriría tener una tabla de calendario estática que contenga una lista secuencial de fechas. Sin embargo, usando el enfoque inteligente de Cade Roux de generar una tabla de calendario, tendría algo como:
;With Calendar As
(
Select Cast(Floor(Cast(@StartDate As float)) As datetime) As [Date]
Union All
Select DateAdd(d, 1, [Date])
From Calendar
Where DateAdd(d, 1, [Date]) < @EndDate
)
Select C.[Date], R.Country, Sum(R.PeopleNeeded)
From Calendar As C
Left Join Requests As R
On C.[Date] Between R.[Start Date] And R.[End Date]
And ( @Country Is Null Or R.Country = @Country )
Group By C.[Date], R.Country
Option (MAXRECURSION 0);
Ahora, si es el caso de que desea filtrar por país de modo que los únicos días devueltos sean los del país dado que tienen datos, entonces simplemente necesitaría cambiar Left Join a Inner Join.
ADICIÓN
A partir de los comentarios, se solicitó mostrar a todos los países si tienen una Solicitud o no. Para hacerlo, debe unirse a la tabla de países:
With Calendar As
(
Select Cast(Floor(Cast(@StartDate As float)) As datetime) As [Date]
Union All
Select DateAdd(d, 1, [Date])
From Calendar
Where DateAdd(d, 1, [Date]) < @EndDate
)
Select C.[Date], C2.Country, Sum(R.PeopleNeeded)
From Calendar As C
Cross Join Countries As C2
Left Join Requests As R
On C.[Date] Between R.[Start Date] And R.[End Date]
And R.CountryId = C2.CountryId
Group By C.[Date], C2.Country
Option (MAXRECURSION 0);