我想从表user_payments中只选择最新的membership_id
user_id等于1的用户.
user_id等于1的用户.
这就是表user_payment的样子:
PAYM_ID USER_ID MEMBSHIP_ID PAYM_DATE ---------- ---------- ----------- ------------------------------- 1 1 1 18-DEC-09 12.00.00.000000000 AM 2 1 2 18-DEC-10 12.00.00.000000000 AM 3 1 2 18-DEC-11 12.00.00.000000000 AM 4 2 3 17-MAR-11 12.00.00.000000000 AM 5 3 3 18-JUN-12 12.00.00.000000000 AM 6 4 2 17-FEB-12 12.00.00.000000000 AM 7 5 2 18-FEB-11 12.00.00.000000000 AM 8 5 2 18-FEB-12 12.00.00.000000000 AM 9 6 1 01-JUN-12 12.00.00.000000000 AM 10 7 1 03-FEB-11 12.00.00.000000000 AM 11 7 2 03-FEB-12 12.00.00.000000000 AM
我尝试以下代码没有成功:
SELECT MEMBSHIP_ID FROM user_payment WHERE user_id=1 and MAX(paym_date);
我收到这个错误:
sql错误:ORA-00934:此处不允许组功能
00934. 00000 – “此处不允许群组功能”
我该如何解决?提前致谢!
解决方法
select * from (SELECT MEMBSHIP_ID FROM user_payment WHERE user_id=1 order by paym_date desc) where rownum=1;