I have two different tables (Medical and Rx Data) both that contain the Paid Date and Net Payment. I want to create a report summary by Year and Month of the Total Medical Net Payment and Rx Net Payment. When I run the below SQL, POSTGRESQL says "subquery uses ungrouped column "a.date_paid" from outer query"
My thought was to group by the first table and Subquery the total Net payment from the second table. POSTGRESQL
select cast(EXTRACT(YEAR FROM a.date_paid) as text) || to_char(a.date_paid,'mm') as date_paid,
cast (sum(a.net_payment) as money) as Medical,a.member_ssn,
(Select sum(b.netpayment) from elan.rx_claims b where cast(EXTRACT(YEAR FROM a.date_paid) as text) ||
to_char(a.date_paid,'mm')=cast(EXTRACT(YEAR FROM b.date_paid) as text) || to_char(b.date_paid,'mm')
and a.member_ssn=b.member_ssn ) as RxPaid
from elan.staging_claims a
group by cast(EXTRACT(YEAR FROM a.date_paid) as text) || to_char(a.date_paid,'mm'),a.member_ssn
date_trunc()function; you dont have to use these ugly string-concatenations. Plus: some formatting would help your query's readability.