Du har ikke nævnt, om kolonnen type
varierer for et givet card,service
par. Forudsat at det er det samme, burde du være i stand til at få resultatet med en indlejret select
, inklusive type
i det indre select
og group by
.
SELECT card
,service
,CASE
WHEN type = 'v'
THEN value
ELSE perc
END AS max_result
FROM (
SELECT card
,service
,type
,MAX(date_t) AS Date_t
,MAX(value) KEEP (
DENSE_RANK FIRST ORDER BY date_t DESC
) AS value
,MAX(percentage) KEEP (
DENSE_RANK FIRST ORDER BY date_t DESC
) AS perc
FROM yourtable
GROUP BY card
,service
,type
);