如何在 GROUP BY 和 ORDER BY 中使用多列但只使用一列

How to use multiple columns but only one column in GROUP BY and ORDER BY

我想显示来自联接 table 的多列,但只使用一列到 group byorder by

我的 PostgreSQL 查询是这样的:

SELECT DISTINCT ON (r.id_rekrutmen)
       r.id_rekrutmen,
       r.judul_rekrutmen,
       rw.tanggal_rekrutmen
FROM rekrutmen r
   INNER JOIN rekrutmen_waktu rw
      ON r.id_rekrutmen = rw.id_rekrutmen
ORDER BY r.id_rekrutmen, rw.tanggal_rekrutmen DESC;

我想这就是你想要的:

SELECT 
           r.id_rekrutmen,
           r.judul_rekrutmen,
           MAX(rw.tanggal_rekrutmen) AS tanggal_rekrutmen
    FROM rekrutmen r
       INNER JOIN rekrutmen_waktu rw
          ON r.id_rekrutmen = rw.id_rekrutmen
    GROUP BY 
           r.id_rekrutmen,
           r.judul_rekrutmen,