基于日期的Oracle SQL最新行

时间:2018-11-25 17:06:10

标签: sql oracle

我知道这个问题在论坛上已经被问过很多次了,我已经尝试了多种方法来获取始终需要一些错误消息的内容,因此我失去了尝试调试它的意愿。我正在用Oracle Fusion 13C编写SQL

我收到的消息是“太多值”,我很确定它来自我的最终where子句。没有最后一行的代码会带回两行正确的数据,我只想带回一行,但要基于哪一行具有“ ACTION_DATE”列中的最新日期,或者,如果失败则是“ SEQUENCE_NUM”中的最高数字柱。

到目前为止,我得到的代码是:

WITH
PERSON_DETAILS AS (SELECT DISTINCT
                    PERSON_ID AS PPNPERSONID,
                    FULL_NAME
                FROM
                    PER_PERSON_NAMES_F
                WHERE
                    TO_DATE(EFFECTIVE_END_DATE, 'yyyy/mm/dd') > TO_DATE(SYSDATE, 'yyyy/mm/dd')
                    AND NAME_TYPE = 'GLOBAL'

),

PERSON_ASSIGNMENT AS (SELECT DISTINCT
                        PD.*,
                        PAAM.PERSON_ID AS PAAMPERSONID,
                        PAAM.JOB_ID AS PAAMJOBID
                    FROM
                        PERSON_DETAILS PD,
                        PER_ALL_ASSIGNMENTS_M PAAM
                    WHERE
                        PD.PPNPERSONID = PAAM.PERSON_ID
                        AND TO_DATE(PAAM.EFFECTIVE_END_DATE, 'yyyy/mm/dd') > TO_DATE(SYSDATE, 'yyyy/mm/dd')

),

PERSON_JOB AS (SELECT DISTINCT
                PA.*,
                PJF.JOB_ID AS PJFJOBID,
                PJF.APPROVAL_AUTHORITY
            FROM
                PERSON_ASSIGNMENT PA,
                PER_JOBS_F PJF
            WHERE
                PA.PAAMJOBID = PJF.JOB_ID
                AND TO_DATE(PJF.EFFECTIVE_END_DATE, 'yyyy/mm/dd') > TO_DATE(SYSDATE, 'yyyy/mm/dd')



),

REQ_APPROVER_DETAIL AS (SELECT DISTINCT
                        PJ.*,
                        PAH.OBJECT_TYPE_CODE ,
                        PAH.SEQUENCE_NUM ,
                        PAH.ACTION_CODE ,
                        PAH.ACTION_DATE ,
                        PAH.PERFORMER_ID,
                        PAH.OBJECT_ID AS PAHOBJECTID
                    FROM
                        FUSION.PO_ACTION_HISTORY PAH,
                        PERSON_JOB PJ
                    WHERE
                        PAH.PERFORMER_ID = PJ.PPNPERSONID
                        AND ACTION_CODE = 'APPROVE'

),
REQ_DETAIL AS (SELECT DISTINCT
                RAD.*,
                RHA.requisition_number AS Req_Number,
                RHA.requisition_header_id RHAREQHEADID

            FROM
                REQ_APPROVER_DETAIL RAD,
                FUSION.POR_REQUISITION_HEADERS_ALL RHA
            WHERE
                RAD.PAHOBJECTID = RHA.requisition_header_id

),

REQ_LINE_DETAIL AS (SELECT DISTINCT
                    RD.*,
                    SUM(rla.unit_price*rla.quantity) AS QUANTITY_AMOUNT,
                    SUM(rla.amount) AS AMOUNT_ORDERED
                FROM
                    POR_REQUISITION_LINES_ALL RLA,
                    REQ_DETAIL RD
                WHERE
                    RD.RHAREQHEADID = RLA.requisition_header_id
                GROUP BY
                    RHAREQHEADID,
                    Req_Number,
                    OBJECT_TYPE_CODE,
                    SEQUENCE_NUM,
                    ACTION_CODE,
                    ACTION_DATE,
                    PERFORMER_ID,
                    PAHOBJECTID,
                    PJFJOBID,
                    APPROVAL_AUTHORITY,
                    PPNPERSONID,
                    FULL_NAME,
                    PAAMPERSONID,
                    PAAMJOBID



),

REQ_TOTAL_AMOUNT AS (SELECT DISTINCT
                    RLD.*,
                    CASE WHEN RLD.AMOUNT_ORDERED IS NULL THEN 
RLD.QUANTITY_AMOUNT
                        ELSE RLD.AMOUNT_ORDERED
                            END AS REQ_AMOUNT_TOTAL
                    FROM
                        REQ_LINE_DETAIL RLD
)

SELECT 
Req_Number,
FULL_NAME,
SEQUENCE_NUM,
REQ_AMOUNT_TOTAL,
--OBJECT_TYPE_CODE,
ACTION_DATE,
--ACTION_CODE
--APPROVAL_AUTHORITY


FROM REQ_TOTAL_AMOUNT

--WHERE PPNPERSONID = '300000008160059'
WHERE REQ_NUMBER = '702387'
AND ACTION_DATE = (SELECT 
MAX(PAH.ACTION_DATE),PAH.PERFORMER_ID FROM FUSION.PO_ACTION_HISTORY PAH, PER_PERSON_NAMES_F PPN WHERE PAH.PERFORMER_ID = PPN.PERSON_ID)

感谢您提供的任何帮助。我已将其调试到最后一个SELECT的最后一个WHERE子句。如果我将其删除/注释掉最后一行,它将恢复两行。到目前为止,我带回来的数据是:

REQ_Number|FULL_NAME|SEQUENCE_NUM|REQ_TOTAL_AMOUNT|ACTION_DATE
702387     Sam Jones    3          £19,000         2017-08-23T12:08:39.000+00:00
702387     Bill Baggins  4         £19,000         2017-08-23T13:55:55.000+00:00

我只想带回Bill Baggins的那一行,因为它是两行中最近执行的那一行,但是如果无法在Action_Date上完成,那么SEQUENCE_NUM也会把它带回4> 3等

任何帮助将不胜感激。再次抱歉,这个问题无疑已经被问了很多遍了

此致

马特

1 个答案:

答案 0 :(得分:0)

是的,这是您的问题:

AND ACTION_DATE = (SELECT MAX(PAH.ACTION_DATE), PAH.PERFORMER_ID 
                     FROM FUSION.PO_ACTION_HISTORY PAH, PER_PERSON_NAMES_F PPN 
                    WHERE PAH.PERFORMER_ID = PPN.PERSON_ID)

您的子查询返回两列MAX(PAH.ACTION_DATE)PAH.PERFORMER_ID,但您仅通过=ACTION_DATE来进行检查。因此,您正在将一列与两列进行比较,这是行不通的。

您可以使用(ACTION_DATE, <some other column>) = ...做类似的事情,但是我很确定这甚至不会解析,因为子查询中没有GROUP BY子句。无论如何,该子查询必须仅返回单行,否则您将获得“ ORA-01427:子查询返回多行”。

我怀疑您的子查询不是您想要的,您想删除pah.performer_id并添加一个将其与主查询相关联的子句,也许是AND PAH.PERFORMER_ID = REQ_TOTAL_AMOUNT.PPNPERSONID,也许是这样?