im using sql server 2005.
I have these tables:
CREATE TABLE [dbo].[Proyectos2](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Proyecto] [varchar](255) NULL
)
CREATE TABLE [dbo].[Clientes2](
[Vendedora] [varchar](255) NULL,
[Proyecto] [varchar](255) NULL )
insert into proyectos2
values
(
'Proyecto1'
)
insert into proyectos2
values
(
'Proyecto2'
)
insert into clientes2
values
(
'Jessica','Proyecto1'
)
insert into clientes2
values
(
'Jessica','Proyecto1'
)
insert into clientes2
values
(
'Mariel','Proyecto2'
)
then this query:
Declare @Names As Varchar(Max), @strSQL Varchar(Max);
DECLARE @sum AS varchar(MAX);
Select @Names = Stuff((Select ',' + QuoteName(Proyecto)
From Proyectos2 Group By Proyecto Order by Proyecto For XML Path('')), 1, 1, '');
Select @sum = Stuff(
(
Select ', 1.0 * SUM(' + QuoteName(Proyecto) + ') as ' + QuoteName(Proyecto)
From Proyectos2
Group By Proyecto
Order by Proyecto For XML Path('')
), 1, 1, '') + ', SUM(Total) as Total';
Set @strSQL = 'select case when grouping(Vendedora) = 1 then ''Grand Total'' else Vendedora end as Vendedora, ' + @sum + N'
from (
Select Vendedora, ' + @Names + ', (' + REPLACE(@Names, '],[', '] + [') + ') as Total' +
' From (Select Vendedora, Proyecto From Clientes2) as P
Pivot (Count(Proyecto) For Proyecto in (' + @Names + ')) As Pvt
) as S
group by
Vendedora
with rollup';
SET @strSQL = 'WITH CTE AS ( ' + @strSQL + ' ) ' +
'SELECT Vendedora,cast(floor(Proyecto1) as varchar) Proyecto1,cast(floor(Proyecto2) as varchar)Proyecto2,Total FROM CTE UNION ALL ' +
'SELECT ''Porcentaje'',cast( ' + REPLACE(@Names, '],[', '] / Total as varchar) Proyecto1, cast([') + ' / Total as varchar), ''100''' +
' FROM CTE ' +
' WHERE Vendedora = ''Grand Total'';';
print @strSQL;
Execute (@strSQL);
GO
If you execute this code, this is displayed:
Vendedora------Proyecto1--------------Proyecto2-----Total
Jessica-------------2-------------------0--------------2
Mariel--------------0-------------------1--------------1
Grand Total---------2-------------------1--------------3
Percentage----------0.666666666666------0.333333333333---100
I would like something like this:
Vendedora------Proyecto1--------------Proyecto2-----Total
Jessica-------------2-------------------0--------------0.66
Mariel--------------0-------------------1--------------0.33
Grand Total---------2-------------------1--------------1
Percentage----------0.66--------------0.33------------------100
That is, two digits after the decimal for the last row, and also for the total of each row.
Is there a way to do this?
Thanks.
I’m focusing on your saying that you need the total of each row to be a percentage of the final grand total.
In order to do that you’re going to need to know what the grand total is before producing each line. You really would be best off doing that in a reporting tool, such as SSRS, ActiveReports, or Crystal Reports.
But since you asked: this is the approach I would take:
ETA: rewritten to be more specific. NOTE that this is still an example and needs to be adapted for your situation.
But this is very messy indeed: your best bet if possible is to just return the data and do the totaling and percentage-calculations in a reporting tool.