PostgresQL:在具有现有连接的 Unnest 上使用 Where 子句
PostgresSQL: Using Where Clause on Unnest with Existing Join
我已经努力解决了此查询的一些数据格式问题,但无法全神贯注地思考如何限制 unnest 中的月份列以排除未来日期。来源 table 包含不可靠的未来数据,因此我想将其排除。
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"
理想情况下,我想添加一个 where 子句 of month is less than or equal to today 这应该产生:
"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"
我看完了这个问题:
但是当我尝试添加额外的 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
任何指导都会很有帮助,谢谢!
从 select
部分移动 unnest
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
我已经努力解决了此查询的一些数据格式问题,但无法全神贯注地思考如何限制 unnest 中的月份列以排除未来日期。来源 table 包含不可靠的未来数据,因此我想将其排除。
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"
理想情况下,我想添加一个 where 子句 of month is less than or equal to today 这应该产生:
"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"
我看完了这个问题:
但是当我尝试添加额外的 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
任何指导都会很有帮助,谢谢!
从 select
部分移动 unnest
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