如何在左联接上获得最大日期

时间:2019-01-29 22:32:10

标签: postgresql sql-view

在我们的数据架构师意外离开后,我最近被任命为数据角色。我试图将一个新列添加到现有视图中,这是成功的,但是我需要获取可以包含多行的记录的最大日期,并为新列添加逻辑。

我能够成功添加新列和逻辑,但是随后要求我们仅在一个记录集存在多个行时才返回最新行。我已经尝试在该站点的LEFT JOIN中找到所有MAX(date)的变体,这些变体都可以工作,但是后来我改变了新列的逻辑。经验不足,我不知下一步该怎么做。

CREATE OR REPLACE VIEW public.qafeedback_vw AS
SELECT DISTINCT
    CASE
        WHEN pie_work.pie_date IS NULL THEN 
error_assigned.created::timestamp with time zone
        ELSE pie_work.pie_date
    END AS pie_date,
    CASE
        WHEN error_assigned.qa_date IS NULL THEN 
note_qa.qa_date::timestamp with time zone
        ELSE error_assigned.qa_date
    END AS qa_date,
error_assigned.rse_state,
error_assigned.recordsetid,
error_assigned.record_set_id,
    CASE
        WHEN pie_work.validity_decision IS NULL THEN 
error_assigned.validity_decision
        ELSE pie_work.validity_decision
    END AS validity_decision,
error_assigned.error_status,
error_assigned.weight,
error_assigned.error_type,
error_assigned.task_id,
error_assigned.jattuserid,
error_assigned.user_id,
error_assigned.project_id,
error_assigned.project_name,
error_assigned.job_id,
concat_ws(' : '::text,
    CASE
        WHEN array_to_string(note_pie.note, '--'::text) IS NULL THEN 
''::text
        ELSE array_to_string(note_pie.note, '--'::text)
    END, array_to_string(note_qa.note, '--'::text)) AS comments,
    CASE
        WHEN (EXISTS ( SELECT 1
           FROM action
             JOIN activity ON action.action_id = activity.action_id
          WHERE activity.activity_type_id = 1227::numeric AND 
action.set_record_set_id = error_assigned.record_set_id AND 
action.created_by = error_assigned.jattuserid)) THEN true
        ELSE false
    END AS user_reviewed,
    CASE
       WHEN resolve_reason IS NULL or resolve_reason = '' THEN
            CASE WHEN supervisor_review.id IS NULL THEN ''
            ELSE 'PENDING' END
        ELSE 'RESOLVED'
    END AS supervisor_comment
 FROM ( SELECT rse.record_set_id,
        rse.workflow_state AS rse_state,
        ep.recordsetstate AS error_state,
        ep.displayname AS error_status,
        rse.create_dt AS qa_date,
        round(ep.weight::numeric(2,1), 1) AS weight,
        ep.error_type,
        rs.recordsetid,
        rs.created,
        j.id AS jattuserid,
        j.userid AS user_id,
        rse.project_id,
        rs.projectid AS project_name,
        rse.work_task_id AS task_id,
        rse.created_by AS qa_user_id,
        rse.job_id,
        rs.validationdecision AS validity_decision
       FROM record_set_error rse
         JOIN error_project ep ON ep.id = rse.error_project_id
         JOIN record_set rs ON rse.record_set_id = rs.id
         JOIN jattuser j ON rse.user_assigned_to_error = j.id) 
error_assigned           
 LEFT JOIN ( SELECT max(act.create_dt) AS pie_date,
        a.set_record_set_id,
        a.set_task_id AS task_id,
        a.set_validity_decision AS validity_decision
       FROM action a
         JOIN activity act ON a.action_id = act.action_id AND 
(act.activity_nm::text ~~ '%Advance%'::text OR act.activity_nm::text ~~ 
'Set Reviewed'::text OR act.activity_nm::text ~~ 'Split approved'::text)
      GROUP BY a.set_record_set_id, a.set_task_id, 
a.set_validity_decision) pie_work ON pie_work.set_record_set_id = 
error_assigned.record_set_id AND error_assigned.task_id = 
pie_work.task_id

LEFT JOIN supervisor_review on error_assigned.project_id = 
supervisor_review.project_id 
        AND error_assigned.record_set_id = 
supervisor_review.record_set_id

LEFT JOIN ( SELECT n.record_set_id,
        n.recordsetstate,
        array_agg(n.note) AS note,
        n.user_id,
        max(n.createdon) AS qa_date
       FROM notes n
      GROUP BY n.record_set_id, n.recordsetstate, n.user_id) note_pie ON 
error_assigned.rse_state::text = note_pie.recordsetstate::text AND 
note_pie.record_set_id = error_assigned.record_set_id AND 
note_pie.user_id = error_assigned.jattuserid
 LEFT JOIN ( SELECT n.record_set_id,
        n.recordsetstate,
        array_agg(n.note) AS note,
        n.user_id,
        max(n.createdon) AS qa_date
       FROM notes n
      GROUP BY n.record_set_id, n.recordsetstate, n.user_id) note_qa ON 
(split_part(error_assigned.rse_state::text, '-'::text, 1) ||
    CASE
        WHEN split_part(error_assigned.rse_state::text, '-'::text, 2) = 
'QA'::text THEN '-QAQA'::text
        ELSE '-QA'::text
    END) = note_qa.recordsetstate::text AND note_qa.record_set_id = 
error_assigned.record_set_id AND note_qa.user_id = 
error_assigned.qa_user_id;

这的实际结果是,我得到6行数据和2行数据,记录集为1。我需要获取具有最新日期的记录集。我正在使用以下代码:

LEFT JOIN supervisor_review on error_assigned.project_id = 
supervisor_review.project_id 
AND error_assigned.record_set_id = supervisor_review.record_set_id

每次尝试都没有MAX(date)时,我就失去了create_comment的逻辑,因此我把它遗漏了,希望有一些建议。

新表为supervisor_review,新列为create_comment (supervisor_comment)

0 个答案:

没有答案