Turn rows into SQL columns

Asked

Viewed 1,317 times

0

I have the following Query:

SELECT distinct finmovem.num_transacao
       ,finempe.codigo_orgao 
       ,finempe.cod_reduzido 
       ,finempe.num_empenho 
       ,finempe.data_empenho 
       ,finempe.nome_fornecedor 
       ,finempe.valor_empenhado 
       ,finempe.valor_anulado 
       ,finempe.valor_pago 
       ,finempe.valor_liquidado 
       ,cast(finempe.valor_empenhado as decimal(18,2)) - cast(finempe.valor_anulado as decimal(18,2)) - cast(finempe.valor_liquidado as decimal(18,2)) as sld_a_liquidar 
       ,cast(finempe.valor_empenhado as decimal(18,2)) - cast(finempe.valor_anulado as decimal(18,2)) - cast(finempe.valor_pago as decimal(18,2)) as sld_a_pagar
       ,cast(orcdotac.sld_orc_ano as decimal(18,2)) + cast(orcdotac.sld_orc_vinc as decimal(18,2)) + cast(orcdotac.sld_supl_ano as decimal(18,2)) + cast(orcdotac.sld_esp_ano as decimal(18,2)) + cast(orcdotac.sld_ext_ano as decimal(18,2)) - cast(orcdotac.sld_re_ano as decimal(18,2)) as saldo_dot_ant
       ,SUM(finmovem.valor_transacao ) tansacao
FROM finempe
INNER JOIN finmovem ON
    (finempe.num_empenho = finmovem.num_empenho) 
INNER JOIN orcdotac ON
    (finempe.cod_reduzido = orcdotac.cod_reduzido) 
WHERE finempe.codigo_tipo = 1 and finempe.codigo_orgao = 02 and finempe.data_empenho between '1-1-2002' and '31-12-2002' and finempe.cod_reduzido = 25
GROUP BY finmovem.num_transacao, finempe.codigo_orgao, finempe.cod_reduzido, finempe.num_empenho, finempe.data_empenho, finempe.nome_fornecedor, finempe.valor_empenhado, 
finempe.valor_anulado, finempe.valor_pago, finempe.valor_liquidado, orcdotac.sld_orc_ano, orcdotac.sld_orc_vinc, orcdotac.sld_supl_ano, orcdotac.sld_esp_ano, orcdotac.sld_ext_ano,
orcdotac.sld_re_ano   
ORDER BY finempe.cod_reduzido, finempe.num_empenho

That returns me the following result:

inserir a descrição da imagem aqui

I would like the in a transaction and transaction turn columns instead of rows, that is, for each num_transaction to have a corresponding transaction column, what modifications should be made? I just want a line to number 11.

  • the problem is different

  • 1

    the problem may even be different but I believe that adapting the other resosta may have the solution. For the complete solution I believe it is difficult to deliver to you

  • 1

    @Otto , I do not know if you agree with me, although the doubt is the same and only present a variation I think it is valid not to be a duplicate, because there are differences and I believe it will help more than the post you mentioned, given that I put the answer to what I did right here, What do you think? take a look there, it’s in SQL more "raw", let’s say so

  • Duplicate because it is a similar solution, it is very naughty to come here and want the complete solution to your problem since it has similar things, everything needs an adaptation to work as Oce wants.

2 answers

1

  • the question is, if num_transaction is 2 transaction goes to a column, if num_transaction is 3 goes to another column and so on, you think that would be the case to use pivot?

  • the pivot is exactly for this

  • Hello @V.Avancini use the example already posted here in the link https://answall.com/questions/289481/pivot-t-sql-din%C3%A2mico-sequencial

0


I used the tips given in the post and arrived at the following answer:

SELECT num_empenho AS EMP, data_empenho AS DATA, nome_fornecedor AS FORNECEDOR, valor_empenhado AS EMPENHADO, valor_anulado AS ANULADO, [3] AS LIQ_PERIODO, valor_liquidado AS LIQUID_ACUM, [5] + [7] AS PAGO_PERIODO, valor_pago AS PAGO_ACUMUL
FROM (
      SELECT finmovem.num_transacao
       ,finmovem.valor_transacao
       ,finempe.codigo_orgao 
       ,finempe.cod_reduzido 
       ,finempe.num_empenho 
       ,finempe.data_empenho 
       ,finempe.nome_fornecedor 
       ,finempe.valor_empenhado 
       ,finempe.valor_anulado 
       ,finempe.valor_pago 
       ,finempe.valor_liquidado 
       ,cast(finempe.valor_empenhado as decimal(18,2)) - cast(finempe.valor_anulado as decimal(18,2)) - cast(finempe.valor_liquidado as decimal(18,2)) as sld_a_liquidar 
       ,cast(finempe.valor_empenhado as decimal(18,2)) - cast(finempe.valor_anulado as decimal(18,2)) - cast(finempe.valor_pago as decimal(18,2)) as sld_a_pagar
       ,cast(orcdotac.sld_orc_ano as decimal(18,2)) + cast(orcdotac.sld_orc_vinc as decimal(18,2)) + cast(orcdotac.sld_supl_ano as decimal(18,2)) + cast(orcdotac.sld_esp_ano as decimal(18,2)) + cast(orcdotac.sld_ext_ano as decimal(18,2)) - cast(orcdotac.sld_re_ano as decimal(18,2)) as saldo_dot_ant     
FROM finmovem
LEFT JOIN finempe ON
    (finempe.num_empenho = finmovem.num_empenho) 
INNER JOIN orcdotac ON
    (finempe.cod_reduzido = orcdotac.cod_reduzido) 
WHERE finmovem.codigo_tipo = 1 and finmovem.codigo_orgao = 02 and finmovem.data_transacao between '1-1-2002' and '31-12-2002' and finempe.cod_reduzido = 25
GROUP BY finmovem.valor_transacao, finmovem.num_transacao, finempe.codigo_orgao, finempe.cod_reduzido, finempe.num_empenho, finempe.data_empenho, finempe.nome_fornecedor, finempe.valor_empenhado, 
finempe.valor_anulado, finempe.valor_pago, finempe.valor_liquidado, orcdotac.sld_orc_ano, orcdotac.sld_orc_vinc, orcdotac.sld_supl_ano, orcdotac.sld_esp_ano, orcdotac.sld_ext_ano,
orcdotac.sld_re_ano   
      ) sq 
PIVOT (SUM(sq.valor_transacao) FOR num_transacao IN ( [3], [5], [7])) AS pt

The four generated lines turned this:

inserir a descrição da imagem aqui

Thanks a lot for the tips, guys!

  • If you want to test other solutions, visit Alas & Pivots - turning rows into columns -> https://portosql.wordpress.com/2019/05/04/pivot/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.