我在SQL中有以下查询:
SELECT TOP 1000 [Date]
,[APDERM]
,[IN HOUSE]
,[RAD EMR ORDERS]
,[EMR ORDERS]
FROM [database].[dbo].[MYTABLE]
产生这个:
如何编写一个查询,该查询将添加每列并在[MYTABLE]
这样的事情:
答案 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