Si8
Si8

Reputation: 9235

How to SUM the values in a column in SQL

I have the following query in SQL:

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

Which produces this:

enter image description here

How can I write a query which will add each column and insert a new row under the [MYTABLE]

Something like this:

enter image description here

Upvotes: 0

Views: 79

Answers (3)

skeepa
skeepa

Reputation: 26

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

Upvotes: 1

pquest
pquest

Reputation: 3290

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.

Upvotes: 1

bjnr
bjnr

Reputation: 3437

Use UNION operator:

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]

Upvotes: 1

Related Questions