select 语句中的子查询 POSTGRESQL
SubQuery within a select statement POSTGRESQL
我有两个不同的 tables(医疗和 Rx 数据)都包含支付日期和净付款。我想按医疗净支付总额和 Rx 净支付总额的年份和月份创建报告摘要。当我 运行 下面的 SQL 时,POSTGRESQL 说“子查询使用来自外部查询”的未分组列“a.date_paid”
我的想法是按第一个 table 分组并从第二个 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
我自己解决了这个问题...
select a.date_paid,a.member_ssn,memfirstname,memlastname ,cast (sum(a.net_payment) as money) ,
(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)
from elan.staging_claims a, elan.elig c
where a.member_ssn=c.employeessn
group by a.date_paid,a.member_ssn,memfirstname,memlastname
having sum(a.net_payment)>79999
order by 2 desc
我有两个不同的 tables(医疗和 Rx 数据)都包含支付日期和净付款。我想按医疗净支付总额和 Rx 净支付总额的年份和月份创建报告摘要。当我 运行 下面的 SQL 时,POSTGRESQL 说“子查询使用来自外部查询”的未分组列“a.date_paid”
我的想法是按第一个 table 分组并从第二个 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
我自己解决了这个问题...
select a.date_paid,a.member_ssn,memfirstname,memlastname ,cast (sum(a.net_payment) as money) ,
(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)
from elan.staging_claims a, elan.elig c
where a.member_ssn=c.employeessn
group by a.date_paid,a.member_ssn,memfirstname,memlastname
having sum(a.net_payment)>79999
order by 2 desc