ferreira12 Posted March 18, 2015 at 11:43 AM Report Share #579708 Posted March 18, 2015 at 11:43 AM (edited) Boas, estou a fazer a seguinte query. Select Marca, case when XX = 'Vendas' then Valor else '' end Vendas, case when XX = 'Devolucoes' then Valor else '' end Devolucoes from ( select MArca,SUM(valor) as Valor, 'Vendas' as XX from tabela1 group by MarcaID union select Marca,SUM(valor) as Valor, 'Devolucoes' as XX from tabela2 )T a seguinte query devolve a informação na seguinte posição: MarcaID Vendas Devolucoes -1 7 0,00 -1 0,00 3 No entanto eu desejava o seguinte: MarcaID Vendas Devolucoes -1 7 3 Como consigo ultrapassar este problema? Edited March 18, 2015 at 11:44 AM by ferreira12 Link to comment Share on other sites More sharing options...
Rechousa Posted March 18, 2015 at 11:57 AM Report Share #579709 Posted March 18, 2015 at 11:57 AM Viva, Acredito que a solução para o teu problema passe por usares os statements PIVOT e/ou UNPIVOT. Vê aqui uma explicação ou pesquisa no google. Pedro Martins Sharing is Knowledge! http://www.linkedin.com/in/rechousa Link to comment Share on other sites More sharing options...
ferreira12 Posted March 18, 2015 at 04:58 PM Author Report Share #579736 Posted March 18, 2015 at 04:58 PM Não estou a conseguir dá a seguinte mensagem Incorrect syntax near 'UNPIVOT'. You may need to set the compatibility level of the current database to a higher value to enable this feature. See help for the SET COMPATIBILITY_LEVEL option of ALTER DATABASE. Link to comment Share on other sites More sharing options...
Rechousa Posted March 18, 2015 at 05:10 PM Report Share #579738 Posted March 18, 2015 at 05:10 PM Qual a versão do SQL Server que estás a usar? Using SQL Server Management Studio To view or change the compatibility level of a database After connecting to the appropriate instance of the SQL Server Database Engine, in Object Explorer, click the server name. Expand Databases, and, depending on the database, either select a user database or expand System Databases and select a system database. Right-click the database, and then click Properties. The Database Properties dialog box opens. In the Select a page pane, click Options. The current compatibility level is displayed in the Compatibility level list box. To change the compatibility level, select a different option from the list. The choices are SQL Server 2008 (100), SQL Server 2012 (110), or SQL Server 2014 (120). Mais info aqui: https://msdn.microsoft.com/en-us/library/bb933794.aspx Pedro Martins Sharing is Knowledge! http://www.linkedin.com/in/rechousa Link to comment Share on other sites More sharing options...
ferreira12 Posted March 18, 2015 at 10:33 PM Author Report Share #579781 Posted March 18, 2015 at 10:33 PM Estou a utilizar a Microsoft SQL Server 2008 R2 (RTM) - 10.50.1617.0 (X64) nao dá para fazer sem utilizar os pivot? Link to comment Share on other sites More sharing options...
Rechousa Posted March 18, 2015 at 10:54 PM Report Share #579782 Posted March 18, 2015 at 10:54 PM Verifica o compatibility level da tua base de dados. Deve estar em SQL Server 2000, quando deveria ser SQL Server 2008. Pedro Martins Sharing is Knowledge! http://www.linkedin.com/in/rechousa Link to comment Share on other sites More sharing options...
ferreira12 Posted March 18, 2015 at 11:18 PM Author Report Share #579788 Posted March 18, 2015 at 11:18 PM Rechousa não estou a conseguir... não existe forma de fazer com os case? Link to comment Share on other sites More sharing options...
Rechousa Posted March 18, 2015 at 11:22 PM Report Share #579790 Posted March 18, 2015 at 11:22 PM Bem, podes sempre somar os valores das linhas. Serve? SELECT Marca, SUM(Vendas) AS Vendas, SUM(Devolucoes) AS Devolucoes FROM ( Select Marca, case when XX = 'Vendas' then Valor else '' end Vendas, case when XX = 'Devolucoes' then Valor else '' end Devolucoes from ( select MArca,SUM(valor) as Valor, 'Vendas' as XX from tabela1 group by MarcaID union select Marca,SUM(valor) as Valor, 'Devolucoes' as XX from tabela2 )T ) T GROUP BY Marca; Pedro Martins Sharing is Knowledge! http://www.linkedin.com/in/rechousa Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now