嗨,我有两张这样的表:
Tcustcounseldt
包含以下列:Counsel_Seq, Proc_Note, Proc_Date
Tcustcounselm
包含以下列:Counsel_Seq, Cust_No, Proc_Date
我有客户,我想要检索客户proc_note(退款明细)。有些客户有多个退款详情,我只想要最新的退款。
这是我的plsql代码,但是当我运行它时,它只给两次相同的cust_no,我只想看到最新的。
Select
A.Cust_No,
Max(To_Char(B.Proc_Date, 'yyyy/mm/dd hh24:mi:ss')) As Proc,
B.Proc_Note,
A.Counsel_Seq
FROM Tcustcounselm A,
Tcustcounseldt B
WHERE
A.Counsel_Seq = B.Counsel_Seq
--AND B.Do_Flag ='40'
AND A.Proc_Date BETWEEN TO_DATE('2013/07/08', 'YYYY/MM/DD') AND TO_DATE('2013/07/08', 'YYYY/MM/DD')+1
GROUP BY
A.Cust_No,
B.Proc_Note,
A.Counsel_Seq
ORDER BY 2 DESC;
我thınkMAX是问题,所以我尝试了不同的示例代码,但同样的问题
SELECT A.Cust_No,
B.Proc_Note
FROM Tcustcounselm A ,
(SELECT Counsel_Seq,
Proc_Note,
Rank () Over (Partition By Counsel_Seq Order By Proc_Date Desc) As Priority
From Tcustcounseldt
) B
Where A.Counsel_Seq = B.Counsel_Seq
--And B.Priority = 1
AND A.Proc_Date BETWEEN TO_DATE('2013/07/08', 'YYYY/MM/DD') AND TO_DATE('2013/07/08', 'YYYY/MM/DD')+1;
答案 0 :(得分:0)
这(或类似的东西)应该有效
SELECT A.Cust_No,
To_Char(B.Proc_Date, 'yyyy/mm/dd hh24:mi:ss') As Proc,
B.Proc_Note,
A.Counsel_Seq
FROM Tcustcounselm B
INNER JOIN Tcustcounseldt A
ON A.Counsel_Seq = B.Counsel_Seq
WHERE (A.Cust_No,B.Proc_Date) IN ( SELECT A.Cust_No,
max(B.Proc_Date) PD
FROM Tcustcounselm B
INNER JOIN Tcustcounseldt A
ON A.Counsel_Seq = B.Counsel_Seq
GROUP BY A.Cust_No)
我有时间玩sqlfiddle并修复了查询,尝试检查here。