x
login about faq Site discussion (meta-askssc)

Count all data in table

I am trying to count all my errors but cant figure out the best way of doing this. I have 25 columns of different errors anything that is not null is an error. I want to count everything that is not null ending with one total for each dept sql code based on 2 columns of errors is below. any advice on this would be great many thanks in advance

SELECT 
office,([Agg_Error]+[cover_Error]) Total_Errors
FROM(SELECT Placing_Office OFFICE,
COUNT([Aggregates_Error])[Agg_Error],
COUNT([Basisofcover_Error])[cover_Error]
FROM [LEE].[dbo].[ERRORS]
GROUP BY Placing_Office)A
more ▼

asked Nov 21 '12 at 12:00 PM in Default

n3w2sql gravatar image

n3w2sql
730 5 11 19

(comments are locked)
10|1200 characters needed characters left

2 answers: sort voted first

Are the error columns numeric?

If not, try something like

select 
 Placing_Office as Office,
 sum(case when [Aggregates_Error] is not null then 1 else 0 end) as [Agg_Error],
 sum(case when [Basisofcover_Error] is not null then 1 else 0 end) as [Cover_Error]
from
 [LEE].[dbo].[ERRORS]
group by Placing_Office

as the inner query

more ▼

answered Nov 21 '12 at 12:09 PM

Kev Riley gravatar image

Kev Riley ♦♦
46.1k 38 43 69

(comments are locked)
10|1200 characters needed characters left

I would recommend sticking with COUNT, as it will filter out the NULL values automatically. If you don't need any additional joins, you could do this without a subquery.

SELECT
Placing_Office,
  COUNT([Aggregates_Error]) +
  COUNT([Basisofcover_Error]) AS TotalErrors
FROM [LEE].[dbo].[ERRORS]
GROUP BY Placing_Office

This way you wouldn't need to add/remove columns in two places if you needed to make a change.

more ▼

answered Nov 21 '12 at 09:36 PM

Beandon10 gravatar image

Beandon10
138 3

(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments



Facebook logo Follow Ask SSC on Facebook
Find Ask SSC on Google+
linkedin logo Find us on LinkedIn

Topics:

x1601

asked: Nov 21 '12 at 12:00 PM

Seen: 228 times

Last Updated: Nov 23 '12 at 01:45 AM

Copyright © 2002-2012 Simple Talk Publishing. All Rights Reserved. If you have any queries, please contact the site administrators.
Ask SQL Server Central is a community service provided by Red Gate.