如何在SQL中对值中的值进行求和

时间:2014-04-10 17:58:21

标签: sql sql-server

我在SQL中有以下查询:

SELECT TOP 1000 [Date]
      ,[APDERM]
      ,[IN HOUSE]
      ,[RAD EMR ORDERS]
      ,[EMR ORDERS]
  FROM [database].[dbo].[MYTABLE]

产生这个:

enter image description here

如何编写一个查询,该查询将添加每列并在[MYTABLE]

下插入新行

这样的事情:

enter image description here

3 个答案:

答案 0 :(得分:1)

使用UNION运算符:

SELECT [Date]
      ,[APDERM]
      ,[IN HOUSE]
      ,[RAD EMR ORDERS]
      ,[EMR ORDERS]
FROM [database].[dbo].[MYTABLE]
UNION
SELECT  [Date]
      , SUM([APDERM])
      , SUM([IN HOUSE])
      , SUM([RAD EMR ORDERS])
      , SUM([EMR ORDERS](
  FROM [database].[dbo].[MYTABLE]

答案 1 :(得分:1)

这并不完全符合您的要求,但这应该足够接近:

SELECT TOP 1000 [Date]
      ,[APDERM]
      ,[IN HOUSE]
      ,[RAD EMR ORDERS]
      ,[EMR ORDERS]
  FROM [database].[dbo].[MYTABLE]
COMPUTE SUM(APDERM), SUM([In House]), SUM([RAD EMR ORDERS]), SUM([EMR ORDERS])

这会将这些列的总和作为第二个数据集发布,而不是将其附加到该数据集的末尾。

答案 2 :(得分:1)

您可以尝试在单个语句中执行此操作:

SELECT TOP 1000 [Date]
      ,SUM([APDERM]) AS [APDERM]
      ,SUM([IN HOUSE]) AS [IN HOUSE]
      ,SUM([RAD EMR ORDERS]) AS [RAD EMR ORDERS]
      ,SUM([EMR ORDERS]) AS [EMR ORDERS]
FROM [database].[dbo].[MYTABLE]
GROUP BY [Date]
WITH ROLLUP