I work on Azure SQL Database working with SQL Server
In SQL, I try to have a table by day, but the day is not in the table. I explain it by the example below:
TABLE STARTER: (Format Date: YYYY-MM-DD)
Date begin | Date End | Category | Value |
---|---|---|---|
2021-01-01 | 2021-01-03 | 1 | 0.2 |
2021-01-02 | 2021-01-03 | 1 | 0.1 |
2021-01-01 | 2021-01-02 | 2 | 0.3 |
For the result, I try to have this TABLE RESULT:
Date | Category | Value |
---|---|---|
2021-01-01 | 1 | 0.2 |
2021-01-01 | 2 | 0.3 |
2021-01-02 | 1 | 0.3 (0.2+0.1) |
2021-01-02 | 2 | 0.3 |
2021-01-03 | 1 | 0.3 (0.2+0.1) |
For each day, I want to sum the value if the day is between the beginning and the end of the date. I need to do that for each category.
In terms of SQL code I try to do something like that:
SELECT SUM(CAST(value as float)) OVER (PARTITION BY Date begin, Category) as value,
Date begin,
Category,
Value
FROM TABLE STARTER
This code calculates only the value that has the same Date begin but don't consider all date between Date begin and Date End. So in my code, it doesn't calculate the sum of the value for the 02-01-2021 of Category 1 because it doesn't write explicitly. (between 01-01-2021 and 03-01-2021)
Is it possible to do that in SQL?
Thanks so much for your help!