PostgresSQL: использование предложения Where для Unnest с существующим соединением - PullRequest
0 голосов
/ 03 апреля 2020

Я усердно трудился, чтобы преодолеть некоторые проблемы с форматированием данных с помощью этого запроса, но не могу понять, как ограничить столбец месяца ненужным, чтобы исключить будущие даты. Исходная таблица содержит будущие данные, которые ненадежны, поэтому я хочу исключить их.

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"

В идеале я бы хотел добавить, где предложение месяца меньше или равно сегодняшнему дню, что должно дать:

"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: Как добавить предложение where с unnest в sql запросе?

Но когда я попытался добавить дополнительный 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 Ответ

2 голосов
/ 03 апреля 2020

Переместить 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
...