title: Get data from the last day of the month without the use of loops or variables
body: <p>I wrote a query that should select the last record of each month in a year. I'd like to create a View based on this select, that I could run later in my project, but unfortunately I can't use any while loops or variables in a view command. Is there a way to select all these records - last days of a month in a View that I can use later?</p>
  <p>My desired effect of the view:</p>
  <p><a href="https://i.stack.imgur.com/HjuCn.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/HjuCn.png" alt="enter image description here" /></a></p>
  <p>The query that I'm trying to implement in a view:</p>
  <pre><code>DECLARE @var_day01 DATETIME;
  DECLARE @month int;
  SET @month = 1;
  DROP TABLE IF EXISTS #TempTable2;
  CREATE TABLE #TempTable2 (ID int, date datetime, INP2D float, INP3D float, ID_device varchar(max));
  WHILE @month &lt; 13
  BEGIN
  SELECT @var_day01 = CONVERT(nvarchar, date)  FROM (SELECT TOP 1 * FROM data
  WHERE DATEPART(MINUTE, CONVERT(nvarchar, date)) = '59'
  AND
  MONTH(CONVERT(nvarchar, date)) = (CONVERT(nvarchar, @month))
  ORDER BY date DESC
   ) results
  ORDER BY date DESC;
  
  INSERT INTO #TempTable2 (ID, date, INP2D,INP3D,ID_device) 
  SELECT * FROM data
  WHERE DATEPART(MINUTE, CONVERT(nvarchar, date)) = '59'
  AND
  MONTH(CONVERT(nvarchar, date)) = (CONVERT(nvarchar, @month))
  AND
  DAY(CONVERT(nvarchar, date)) = CONVERT(datetime, DATEPART(DAY, @var_day01))
  ORDER BY date DESC
  PRINT @var_day01
  SET @month = @month +1;
  END
  
  
  SELECT * FROM #TempTable2;
  </code></pre>