计算 SQL 服务器中动态列的按列总计
Calculate columnwise total for dynamic columns in SQL Server
我有以下输出,它是从以下存储过程生成的
SET @sql = '
SELECT * FROM (SELECT [NAMES],logdate,[worked time]
from tmp_phys_table GROUP BY NAMES,LOGDATE,[WORKED TIME]) AS [SubTable]
PIVOT
(
MAX([worked time])
FOR [logdate] IN ([' + @docDates + '])
) AS [Pivot] ;';
以上查询给出以下输出
现在我想按列进行总计,并在列的末尾包含一个名为总时间的新列,这些列是动态列,例如。可能有 3 列或 30 列。
应该是这样的:
SET @sql = '
SELECT *
FROM (SELECT [NAMES], logdate, [worked time], [Total Time]
FROM tmp_phys_table
GROUP BY NAMES,LOGDATE,[WORKED TIME], [Total Time]
) AS [SubTable]
PIVOT
(
MAX([worked time])
FOR [logdate] IN ([' + @docDates + '])
) AS [Pivot] ;';
我有以下输出,它是从以下存储过程生成的
SET @sql = '
SELECT * FROM (SELECT [NAMES],logdate,[worked time]
from tmp_phys_table GROUP BY NAMES,LOGDATE,[WORKED TIME]) AS [SubTable]
PIVOT
(
MAX([worked time])
FOR [logdate] IN ([' + @docDates + '])
) AS [Pivot] ;';
以上查询给出以下输出
现在我想按列进行总计,并在列的末尾包含一个名为总时间的新列,这些列是动态列,例如。可能有 3 列或 30 列。
应该是这样的:
SET @sql = '
SELECT *
FROM (SELECT [NAMES], logdate, [worked time], [Total Time]
FROM tmp_phys_table
GROUP BY NAMES,LOGDATE,[WORKED TIME], [Total Time]
) AS [SubTable]
PIVOT
(
MAX([worked time])
FOR [logdate] IN ([' + @docDates + '])
) AS [Pivot] ;';