Skip to content
Advertisement

Set based query to replace loop to populate all month end dates from given date for all records

I have a table that stores patient lab test results. There can be results from multiple tests like Albumin, Potassium, Phosphorus etc. First reading for each patient from each of these categories is stored in a table called #MetricFirstGroupReading.

Now, I need to populate all month end dates upto current month into a new table, for each record from the #MetricFirstGroupReading table. Following is the expected result when the query run on December 2018.

enter image description here

I know how to do it using WHILE loops. How to do this without loops, using set based SQL queries, in SQL Server 2016?

Advertisement

Answer

Following worked. This is an expansion of the idea present in tsql: How to retrieve the last date of each month between given date range

Query

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