PostgresSQL:对现有联接在嵌套上使用Where子句

时间:2020-04-03 19:07:09

标签: postgresql join unnest

我一直在努力克服此查询的一些数据格式问题,但无法解决如何限制月份列以排除未来日期的问题。源表包含将来不可靠的数据,因此我想排除它。

select fbr.organization, fbr.year, a.accounttype, unnest(array[concat(year,'0101'), concat(year,'0201'), concat(year,'0301'), concat(year,'0401'), concat(year,'0501'), concat(year,'0601'), concat(year,'0701'), concat(year,'0801'), concat(year,'0901'), concat(year,'1001'), concat(year,'1101'), concat(year,'1201')])::date as month, unnest(array[sum(jan), sum(feb), sum(mar), sum(apr), sum(may), sum(jun), sum(jul), sum(aug), sum(sep), sum(oct), sum(nov), sum(dec)]) AS balance from fundbalancereport as fbr
left join account as a on a.accountnumber = split_part(text,' ',1) and a.organization = fbr.organization
where year > 2018
and left(text,1) = '1'
and accounttype = 'Bank'
group by fbr.organization, fbr.year, a.accounttype

这将产生我需要的确切格式,例如:

"organization","year","accounttype","month","balance"
"org1",2020,"Bank","2020-01-01","500.00000"
"org1",2020,"Bank","2020-02-01","550.00000"
"org1",2020,"Bank","2020-03-01","650.00000"
"org1",2020,"Bank","2020-04-01","450.00000"
"org1",2020,"Bank","2020-05-01","450.00000"
"org1",2020,"Bank","2020-06-01","450.00000"
"org1",2020,"Bank","2020-07-01","450.00000"
"org1",2020,"Bank","2020-08-01","450.00000"
"org1",2020,"Bank","2020-09-01","450.00000"
"org1",2020,"Bank","2020-10-01","450.00000"
"org1",2020,"Bank","2020-11-01","450.00000"
"org1",2020,"Bank","2020-12-01","450.00000"

理想情况下,我想添加month的where子句小于或等于今天,该子句应产生:

"organization","year","accounttype","month","balance"
"org1",2020,"Bank","2020-01-01","500.00000"
"org1",2020,"Bank","2020-02-01","550.00000"
"org1",2020,"Bank","2020-03-01","650.00000"
"org1",2020,"Bank","2020-04-01","450.00000"

我阅读了以下q:How to add where clause with unnest in sql query?

但是当我尝试添加附加的SELECT时,我收到一个错误,返回了多行,因此我从这里有点迷失了,因为我已经有了一个需要的现有联接。

我尝试过:

select fbr.organization, fbr.year, a.accounttype, 
(select unnest(array[concat(year,'0101'), concat(year,'0201'), concat(year,'0301'), concat(year,'0401'), concat(year,'0501'), concat(year,'0601'), concat(year,'0701'), concat(year,'0801'), concat(year,'0901'), concat(year,'1001'), concat(year,'1101'), concat(year,'1201')])::date as month), (select unnest(array[sum(jan), sum(feb), sum(mar), sum(apr), sum(may), sum(jun), sum(jul), sum(aug), sum(sep), sum(oct), sum(nov), sum(dec)]) AS balance) from fundbalancereport as fbr
left join account as a on a.accountnumber = split_part(text,' ',1) and a.organization = fbr.organization
where year > 2018
and left(text,1) = '1'
and accounttype = 'Bank'
group by fbr.organization, fbr.year, a.accounttype

得到了

ERROR:  more than one row returned by a subquery used as an expression

任何指导都会非常有帮助,谢谢!

1 个答案:

答案 0 :(得分:2)

unnest部分移出select

select unnest(array['20190101'::date, '20200101'::date, '20210101'::date]) d

添加到查询的from部分,并添加where部分

select d
from unnest(array['20190101'::date, '20200101'::date, '20210101'::date]) d
where d<=now()

编辑:

我相信您想取消数据透视,在这种情况下,您的查询可能是:

select fbr.organization, fbr.year, a.accounttype, fbr.month, sum(fbr.val) as balance
from
  (
    select organization, year, text, concat(year,'0101')::date as month, jan as val from fundbalancereport union
    select organization, year, text, concat(year,'0201')::date as month, feb as val from fundbalancereport union
    select organization, year, text, concat(year,'0301')::date as month, mar as val from fundbalancereport union
    select organization, year, text, concat(year,'0401')::date as month, apr as val from fundbalancereport union
    select organization, year, text, concat(year,'0501')::date as month, may as val from fundbalancereport union
    select organization, year, text, concat(year,'0601')::date as month, jun as val from fundbalancereport union
    select organization, year, text, concat(year,'0701')::date as month, jul as val from fundbalancereport union
    select organization, year, text, concat(year,'0801')::date as month, aug as val from fundbalancereport union
    select organization, year, text, concat(year,'0901')::date as month, sep as val from fundbalancereport union
    select organization, year, text, concat(year,'1001')::date as month, oct as val from fundbalancereport union
    select organization, year, text, concat(year,'1101')::date as month, nov as val from fundbalancereport union
    select organization, year, text, concat(year,'1201')::date as month, dec as val from fundbalancereport
  ) fbr
left join account as a on a.accountnumber = split_part(text,' ',1) and a.organization = fbr.organization
where year > 2018
and left(text,1) = '1'
and accounttype = 'Bank'
group by fbr.organization, fbr.year, a.accounttype, fbr.month