我在这里开始提问(Query to get the most recent record and with the higher value),但我有一个小的变更请求。
我将有一个包含Percentage的列和另一个包含Char的列,以指示它是值还是百分比。
我正在尝试做这样的事情:
select card,
service,
max(date),
case when type = 'v'
then
MAX(value) KEEP (
dense_rank first order by date desc
)
else
max(percentage) valor keep (
dense_rank first order by date desc
) end
from table
group by card,
service;
但我得到了ORA-00979:不是GROUP BY表达式。
答案 0 :(得分:0)
您尚未提及列type
是否因给定card,service
对而异。假设它是相同的,您应该能够使用嵌套的select
获得结果,包括内部type
和select
中的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
);