对此查询我很累,请帮助我。
select max(cr.`CARD_RECHARGE_ID`),cp.`FULL_NAME` as userName , cr.`CHARGE_DATE` as lastRechargeDate,cr.`CUSTOMER_ID`
from nettxs_communication.customerprofile cp
LEFT JOIN nettxs_communication.cardrecharge cr ON cr.`CUSTOMER_ID` = cp.`CUSTOMER_ID`
group by cr.`CUSTOMER_ID` ORDER BY cr.`CHARGE_DATE`;
答案 0 :(得分:0)
据我的理解,您想从cardrecharge
中为每个CUSTOMER_ID
选择一个与greatest-n-per-group相关的最新条目
select cp.CUSTOMER_ID,
cp.FULL_NAME,
cr.CHARGE_DATE as lastRechargeDate,
cr.CARD_RECHARGE_ID
from customerprofile cp
left join cardrecharge cr ON cr.CUSTOMER_ID = cp.CUSTOMER_ID
join (
select CUSTOMER_ID, max(CHARGE_DATE) CHARGE_DATE
from cardrecharge
group by CUSTOMER_ID
) cr1 on cr.CUSTOMER_ID = cr1.CUSTOMER_ID and cr.CHARGE_DATE = cr1.CHARGE_DATE