Ups, sorry habe nicht genau gelesen...
Neuer Vorschlag...
SQL-Code:
SELECT id, MIN(datum) datum, TO_DAYS(CURRENT_TIMESTAMP) - TO_DAYS(MIN(datum)) AS tage
FROM tabelle1 x
WHERE EXISTS (SELECT *
FROM tabelle1 y
WHERE EXISTS (SELECT *
FROM tabelle1 z
WHERE datum = (SELECT MAX(datum)
FROM tabelle1
WHERE id = z.id)
AND id = y.id
AND wert <> y.wert)
AND id = x.id
GROUP BY id
HAVING x.datum > MAX(datum))
GROUP BY id
Edit: Hier nochmal ein weiterer Vorschlag, für den Sonderfall, dass es keine Änderungen innerhalb einer ID gab...
SQL-Code:
SELECT id, MIN(datum) datum, TO_DAYS(CURRENT_TIMESTAMP) - TO_DAYS(MIN(datum)) AS tage
FROM tabelle1 x
WHERE EXISTS (SELECT *
FROM tabelle1 y
WHERE ( EXISTS (SELECT *
FROM tabelle1 z
WHERE datum = (SELECT MAX(datum)
FROM tabelle1
WHERE id = z.id)
AND id = y.id
AND wert <> y.wert)
OR NOT EXISTS (SELECT *
FROM tabelle1
WHERE id = y.id
AND wert <> y.wert))
AND id = x.id
GROUP BY id
HAVING x.datum > MAX(datum)
OR (SELECT COUNT(DISTINCT wert)
FROM tabelle1
WHERE id = y.id) = 1)
GROUP BY id