如何对Pivoted Columns的值求和并将其添加到另一个Pivoted Column中

时间:2014-05-19 05:03:18

标签: sql-server sql-server-2008 pivot-table

我想总结所有 CountHours 并在旋转列总计中显示.Total是所有星期日,星期一...与特定用户名的总和。如何实现这一目标 ?

select FullName,Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Total
   from  
   (Select UserId_Fk,ISNULL(CAST(CountHours as decimal(18,2)),0)as CountHours,[Day] f     rom CheckInCheckOut)
    as convertedtable
    inner join Users
    on convertedtable.UserId_Fk=Users.UserId
 PIVOT
(
 SUM(CountHours) 
 FOR Day
 IN([Sunday],[Monday],[Tuesday],[Wednesday],[Thursday],[Friday],[Saturday],[Total])
)
as PivotTable

此查询的结果是:

enter image description here

表格结构:

  Table[CheckInCheckOut]     

 CheckInCheckOutId  int 
    UserId_Fk       int 
  CountHours    nvarchar(50)    
         Day    nvarchar(50)

示例将不胜感激。

2 个答案:

答案 0 :(得分:3)

您应该计算总列字段,即它不在透视列列表中。

数据

create table #CheckInCheckOut(Id int identity(1,1),UserId_Fk int,CountHours varchar(50),[Day] varchar(50))
INSERT INTO #CheckInCheckOut(UserId_Fk,CountHours,[Day]) VALUES
(1,'2','Sunday'),(1,'2','Monday'),(1,'2','Tuesday'),(1,'2','Wednesday'),(1,'2','Thursday'),(1,'2','Friday'),(1,'2','Saturday')
,(2,'3','Sunday'),(2,'3','Monday'),(2,'3','Tuesday'),(2,'3','Wednesday'),(2,'3','Thursday'),(2,'3','Friday'),(2,'3','Saturday')
,(3,'3','Sunday'),(3,'3','Monday'),(3,'3','Tuesday'),(3,'3','Wednesday'),(3,'3','Thursday'),(3,'3','Friday'),(3,'3','Saturday')


create table #Users(UserId int identity(1,1),FullName varchar(50))
INSERT #Users(FullName) values('Abdul'),('khan'),('Tariq')

查询以查找总数:

select FullName
,[Sunday] = SUM([Sunday])
,[Monday] = SUM([Monday])
,[Tuesday] = SUM([Tuesday])
,[Wednesday] = SUM([Wednesday])
,[Thursday] = SUM([Thursday])
,[Friday] = SUM([Friday])
,[Saturday] = SUM([Saturday])
, Total= SUM([Sunday]+[Monday]+[Tuesday]+[Wednesday]+[Thursday]+[Friday]+[Saturday])
   from  
   (Select UserId_Fk,ISNULL(CAST(CountHours as decimal(18,2)),0)as CountHours,[Day] 
   from #CheckInCheckOut)
    as convertedtable
    inner join #Users
    on convertedtable.UserId_Fk=#Users.UserId
 PIVOT
(
 SUM(CountHours) 
 FOR Day
 IN([Sunday],[Monday],[Tuesday],[Wednesday],[Thursday],[Friday],[Saturday])
)
as PivotTable
GROUP BY FullName

输出

enter image description here

如果你想要总水平和垂直两者,那么替换:

 --GROUP BY FullName
GROUP BY ROLLUP(FullName);

如需更多关注链接https://stackoverflow.com/a/17142530/1915855

DROP TABLE #CheckInCheckOut
DROP TABLE #Users

答案 1 :(得分:0)

尝试这种方式就是一个例子。

创建表

    CREATE TABLE cars
    (
     car_id tinyint,
     attribute varchar(20),
     value varchar(20),
     sumd decimal(18,2)
    )

向其中插入值

insert into cars(car_id, attribute, value, sumd)
values      (1, 'Make', 'VW',1),
            (1, 'Model', 'Rabbit',2),
            (1, 'Color', 'Gold',3),
            (2, 'Make', 'Jeep',4),
            (2, 'Model', 'Wrangler',5),
            (2, 'Color', 'Gray',6)

总计

declare @Columns2 VARCHAR(8000)
declare @Sql VARCHAR(4000)
declare @Columns VARCHAR(8000)

SET @Columns = substring((select distinct ',['+attribute+']' from cars group by attribute for xml path('')),2,8000)
    SET @Columns2 = substring((select distinct ',IsNull(['+attribute+'],0) as ['+attribute+']' from cars group by attribute for xml path('')),2,8000)
    print @Columns
    print @Columns2

SET @SQL = 'SELECT car_id, '+@Columns2+', total
                                        FROM 
                                                (Select car_id,attribute, SUM(sumd) OVER (PARTITION BY attribute) as total
                                                , sumd from cars)  SourceData
                                        PIVOT
                                                (sum(sumd) for attribute in ('+@Columns+')) pivottable 
                                        Order by car_id '


        exec(@sql)