答案 0 :(得分:1)
您也可以执行此操作而无需子查询:
WITH b(ID, "date",bal) AS
(
SELECT 'CUST_I',DATE '2013-07-27', 14777.44 FROM dual UNION ALL
SELECT 'CUST_H',DATE '2013-07-26', 71085.13 FROM dual UNION ALL
SELECT 'CUST_I',DATE '2013-08-27', 66431.35656 FROM dual UNION ALL
SELECT 'CUST_H',DATE '2013-08-26', 63102.68622 FROM dual UNION ALL
SELECT 'CUST_H',DATE '2013-08-20', 6310.68622 FROM dual UNION ALL
SELECT 'CUST_H',DATE '2013-08-10', 630.68622 FROM dual UNION ALL
SELECT 'CUST_G',DATE '2013-09-25', 89732.04889 FROM dual UNION ALL
SELECT 'CUST_E',DATE '2013-09-23', 83074.70822 FROM dual
)
SELECT ID,
MAX("date") KEEP (DENSE_RANK FIRST ORDER BY "date" desc) AS MAX_DATE,
MAX(bal) KEEP (DENSE_RANK FIRST ORDER BY "date" desc) AS MAX_BAL
FROM b
GROUP BY ID, TRUNC("date", 'MM');
+-----------------------------+
|ID |MAX_DATE |MAX_BAL |
+-----------------------------+
|CUST_E|23.09.2013|83074.70822|
|CUST_G|25.09.2013|89732.04889|
|CUST_H|26.07.2013|71085.13 |
|CUST_H|26.08.2013|63102.68622|
|CUST_I|27.07.2013|14777.44 |
|CUST_I|27.08.2013|66431.35656|
+-----------------------------+
答案 1 :(得分:0)
您可以加入分组数据:
SELECT *
FROM t
INNER JOIN (
SELECT ID, MAX(Date) AS Date_Last
FROM t
GROUP BY ID, EXTRACT(YEAR FROM Date), EXTRACT(MONTH FROM Date)
) AS x ON t.ID = x.ID AND t.Date = x.Date_Last
答案 2 :(得分:0)
您可以尝试使用窗口函数-row_number()
select * from
(
SELECT *,row_number() over(partition by extract(YEAR FROM Date), extract(MONTH FROM Date) order by date desc) as rn
FROM t
)rn=1
答案 3 :(得分:0)
您可以对表调用cust_balances
使用自联接:
select c1.*
from cust_balances c1
join
(
select max("date") max_date
from cust_balances
group by to_char("date",'yyyymm')
) c2 on ( c1."date" = c2.max_date );