简体   繁体   English

如何对SQL列中的值求和

[英]How to SUM the values in a column in SQL

I have the following query in SQL: 我在SQL中有以下查询:

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

Which produces this: 产生此:

在此处输入图片说明

How can I write a query which will add each column and insert a new row under the [MYTABLE] 如何编写查询,该查询将添加每列并在[MYTABLE]下插入新行

Something like this: 像这样:

在此处输入图片说明

Use UNION operator: 使用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]

This doesn't do EXACTLY what you asked for, but this should be close enough: 这并不能完全满足您的要求,但这应该足够接近:

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

This will post the sums of those columns as a second dataset, rather than appending it to the end of that one. 这会将这些列的总和作为第二个数据集发布,而不是将其附加到该数据集的末尾。

you can try this to do it in a single statement: 您可以在单个语句中尝试执行以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM