0

我使用的是 sql server 2005。我有这些表:

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'
)

然后这个查询:

    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

如果执行此代码,则会显示:

Vendedora------Proyecto1--------------Proyecto2-----Total
Jessica-------------2-------------------0--------------2
Mariel--------------0-------------------1--------------1
Grand Total---------2-------------------1--------------3
Percentage----------0.666666666666------0.333333333333---100

我想要这样的东西:

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

也就是说,最后一行的小数点后两位数,以及每行的总数。

有没有办法做到这一点?

谢谢。

4

3 回答 3

0

我这样做了:

Declare @Names As Varchar(Max), @strSQL Varchar(Max), @PercentNames 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 @PercentNames = Stuff((Select ',' + 'CAST(' + QuoteName(Proyecto)  + '* 100.0/Total AS DECIMAL(10,2))'
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 *, CAST(Total * 100.0 / (select Total from cte where Vendedora = ''Grand Total'') as Decimal(10,2)) as [Percent]

              FROM CTE UNION ALL 
              SELECT ''Porcentaje'', ' + @PercentNames + ', ''100'', 100' +
              '  FROM CTE ' +
              ' WHERE Vendedora = ''Grand Total'';';

print @strSQL;
Execute (@strSQL);
GO

工作正常。

但我仍然有一个问题:我想删除小数点后的数字。在这种情况下我能做什么?

于 2012-04-17T16:44:08.050 回答
0

将您想要的值乘以百分比(如 0.33)乘以 100.0,然后将其转换为 int 以将值截断为不带小数位的百分比。

cast(percentage * 100.0 as int) as percentage
于 2012-04-16T18:35:53.747 回答
0

我专注于您的说法,即您需要每行的总数占最终总数的百分比。

为了做到这一点,您需要在生产每条线之前知道总计是多少。您最好在报告工具中执行此操作,例如 SSRS、ActiveReports 或 Crystal Reports。

但既然你问:这是我会采取的方法:

ETA:重写为更具体。请注意,这仍然是一个示例,需要根据您的情况进行调整。

WITH CTE AS ( <@strSQL> ),
     CTE2 AS (
       SELECT Vendedora, 
              cast(floor(Proyecto1) as varchar) Proyecto1,
              cast(floor(Proyecto2) as varchar) Proyecto2,
              Total 
       FROM   CTE),
       SELECT   Vendedora,
              Proyecto1,
              Proyecto2,
              Total / ( SELECT SUM(Total) FROM CTE2 ) Percentage
       FROM     CTE2    
       UNION  ALL
       SELECT 'Porcentaje',
               <your two columns>
               '100'
       FROM   CTE

但这确实很混乱:如果可能的话,最好的选择是只返回数据并在报告工具中进行总计和百分比计算。

于 2012-04-16T19:32:30.397 回答