Join tables on column where another column is max

时间:2019-03-19 14:54:22

标签: sql postgresql

In PostgreSQL, I'm attempting to select the columns in the first select list which have the most recent ff.sub_date. The unique value in the ff table is object_id which is what I need to be joining on. My attempt is below:

SELECT r0.ein, "TtlRvAndExpnssAmt" AS "Revenue", "TtlAsstsEOYFMVAmt" AS "Assets",
       "CmpOfcrDrTrstRvAndExpnssAmt" AS "Compensation & Benefits Expense" 
FROM filing_filing ff
JOIN return_pf_part_0 r0 ON ff.object_id = r0.object_id
JOIN return_pf_part_i r1 ON ff.object_id = r1.object_id
JOIN return_pf_part_ii r2 ON ff.object_id = r2.object_id
WHERE (ff.ein, sub_date) IN (SELECT ff.ein, max(sub_date)
                             FROM filing_filing ff
                             GROUP BY ff.ein)
  AND ff.ein = '456829368';

It seems like what I really need in the subquery is to include the column object_id, but I can't do that because I can't group by it.

Note: Later on, the WHERE clause will include a long list of ff.EINs

2 个答案:

答案 0 :(得分:1)

Try with a window function (ROW_NUMBER() used below but RANK() could be an alternative in case 2 records have the same sub_date):

SELECT r0.ein, "TtlRvAndExpnssAmt" AS "Revenue", "TtlAsstsEOYFMVAmt" AS "Assets",
   "CmpOfcrDrTrstRvAndExpnssAmt" AS "Compensation & Benefits Expense" 
FROM (
    SELECT *, ROW_NUMBER() OVER (PARTITION BY ein ORDER BY sub_date DESC) AS RowNumber
    FROM filing_filing
) ff
JOIN return_pf_part_0 r0 ON ff.object_id = r0.object_id
JOIN return_pf_part_i r1 ON ff.object_id = r1.object_id
JOIN return_pf_part_ii r2 ON ff.object_id = r2.object_id
WHERE ff.rownumber = 1

答案 1 :(得分:0)

Could you do something like this an pre-filter your filing table?

WITH Filing as (Select * from 
(Select object_id, sub_date, ein, DENSE_RANK() OVER (Order by SubDate desc) as drank 
from filing_filing) aa where aa.drank=1)

SELECT r0.ein, "TtlRvAndExpnssAmt" AS "Revenue", "TtlAsstsEOYFMVAmt" AS "Assets",
       "CmpOfcrDrTrstRvAndExpnssAmt" AS "Compensation & Benefits Expense" 
FROM filing ff
JOIN return_pf_part_0 r0 ON ff.object_id = r0.object_id
JOIN return_pf_part_i r1 ON ff.object_id = r1.object_id
JOIN return_pf_part_ii r2 ON ff.object_id = r2.object_id
WHERE (ff.ein, sub_date) IN (SELECT ff.ein, max(sub_date)
                             FROM filing_filing ff
                             GROUP BY ff.ein)
  AND ff.ein = '456829368';