It easy and efficient if you use a table of numbers. Here is a good article describing how to generate it.
For this example I populate it on the fly, but in production I have a permanent table with 100K rows. It is useful in many reports.
DECLARE @Numbers TABLE (Number int PRIMARY KEY);
INSERT INTO @Numbers (Number)
SELECT TOP(100)
ROW_NUMBER() OVER(ORDER BY sys.all_objects.object_id) AS Number
FROM sys.all_objects;
Now for each session we return all days of the month using CROSS APPLY
. This solution returns all days of the month on or before the startDate
. If your startDate
is the last day of the month (as you said it is), then there will be a row for each day of the month.
SQLFiddle
DECLARE @SessionsPerArea TABLE (idSession INT, startDate DATE)
INSERT @SessionsPerArea VALUES
(1,'2013-01-31'),
(2,'2013-02-28'),
(3,'2013-03-31')
SELECT *
FROM
@SessionsPerArea AS S
CROSS APPLY
(
SELECT DATEADD(day, 1-N.Number, S.startDate) AS NewDate
FROM @Numbers AS N
WHERE N.Number <= DAY(S.startDate)
) AS CA
ORDER BY S.idSession, NewDate;
Result set
idSession startDate NewDate
1 2013-01-31 2013-01-01
1 2013-01-31 2013-01-02
1 2013-01-31 2013-01-03
1 2013-01-31 2013-01-04
1 2013-01-31 2013-01-05
1 2013-01-31 2013-01-06
1 2013-01-31 2013-01-07
1 2013-01-31 2013-01-08
1 2013-01-31 2013-01-09
1 2013-01-31 2013-01-10
1 2013-01-31 2013-01-11
1 2013-01-31 2013-01-12
1 2013-01-31 2013-01-13
1 2013-01-31 2013-01-14
1 2013-01-31 2013-01-15
1 2013-01-31 2013-01-16
1 2013-01-31 2013-01-17
1 2013-01-31 2013-01-18
1 2013-01-31 2013-01-19
1 2013-01-31 2013-01-20
1 2013-01-31 2013-01-21
1 2013-01-31 2013-01-22
1 2013-01-31 2013-01-23
1 2013-01-31 2013-01-24
1 2013-01-31 2013-01-25
1 2013-01-31 2013-01-26
1 2013-01-31 2013-01-27
1 2013-01-31 2013-01-28
1 2013-01-31 2013-01-29
1 2013-01-31 2013-01-30
1 2013-01-31 2013-01-31
2 2013-02-28 2013-02-01
2 2013-02-28 2013-02-02
2 2013-02-28 2013-02-03
2 2013-02-28 2013-02-04
2 2013-02-28 2013-02-05
2 2013-02-28 2013-02-06
2 2013-02-28 2013-02-07
2 2013-02-28 2013-02-08
2 2013-02-28 2013-02-09
2 2013-02-28 2013-02-10
2 2013-02-28 2013-02-11
2 2013-02-28 2013-02-12
2 2013-02-28 2013-02-13
2 2013-02-28 2013-02-14
2 2013-02-28 2013-02-15
2 2013-02-28 2013-02-16
2 2013-02-28 2013-02-17
2 2013-02-28 2013-02-18
2 2013-02-28 2013-02-19
2 2013-02-28 2013-02-20
2 2013-02-28 2013-02-21
2 2013-02-28 2013-02-22
2 2013-02-28 2013-02-23
2 2013-02-28 2013-02-24
2 2013-02-28 2013-02-25
2 2013-02-28 2013-02-26
2 2013-02-28 2013-02-27
2 2013-02-28 2013-02-28
3 2013-03-31 2013-03-01
3 2013-03-31 2013-03-02
3 2013-03-31 2013-03-03
3 2013-03-31 2013-03-04
3 2013-03-31 2013-03-05
3 2013-03-31 2013-03-06
3 2013-03-31 2013-03-07
3 2013-03-31 2013-03-08
3 2013-03-31 2013-03-09
3 2013-03-31 2013-03-10
3 2013-03-31 2013-03-11
3 2013-03-31 2013-03-12
3 2013-03-31 2013-03-13
3 2013-03-31 2013-03-14
3 2013-03-31 2013-03-15
3 2013-03-31 2013-03-16
3 2013-03-31 2013-03-17
3 2013-03-31 2013-03-18
3 2013-03-31 2013-03-19
3 2013-03-31 2013-03-20
3 2013-03-31 2013-03-21
3 2013-03-31 2013-03-22
3 2013-03-31 2013-03-23
3 2013-03-31 2013-03-24
3 2013-03-31 2013-03-25
3 2013-03-31 2013-03-26
3 2013-03-31 2013-03-27
3 2013-03-31 2013-03-28
3 2013-03-31 2013-03-29
3 2013-03-31 2013-03-30
3 2013-03-31 2013-03-31