3
I’m riding a view in the database that the view pai
uses as another query view, which in turn calls again view (in that view is made the sum of several items and brings the total per line), in view 'outside' is picked up the accounts (by number) and play a name for each account, example:
case when conta = '1' then 'chamar desse nome'
But this occurs about 30 times in view, since we have several accounts, and at 3° view who calls this, he puts together several accounts, but using the name given in view previous, so it gets more or less this:
case when conta in ('alugueis', 'agua', 'luz') then ('grupo despesa')
It’s just that there are many accounts, and many groups, have reached a limit where the SQL gave me complexity limit reached, know what can be done to fix?
I’ll post an excerpt from the 3° view here to exemplify:
case
when (a.num_conta between '1.1.01.001.0001' and '1.2.99.999.9999') then 'TOTAL ATIVO'
when (a.num_conta between '2.1.01.001.0001' and '2.4.99.999.9999') then 'TOTAL PASSIVO'
when (a.num_conta between '2.1.01.001.0001' and '2.4.99.999.9999') then 'TOTAL PASSIVO'
when (a.num_conta between '3.1.01.001.0001' and '3.9.99.999.9999 ') then 'TOTAL RESULTADO' end as 'TC',
case
when (a.NUM_CONTA between '1.1.01.001.0001' and '1.1.99.999.9999') then '1 - ATIVO CIRCULANTE'
when (a.NUM_CONTA between '1.2.01.001.0001' and '1.2.99.999.9999 ') then '2 - ATIVO NÃO CIRCULANTE'
when (a.NUM_CONTA between '2.1.01.001.0001' and '2.1.99.999.9999 ') then '3 - PASSIVO CIRCULANTE'
when (a.NUM_CONTA between '2.2.01.001.0001' and '2.3.99.999.9999 ') then '4 - PASSIVO NÃO CIRCULANTE'
when (a.NUM_CONTA between '2.4.01.001.0001' and '2.4.99.999.9999 ') then '5 - PATRIMONIO LIQUIDO'
when (a.NUM_CONTA between '3.1.01.001.0001' and '3.9.99.999.9999 ') then '6 - RESULTADO DO PERIODO' end as tipo,
case when (a.tipo_conta in ('01 - CAIXAS E BANCOS', '02 - APLICAÇÕES FINANCEIRAS','03 - NUMERÁRIOS EM TRÂNSITO')) then '01 - TOTAL EQ CAIXA'
when (a.tipo_conta in ('01 - CLIENTES','02 - BANCOS CONTAS VINCULADAS','03 - TITULOS DE CAPITALIZAÇÃO','04 - ADIANTAMENTOS','05 - IMPOSTOS A RECUPERAR','OUTROS CRÉDITOS - CHEQUES DEVOLVIDOS','07 - MERCADORIAS EM TRANSITO ATIVO','08 - MUTUO NEWFER','09 - ADIANTAMENTOS PROJETO NEWFER')) then '02 - DIREITOS REALIZAVEIS'
when (a.tipo_conta in ('DESPESAS EXERCICIO SEGUINTE')) then '04 - DESPESAS DO EXERCICIO SEGUINTE'
when (a.tipo_conta in ('ESTOQUE')) then '03 - ESTOQUE'....
I don’t know which DBMS you are using but already evaluated the use of CTE (Common Table Expressions) with WITH?
– anonimo
I do not know this functionality friend, I will give a search to see, I was able to run eliminating some 'case whens' denials, but it is as the friend above said, the more accounts are entering, I will have to change and add more conditions and soon will burst again
– Rafael S.
First you do not need between, you can simply take the characters before the second level (1.1, 2.1, 3.1 etc) and make a dirata comparison. By the way I seem to have problem the format that used in the field (for example, what do the points inside the string? if the width is fixed, is storing spurious thing, if it is not fixed, the between will not work qq way) - that you should not have these strings in SQL, but in a separate table (either in SQL or in your application). The @Niero answer the main problems in the way it is applying its logic.
– Bacco
@Rafaels. Has the answer solved your question? Do you think you can accept it? See [tour] if you don’t know how to do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site.
– Maniero