I'm working with a table called du_vertrag
, and I'm trying to build a query that selects distinct values for id
and status
pairs.
Input Table:
id | status | date |
---|---|---|
6251899 | beantragt | 20201008 |
6377042 | beantragt | 20201008 |
6387891 | beantragt | 20201008 |
6251899 | übergabe | 20201009 |
6377042 | übergabe | 20201009 |
6387891 | übergabe | 20201009 |
6251899 | übergabe | 20201010 |
6377042 | übergabe | 20201010 |
6387891 | übergabe | 20201010 |
6251899 | aktiv | 20201024 |
6377042 | aktiv | 20201024 |
6387891 | aktiv | 20201024 |
Desired Output:
id | status | date |
---|---|---|
6251899 | beantragt | 20201008 |
6377042 | beantragt | 20201008 |
6387891 | beantragt | 20201008 |
6251899 | übergabe | 20201009 |
6377042 | übergabe | 20201009 |
6387891 | übergabe | 20201009 |
6251899 | aktiv | 20201024 |
6377042 | aktiv | 20201024 |
6387891 | aktiv | 20201024 |
I need for every id the new status with the first datetime the status changed.
If you need distinct values for each "id" and "status", you can aggregate over those two fields and select the minimum date to get your output:
SELECT id,
status,
MIN(date) AS date
FROM du_vertrag
GROUP BY id,
status
If you can share what your DBMS is, I can link a fiddle where you can test this solution.