I have a table as below:

```none
Name  Start Date  End Date
Joe   20/04/2021  20/05/2021
John  01/05/2021  28/05/2021

```

I am using a SQL table-valued function to return a table that has 2 columns: Month and Count total Date of this month.

Example:

* Joe: 10 days in Apr, 20 days in May
* John: 28 days in May

Finally I will return a new table

| Month | Count |
| --- | --- |
| 4 | 10 |
| 5 | 48 |

I tried to use datediff and datepart to group by month, but don't know how to sum after group. Is there any way to do this?

Besides, I want to add filter from date and to date.