I'm trying to search and I can not find the right logic to filter the results I want.
I have a table with all the due dates for certain equipment. My problem is related here:
- Equipments (1, 22, 23) are from the same group, so if one of them appears OK , I do not want you to list other equipment in the group. If it appears all expired, I want you to list the 3 equipments as expired.
So far I have searched the values in the database and created a select to find the data I need, but I can not filter the way I reported it. Follow script:
SELECT
y.* FROM
(SELECT
e.numcad,
e.codepi,
Max(e.datent) AS Entrega,
Max(e.datent) + p.diaval AS Validade,
CASE
WHEN
Max(e.datent) + p.diaval <= Getdate()
THEN
'VENCIDO'
ELSE
'OK'
END
AS Situacao
FROM
r096die e, r096epi p
WHERE
e.numcad = 241
AND e.codepi IN (1, 22, 23, 6)
AND p.codepi = e.codepi
GROUP BY
e.codepi , e.numcad , p.diaval
) Y
GROUP BY y.numcad, y.codepi, y.entrega, y.validade, y.situacao
NOTE: I had to post as an image because I could not add a results table