0

嗨,我有两张这样的桌子:

  • 第一:Tcustcounselm
  • 第二:Tcustcounseldt

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;

我认为 MAX 是问题所以我尝试了不同的示例代码但同样的问题

               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;
4

1 回答 1

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 并修复了查询,请尝试在此处检查。

于 2013-10-10T13:09:14.830 回答