PSQL:如何将记录与以前的记录(2 个表)进行比较?

PSQL: How to compare record to previous record (2 tables)?

我有两个 table:

01_System_Log

request_date   request_id
2022-01-01     1
2022-01-10     2
2022-01-20     3

记录

firm    name    city  request_id

firm_a  John    NY              1
firm_b  Will    LA              1
firm_c  Andy    SF              1
firm_c  Marga   CH              1

firm_a  John    NY              2
firm_b  Will    LA              2
firm_b  Nancy   LA              2
firm_c  Andy    SF              2
firm_c  Marga   CH              2

firm_a  John    NY              3
firm_b  Will    CH              3
firm_b  Nancy   LA              3
firm_c  Andy    SF              3
firm_c  Marga   SF              3 
firm_c  Joe     SF              3 

我需要创建一个视图,将最后一个请求 id (3) 的记录与前一个 (2) 的记录进行比较。到目前为止我做了一个视图,其中显示了最实际的记录。

SELECT DISTINCT main.firm,
    main.name,
    main.city,
    sys_log.request_id,
FROM "Records" main
INNER JOIN ( 
    SELECT sys.request_date,
           sys.request_id
    FROM "01_System_Log" sys
    ORDER BY sys.request_id DESC
    LIMIT 1 ) sys_log ON sys_log.request_id = main.request_id
WHERE  main.request_id IS NOT NULL
ORDER BY main.firm;

我不确定如何进行,而在我发现的所有比较示例中,只使用了一个 table。

我理想的结果是:

firm   name_    city  request_id  firm_n name_n  city_n  request_id_n

firm_a  John     NY    2          firm_a  John     NY    3
firm_b  Will     LA    2          firm_b  Will     CH    3
firm_b  Nancy    LA    2          firm_b  Nancy    LA    3
firm_c  Andy     SF    2          firm_c  Andy     SF    3
firm_c  Marga    CH    2          firm_c  Marga    SF    3 
                                  firm_c  Joe      SF    3

信息全部来自一个 table "Records" 所以你只需要在查询中包含 table 两次,使用适当的别名,一次获取记录最高 request_id 和低于最高的 request_id 一次。最后你需要找到最高的 request_id 是多少,你可以用 "01_System_Log" table.

上的 CTE 来完成

CTE 很简单:

WITH highest AS (
    SELECT request_id AS id
    FROM "01_System_Log"
    ORDER BY request_date DESC LIMIT 1
)

使用最高request_id的记录也很容易找到:

SELECT firm, name, city, request_id
FROM "Records"
JOIN highest ON "Records".request_id = highest.id

要获得每个公司和名称组合的第二高 request_id 的记录,您需要使用 window function:

SELECT DISTINCT firm, name, 
       first_value(city) OVER w AS city, 
       first_value(request_id) OVER w AS request_id
FROM "Records"
JOIN highest ON "Records".request_id < highest.id -- exclude the highest request_id
WINDOW w AS (PARTITION BY (firm, name) ORDER BY request_id DESC
    ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)

然后将它们与 sub-queries 捆绑在一起(并删除包含重复信息的列):

WITH highest AS (
    SELECT request_id AS id
    FROM "01_System_Log"
    ORDER BY request_date DESC LIMIT 1
)
SELECT curr.firm, curr.name, 
       prev.city AS previous_city, prev.request_id AS previous_request_id,
       curr.city AS current_city, curr.request_id AS current_request_id
FROM (
    SELECT DISTINCT firm, name, 
           first_value(city) OVER w AS city, 
           first_value(request_id) OVER w AS request_id
    FROM "Records"
    JOIN highest ON "Records".request_id < highest.id
    WINDOW w AS (PARTITION BY (firm, name) ORDER BY request_id DESC
        ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) ) prev
RIGHT JOIN (
    SELECT firm, name, city, request_id
    FROM "Records"
    JOIN highest ON "Records".request_id = highest.id ) curr USING (firm, name)
ORDER BY curr.firm;