This is the script and data
CREATE TABLE TB_TEST (
ManDate date,
Amount float
)
INSERT INTO TB_Test Values ('2023-Mar-14', 13.0)
INSERT INTO TB_Test Values ('2023-Mar-13', 13.5)
INSERT INTO TB_Test Values ('2023-Mar-10', 12.8)
INSERT INTO TB_Test Values ('2023-Mar-8', 14.6)
And the query returns data
SELECT Mandate, Amount
FROM TB_TEST
However, if my client does not make any products on some dates, they want to show the no production dates with Amount = 0 like rows in yellow as below:
This means that the Mandate column shows everyday until the current date.
Thank you in advance
1条答案
按热度按时间eyh26e7m1#
If you have a date range for the query, then you could use a CTE to generate the dates that will have no products. If I modify an example I've found here - https://www.mssqltips.com/sqlservertip/4054/creating-a-date-dimension-or-calendar-table-in-sql-server/ - the solution would look like this:
This query would select all the dates based on the range you provided, and then join them to your table that has your actual dates/amounts in it. For any row that has an amount, it takes the amount from your table - for any other row it sets the Amount to be 0 (see the case statement).