如何在SQL中获取列的总和

时间:2014-07-30 19:31:01

标签: sql-server sum

我有一个问题需要对列进行总结并执行以下操作: http://puu.sh/axKnr/12d3b2c2eb.png

我有一个带有枢轴的查询将信息按月放置,但我不知道如何注入最后一行并总结列,我希望任何人都可以帮助我。

SELECT
  Programador,
  ISNULL([1], 0) AS Enero,
  ISNULL([2], 0) AS Febrero,
  ISNULL([3], 0) AS Marzo,
  ISNULL([4], 0) AS Abril,
  ISNULL([5], 0) AS Mayo,
  ISNULL([6], 0) AS Junio,
  ISNULL([7], 0) AS Julio,
  ISNULL([8], 0) AS Agosto,
  ISNULL([9], 0) AS Septiembre,
  ISNULL([10], 0) AS Octubre,
  ISNULL([11], 0) AS Noviembre, 
  ISNULL([12], 0) AS Diciembre
FROM
(
  SELECT 
      u.Nombre as Programador, 
      MONTH(FechaCreacion) AS Mes, CAST(DATEDIFF(MINUTE, p.FechaInicio, p.FechaFin) / 60.0 AS DECIMAL(5,1)) as Duracion
  FROM Programaciones p
  INNER JOIN Usuarios u on u.Nombre = p.Programador
  WHERE p.Accion = 'Programada' and u.Rol = 'Programador'
) AS t
PIVOT (
  SUM(Duracion)
  FOR Mes IN([1], [2], [3], [4], [5],[6],[7],[8],[9],[10],[11],[12])
) as p
ORDER BY p.Programador

1 个答案:

答案 0 :(得分:1)

您可以使用GROUP BY WITH ROLLUP。您可能需要查看grouping_id。 http://msdn.microsoft.com/en-us/library/bb510624%28v=sql.105%29.aspx

或者您可以使用COMPUTE,但这会在单独的网格中返回。

编辑:重新阅读有关此文档的文档。 COMPUTEWITH ROLLUP将在未来版本中删除(对于Rollup而言超出SQL2014,对于COMPUTE则超出2008r2)。请改用ROLLUP

使用GROUP BY WITH ROLLUP(我认为这是正确的语法)

SELECT
    Programador,
    SUM(ISNULL([1], 0)) AS Enero,
    SUM(ISNULL([2], 0)) AS Febrero,
    SUM(ISNULL([3], 0)) AS Marzo,
    SUM(ISNULL([4], 0)) AS Abril,
    SUM(ISNULL([5], 0)) AS Mayo,
    SUM(ISNULL([6], 0)) AS Junio,
    SUM(ISNULL([7], 0)) AS Julio,
    SUM(ISNULL([8], 0)) AS Agosto,
    SUM(ISNULL([9], 0)) AS Septiembre,
    SUM(ISNULL([10], 0)) AS Octubre,
    SUM(ISNULL([11], 0)) AS Noviembre, 
    SUM(ISNULL([12], 0)) AS Diciembre,
    GROUPING(Programador) AS 'Grouping'
FROM
(
  SELECT 
      u.Nombre as Programador, 
      MONTH(FechaCreacion) AS Mes, CAST(DATEDIFF(MINUTE, p.FechaInicio, p.FechaFin) / 60.0 AS DECIMAL(5,1)) as Duracion
  FROM Programaciones p
  INNER JOIN Usuarios u on u.Nombre = p.Programador
  WHERE p.Accion = 'Programada' and u.Rol = 'Programador'
) AS t
PIVOT (
  SUM(Duracion)
  FOR Mes IN([1], [2], [3], [4], [5],[6],[7],[8],[9],[10],[11],[12])
) as p
GROUP BY Programador with Rollup
ORDER BY p.Programador

使用COMPUTE

SELECT
  Programador,
  ISNULL([1], 0) AS Enero,
  ISNULL([2], 0) AS Febrero,
  ISNULL([3], 0) AS Marzo,
  ISNULL([4], 0) AS Abril,
  ISNULL([5], 0) AS Mayo,
  ISNULL([6], 0) AS Junio,
  ISNULL([7], 0) AS Julio,
  ISNULL([8], 0) AS Agosto,
  ISNULL([9], 0) AS Septiembre,
  ISNULL([10], 0) AS Octubre,
  ISNULL([11], 0) AS Noviembre, 
  ISNULL([12], 0) AS Diciembre

FROM
(
  SELECT 
      u.Nombre as Programador, 
      MONTH(FechaCreacion) AS Mes, CAST(DATEDIFF(MINUTE, p.FechaInicio, p.FechaFin) / 60.0 AS DECIMAL(5,1)) as Duracion
  FROM Programaciones p
  INNER JOIN Usuarios u on u.Nombre = p.Programador
  WHERE p.Accion = 'Programada' and u.Rol = 'Programador'
) AS t
PIVOT (
  SUM(Duracion)
  FOR Mes IN([1], [2], [3], [4], [5],[6],[7],[8],[9],[10],[11],[12])
) as p
ORDER BY p.Programador
COMPUTE SUM(Enero),
SUM(Febrero),
SUM(Marzo),
SUM(Abril),
SUM(Mayo),
SUM(Junio),
SUM(Julio),
SUM(Agosto),
SUM(Septiembre),
SUM(Octubre),
SUM(Noviembre),
SUM(Diciembre)