-1
Is it possible to do a row of TOTALS per column? The explanation of totals per row was fantastic and it turned out beautiful in my project. Thank you.
My code:
SELECT
ano AS Ano,
CCUSTOS AS [Centro custos],
coalesce([1], 0) janeiro,
coalesce([2], 0) fevereiro,
coalesce([3], 0) março,
coalesce([4], 0) abril,
coalesce([5], 0) maio,
coalesce([6], 0) junho,
coalesce([7], 0) julho,
coalesce([8], 0) agosto,
coalesce([9], 0) setembro,
coalesce([10], 0) outubro,
coalesce([11], 0) novembro,
coalesce([12], 0) dezembro,
coalesce([1],0)+ coalesce([2],0)+ coalesce([3],0)+ coalesce([4],0)+ coalesce([5],0)+ coalesce([6],0)+ coalesce([7],0)+ coalesce([8],0)+ coalesce([9],0)+ coalesce([10],0)+ coalesce([11],0)+ coalesce([12],0) AS Total
FROM V_despesas_group_CC
PIVOT (SUM(total_valor) FOR MES IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12]))P
ORDER BY CCUSTOS
@Rovannlinhalis eventually replied that it is possible, but as you said, I couldn’t see pivot.. I’ve been sending the xD gambiarra
– rLinhares
in hr q I saw the answer until deleted the comment... will q is possible and I who did not know... then I went to read the answer.... rsrsrs but then, the way you did it is still another way, and will not put the result in the column itself...will generate other columns with repeated values in all rows.... Coming back... not at anchor, maybe I’d use Union, but I’d have to do a little snitching to make sure the line stayed at the end. Depending on how and where you are using the query, you would do this by code...
– Rovann Linhalis
ah, I get it.. I still think the ideal would be to treat this total in the code, but let the author pronounce.
– rLinhares
SQL 2008 and in PHP I am creating an Excel file with the contents of PIVOT. I’ll show you what I’ve done :) $filename = "List_default_ccusto"; $file_ending = "xls"; header('Content-Type: application/vnd.ms-excel'); header("Content-Disposition: Attachment; filename=$filename. $file_ending"); header("Pragma: no-cache"); header("Expires: 0"); $stmt = mssql_init('[passes]. [dbo]. [sp_pivot_expenses]'); $result = mssql_execute($stmt); $Sep = "t"; echo '<table border="1">'; $header = false;
– Fernanda Martins
while ($Row = mssql_fetch_assoc($result)) { if (!$header) { // Table header echo '<thead>'; echo '<tr>'; while($field = mssql_fetch_field($result)){ echo '<th>' . $field->name . '</th>'; } echo '</tr>'; echo '</thead>'; $header = true; }
– Fernanda Martins
echo '<tbody>'; echo '<tr>'; foreach($Row as $value) { if(!isset($value)){ $value = "0"; } $value = str_replace(".", ",", $value); echo '<td>' . $value . '</td>'; } echo '</tr>';
– Fernanda Martins
I forgot to say that it is a Stored Procedure that I made to be called in PHP ...
– Fernanda Martins