Skip to content
Advertisement

SQL query to select previous 14 months where add amount ‘0’ if any month is missing

I have create this query to select the date, amount for previous 14 months, In this case the month is missing if no record is present for that month, Please suggest me a solution to add zero and month if that paticular month is missing.

enter image description here

You can view from the results, That NOv 2018 is missing.

Advertisement

Answer

Create an ad-hoc table with select/union of the possible months, then left join your table from that, selecting only the records for the given month. For convenience, compute the date range from the number of months ago (but you could just use the more complicated expressions for start date and end date in the join instead).

This will report NULL as the renewal date for months with no records, and the minimum renewal date found for months that do have records. Not sure what exactly you want there.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement