0

I am getting the postgresql error Type "MAX" does not exist on the following:

SELECT
    ff.object_id 'RecordId',
    ff.ein 'EIN',
    taxpayer_name 'Organization Name',
    r0."USAddrss_AddrssLn1Txt" 'Address',
    r0."USAddrss_CtyNm" 'City',
    r0."USAddrss_SttAbbrvtnCd" 'State',
    r0."USAddrss_ZIPCd" 'Zip Code',
    sj.*
FROM
    peerlist pl
    LEFT JOIN
    filing_filing ff
        ON CAST(pl.ein AS varchar) = ff.ein
    JOIN
    return_part_0 r0
        ON ff.object_id = r0.object_id
    JOIN
    return_skdjrltdorgoffcrtrstkyempl sj
        ON ff.object_id = sj.object_id
    INNER JOIN
    (
        SELECT
            ff.ein,
            MAX(ff.tax_period) 'tax_period'
        FROM
            filing_filing ff
        GROUP BY
            ff.ein
    ) b
        ON ff.ein = b.ein
            AND ff.tax_period = b.tax_period;

FF.tax_period is an integer, so I'm not sure why MAX isn't working.

JNevill
  • 46,980
  • 4
  • 38
  • 63
extensionhelp
  • 564
  • 1
  • 4
  • 18
  • Possible duplicate of [What is the difference between single quotes and double quotes in PostgreSQL?](https://stackoverflow.com/questions/41396195/what-is-the-difference-between-single-quotes-and-double-quotes-in-postgresql) – JNevill Nov 15 '18 at 21:23

1 Answers1

0

You need to cast/assign the column name with the AS keyword. You also need to use double-quotes in the column identifier.

Replace

SELECT ff.ein, MAX(ff.tax_period) 'tax_period' FROM

with:

SELECT ff.ein, MAX(ff.tax_period) AS "tax_period" FROM

Notice the AS after the max() call and before the tax_period

Cody Caughlan
  • 32,456
  • 5
  • 63
  • 68