我试图在同一个表之间运行内部联接。我的语法出了什么问题?
(SELECT user_key, bill_number, MAX(payment_date) AS payment_date
FROM billpayment) bpt
INNER JOIN (SELECT * FROM billpayment) bp
ON bp.user_key=bpt.user_key
AND bp.bill_number=bpt.bill_number
AND bp.payment_date=bpt.payment_date
GROUP BY user_key, bill_number;
它表示sql命令没有在' billpayment)的括号中正确结束'
答案 0 :(得分:2)
SELECT
user_key,
bill_number,
MAX(payment_date) AS payment_date
FROM billpayment bpt
INNER JOIN billpayment bp
ON bp.user_key=bpt.user_key
AND bp.bill_number=bpt.bill_number AND bp.payment_date=bpt.payment_date
GROUP BY
user_key, bill_number
;
答案 1 :(得分:1)
On Oracle试试这个:
SELECT *
FROM (SELECT user_key, bill_number, MAX (payment_date) AS payment_date FROM billpayment) bpt
INNER JOIN billpayment bp
ON bp.user_key = bpt.user_key AND bp.bill_number = bpt.bill_number AND bp.payment_date = bpt.payment_date
GROUP BY user_key, bill_number;
没有表结构就无法测试它。
答案 2 :(得分:0)
如何使用分析函数?
select bp.*,
max(bp.paymentdate) over (partition by user_key, bill_number) as max_payment_date
from billpayment
如果您想要最长付款日期的字段:
select bp.*
from (select bp.*,
max(bp.paymentdate) over (partition by user_key, bill_number) as max_paymentdate
from billpayment
) bp
where paymentdate = max_paymentdate;
答案 3 :(得分:0)
在mySQL上,您可以尝试类似:
SELECT bpt.user_key, bpt.bill_number, MAX(bpt.payment_date) AS payment_date
FROM billpayment bpt
INNER JOIN (SELECT * FROM billpayment) bp
ON bp.user_key=bpt.user_key
AND bp.bill_number=bpt.bill_number
AND bp.payment_date=bpt.payment_date
GROUP BY bpt.user_key, bpt.bill_number;