Jump to content
Sign in to follow this  
samesdavis

[Resolvido] PIVOT em MySQL

Recommended Posts

samesdavis

Olá pessoal,

Estou migrando um sistema que era em Access para MySQL e me deparei com essa dúvida:

Tenho uma consulta de referência cruzada ( PIVOT ) , só que não sei como montar o comando SQL no MySQL.

TRANSFORM IIf(IsNull(Sum([TOTALVL])),0,Sum([TOTALVL])) AS SomaDeSALDOVENC SELECT Qry_Financ_BaseAnalise_CX.ANOCX, Qry_Financ_BaseAnalise_CX.ID_CONTA, Qry_Financ_BaseAnalise_CX.DS_CONTA FROM Qry_Financ_BaseAnalise_CX WHERE (((Qry_Financ_BaseAnalise_CX.ANOCX)=2013) AND ((Qry_Financ_BaseAnalise_CX.TP_CONTA)='1')) GROUP BY Qry_Financ_BaseAnalise_CX.ANOCX, Qry_Financ_BaseAnalise_CX.ID_CONTA, Qry_Financ_BaseAnalise_CX.DS_CONTA PIVOT Qry_Financ_BaseAnalise_CX.MESCX;

A parte do Isnull() eu consegui resolver assim

coalesce(`Qry_Financ_BaseAnalise_CX`.`TOTALVL`,0)

Mas o restante estou com dificuldades. Alguém pode me ajudar ?

Share this post


Link to post
Share on other sites
samesdavis

Consegui resolver a consulta abaixo no MySQL

tSQL = " SELECT `qry_financ_baseanalise`.`ID_CONTA`, `qry_financ_baseanalise`.`DS_CONTA`, Sum(CASE `qry_financ_baseanalise`.`MESVENC` " & _

" WHEN 1 THEN `qry_financ_baseanalise`.`SALDOVENC` ELSE 0 END) 'Jan', Sum(CASE `qry_financ_baseanalise`.`MESVENC` " & _

" WHEN 2 THEN `qry_financ_baseanalise`.`SALDOVENC` ELSE 0 END) 'Fev', Sum(CASE `qry_financ_baseanalise`.`MESVENC` " & _

" WHEN 3 THEN `qry_financ_baseanalise`.`SALDOVENC` ELSE 0 END) 'Mar', Sum(CASE `qry_financ_baseanalise`.`MESVENC` " & _

" WHEN 4 THEN `qry_financ_baseanalise`.`SALDOVENC` ELSE 0 END) 'Abr', Sum(CASE `qry_financ_baseanalise`.`MESVENC` " & _

" WHEN 5 THEN `qry_financ_baseanalise`.`SALDOVENC` ELSE 0 END) 'Mai', Sum(CASE `qry_financ_baseanalise`.`MESVENC` " & _

" WHEN 6 THEN `qry_financ_baseanalise`.`SALDOVENC` ELSE 0 END) 'Jun', Sum(CASE `qry_financ_baseanalise`.`MESVENC` " & _

" WHEN 7 THEN `qry_financ_baseanalise`.`SALDOVENC` ELSE 0 END) 'Jul', Sum(CASE `qry_financ_baseanalise`.`MESVENC` " & _

" WHEN 8 THEN `qry_financ_baseanalise`.`SALDOVENC` ELSE 0 END) 'Ago', Sum(CASE `qry_financ_baseanalise`.`MESVENC` " & _

" WHEN 9 THEN `qry_financ_baseanalise`.`SALDOVENC` ELSE 0 END) 'Set', Sum(CASE `qry_financ_baseanalise`.`MESVENC` " & _

" WHEN 10 THEN `qry_financ_baseanalise`.`SALDOVENC` ELSE 0 END) 'Out', Sum(CASE `qry_financ_baseanalise`.`MESVENC` " & _

" WHEN 11 THEN `qry_financ_baseanalise`.`SALDOVENC` ELSE 0 END) 'Nov', Sum(CASE `qry_financ_baseanalise`.`MESVENC` " & _

" WHEN 12 THEN `qry_financ_baseanalise`.`SALDOVENC` ELSE 0 END) 'Dez' FROM `qry_financ_baseanalise` GROUP BY " & _

" `qry_financ_baseanalise`.`ANOVENC`, `qry_financ_baseanalise`.`TP_LANC`, `qry_financ_baseanalise`.`ID_CONTA`, " & _

" `qry_financ_baseanalise`.`DS_CONTA` HAVING `qry_financ_baseanalise`.`ANOVENC` = " & Me.cboAno.Text & " AND `qry_financ_baseanalise`.`TP_LANC` = 'PG';"

ds = Busca.RetornarDataSet(tSQL)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.