Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 516345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:43:18+00:00 2026-05-13T07:43:18+00:00

I have a table in SQL Server 2008 that looks kind of like this:

  • 0

I have a table in SQL Server 2008 that looks kind of like this:

ID   I1  I2 ...  IN
-------------------------
1    2   3  ..... 2
2    0   0  ..... 0 
3    2   1  ..... 5

Where IN is about 9 columns. What I need to do is count the number of rows, but skipping rows where the values of I1..IN are 0’s. I’m new to SQL and I have basically something like this:

SELECT COUNT(ID) AS Expr1, 
       COUNT(I1) AS Expr2, 
       COUNT(I2) AS Expr3, 
       COUNT(IN) AS ExprN 
 FROM [mytable] 
WHERE (Expr2 !=0) 
  AND (Expr3 != 0) 
  AND (ExprN != 0)

I imagine there is an easier and more efficient way of doing this? I need to ensure that all of the column entries are 0 (other than the ID). I would prefer not to rely on a single column being 0 or not to make the determination. I working with a database somebody already created and these 0’s should have been NULLS.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T07:43:19+00:00Added an answer on May 13, 2026 at 7:43 am
    SELECT COUNT(ID) AS Expr1, 
           COUNT(I1) AS Expr2, 
           COUNT(I2) AS Expr3, 
           COUNT(IN) AS ExprN 
     FROM [mytable] 
    WHERE (I1 + I2 + I3 + I4 + [... +] IN) <> 0
    

    … but I’m still not sure this meets the problem you’re trying to solve. COUNT(I1) will return the same as COUNT(I2) because they’ll be non-NULL in all the rows. Are you trying to find the number of rows with non-zero values?

    SELECT COUNT(ID) AS Expr1,
           SUM(CASE WHEN I1 = 0 THEN 0 ELSE 1 END) as Expr2,
           SUM(CASE WHEN I2 = 0 THEN 0 ELSE 1 END) as Expr3,
           SUM(CASE WHEN IN = 0 THEN 0 ELSE 1 END) as ExprN,
     FROM [mytable] 
    WHERE (I1 + I2 + I3 + I4 + [... +] IN) <> 0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've a large (1TB) table in SQL Server 2008 that looks something like this:
I have a couple of tables in a SQL 2008 server that I need
I have a table that looks like this: Month Site Val 2009-12 Microsoft 10
We have an interesting table query (SQL Server 2008) that fails with a different
I have a table in SQL server that has the normal tree structure of
I have a table in SQL Server 2005 which has three columns: id (int),
I have a table in a SQL Server 2005 database with a trigger that
I have a History Table in SQL Server that basically tracks an item through
On MS SQL Server 2005, I have a table that I want to replace.
I have SQL Server 2008 database with two tables. The first table is called

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.