title: Data value on a given date
body: <p>This time I have a table on a PostgreSQL database that contains the employee name, the date that he started working and the date that he leaves the company, in the cases of the employee still remains in the company, this field has null value.
  Knowing this, I would like to know how many people was working on a predetermined date, ex:</p>
  <p>I would like to know how many people works on the company in January 2021.</p>
  <p>I don't know where to start, in some attempts I got the number of hires and layoffs per month, but I need to show this accumulated value per month, in another column.</p>
  <p>I hope I made myself understood, I'll leave the last SQL I got here.</p>
  <pre><code>select reference, sum(hires) from
  (
  select 
      date_trunc('month', date_hires) as reference,
      count(*) as hires   
  from
      ponto_mais_relatorio_colaboradores
  group by 
      date_hires
  union all
  
  select  
      date_trunc('month', date_layoff) as reference,
      count(*)*-1 as layoffs
  from 
      ponto_mais_relatorio_colaboradores
  group by 
      date_layoff    
  ) as reference
  
  join calendar_aux on calendar_aux.ano_mes = reference
  
  group by reference
  order by reference
  </code></pre>