PostgreSQL聚合函数选择2个相应字段

PostgreSQL聚合函数选择2个相应字段,sql,postgresql,Sql,Postgresql,我有以下子查询: LEFT OUTER JOIN ( SELECT MAX(testresult."testdate") AS beforeresult, testschedule."test_id", testschedule."asset_id", testschedule."testdate" AS testdate, testschedule."testresult_id" as schedulere

我有以下子查询:

LEFT OUTER JOIN
(
    SELECT
        MAX(testresult."testdate") AS beforeresult,
        testschedule."test_id",
        testschedule."asset_id",
        testschedule."testdate" AS testdate,
        testschedule."testresult_id" as scheduleresult_id                                        
    FROM
        "public"."testresult" testresult 
        INNER JOIN "public"."testschedule" testschedule 
            ON testschedule."asset_id" = testresult."asset_id" 
            AND testschedule."test_id" = testresult."test_id" 
    WHERE
        "testresult"."client_id" = 25368272
        AND testresult."testdate" < testschedule."mintolerancedate"
        AND testschedule."testdate" > '2016-10-01' 
        AND testschedule."testdate" < '2016-10-20'
    GROUP BY
        testschedule."asset_id", 
        testschedule."test_id", 
        testschedule."testdate", 
        testschedule."testresult_id" 
    ORDER BY MAX(testresult."testdate")
) lasttr
    ON "testschedule"."asset_id" = lasttr."asset_id"
    AND "testschedule"."test_id" = lasttr."test_id"
    AND testschedule."testdate" = lasttr.testdate

这给了我正确的测试日期。但是,我还需要与日期对应的testschedule.testresult_id。有没有办法从上面的查询中选择此选项?

您可以摆弄窗口函数,或者在最新版本中,执行左侧连接:

LEFT JOIN LATERAL
(SELECT ts."test_id", ts."asset_id", ts."testdate" AS testdate, ts."testresult_id" as scheduleresult_id,
        tr.*
 FROM "public"."testresult" tr INNER JOIN
      "public"."testschedule" ts
      ON ts."asset_id" = tr."asset_id" and ts."test_id" = tr."test_id" 
 WHERE tr."client_id" = 25368272 AND
       tr."testdate" < ts."mintolerancedate" AND
       ts."testdate" > '2016-10-01' and ts."testdate" < '2016-10-20' AND
       "testschedule"."asset_id" = ts."asset_id" AND
       "testschedule"."test_id" = ts."test_id"
 ORDER BY tr."testdate" DESC
 FETCH FIRST 1 ROW ONLY
) lasttr

横向联接类似于FROM子句中的相关子查询。您可以返回任意数量的列。没有ON子句,因为联接条件在子查询的WHERE子句中。

您可以摆弄窗口函数,或者在最新版本中,执行左侧联接:

LEFT JOIN LATERAL
(SELECT ts."test_id", ts."asset_id", ts."testdate" AS testdate, ts."testresult_id" as scheduleresult_id,
        tr.*
 FROM "public"."testresult" tr INNER JOIN
      "public"."testschedule" ts
      ON ts."asset_id" = tr."asset_id" and ts."test_id" = tr."test_id" 
 WHERE tr."client_id" = 25368272 AND
       tr."testdate" < ts."mintolerancedate" AND
       ts."testdate" > '2016-10-01' and ts."testdate" < '2016-10-20' AND
       "testschedule"."asset_id" = ts."asset_id" AND
       "testschedule"."test_id" = ts."test_id"
 ORDER BY tr."testdate" DESC
 FETCH FIRST 1 ROW ONLY
) lasttr

横向联接类似于FROM子句中的相关子查询。您可以返回任意数量的列。没有ON子句,因为连接条件在子查询的WHERE子句中。

感谢您的快速响应,但我仍然没有得到testschedule.testresult\u id字段。基本上,它需要以某种方式连接到testdate的MAX aggregate函数,但我似乎无法做到这一点——我猜这是一个我忽略的简单问题。你不需要另一个连接。横向连接应该做你想做的。所以我在每一行之前添加和:testschedule.asset\u id=ts.asset\u id testschedule.test\u id=ts.test\u id,然后当我运行这个时,即使在添加group by子句时,我在输入的末尾也会得到一个语法错误。如果我添加ON子句,那么它会运行,但不会给出每个组的结果。感谢您的快速响应,但我仍然没有得到testschedule.testresult\u id字段。基本上,它需要以某种方式连接到testdate的MAX aggregate函数,但我似乎无法做到这一点——我猜这是一个我忽略的简单问题。你不需要另一个连接。横向连接应该做你想做的。所以我在每一行之前添加和:testschedule.asset\u id=ts.asset\u id testschedule.test\u id=ts.test\u id,然后当我运行这个时,即使在添加group by子句时,我在输入的末尾也会得到一个语法错误。如果我添加ON子句,它就会运行,但不会给出每个组的结果。