select m2.id id1, m1.id idx, m1.payload->>'community' Gemeinde, m1.item Projekt, 
  m1.payload->>'projectState' Status, m1.payload->>'active' Aktiv,
  to_char(m2.tstamp, 'DD.MM.YYYY') Datum1,
  to_char(m1.tstamp, 'DD.MM.YYYY') Datumx
from messages_current m1, messages m2
where m1.domain='kiss' and m1.class='project' 
  and (m1.payload->>'projectState')::integer <> 20
  --and m1.payload->>'active' is not null  
  and m2.id = (select min(id) from messages m3
	where m3.domain = m1.domain and m3.action = m1.action
	  and m3.class = m1.class and m3.item = m1.item
	  and m3.payload->>'projectState' = m1.payload->>'projectState')
  order by m1.payload->>'community', m1.item 
-- \g projects_10_30_40.csv
