Solved

SQL:  how to count cells accross columns?

Posted on 2012-03-18
5
301 Views
Last Modified: 2012-03-26
dbo.listings  has 25 columns
UserID is a primary key
For a userID, I want to select the total count of all cells that are not Null and have a Length of at least > 0.  Basically, I want to know for a userID, how many cells contain data (not interested in null or blank cells).

How can I do this?  
SO, say userID = 1000.  
Lets assume column names are simply Column1, Column2, etc.

How can I craft a statement that gets the count of how many columns have data for UserID 1000
0
Comment
Question by:arthurh88
  • 3
5 Comments
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 37735748
Assuming those other columns are text-based...

SELECT userID,
    CASE WHEN COALESCE(Column1, '') <> '' THEN 1 ELSE 0 END +
    CASE WHEN COALESCE(Column2, '') <> '' THEN 1 ELSE 0 END +
    CASE WHEN COALESCE(Column3, '') <> '' THEN 1 ELSE 0 END +
...
    CASE WHEN COALESCE(Column25, '') <> '' THEN 1 ELSE 0 END AS PopulatedColumns
FROM SomeTable
WHERE userID = 1000

Open in new window

0
 
LVL 13

Accepted Solution

by:
Ashok earned 500 total points
ID: 37735822
If all columns are of different types, following may work better.....

Try

SELECT userID,
    CASE WHEN IsNull(Column1, '') <> '' THEN 1 ELSE 0 END +
    CASE WHEN IsNull(Column2, '') <> '' THEN 1 ELSE 0 END +
    CASE WHEN IsNull(Column3, '') <> '' THEN 1 ELSE 0 END +
...
    CASE WHEN IsNull(Column25, '') <> '' THEN 1 ELSE 0 END AS PopulatedColumns
FROM SomeTable
WHERE userID = 1000

OR

SELECT userID,
Case Column1 When Null Then 1 Else 0 End +
Case Column2 When Null Then 1 Else 0 End +
Case Column3 When Null Then 1 Else 0 End +
....
Case Column21 When Null Then 1 Else 0 End As CountTotal
from SomeTable
where UserId = 1000
0
 
LVL 13

Expert Comment

by:Ashok
ID: 37735854
SELECT UserID,
CASE WHEN [Column1] IS NULL THEN 0 else 1 END
+ CASE WHEN [Column2] IS NULL THEN 0 else 1 END
+ CASE WHEN [Column3] IS NULL THEN 0 else 1 END
.....
+ CASE WHEN [Column25] IS NULL THEN 0 else 1 END AS CountTotal
from SomeTable
where UserId = 1000
0
 

Author Closing Comment

by:arthurh88
ID: 37736180
that worked great.  thank you
0
 
LVL 13

Expert Comment

by:Ashok
ID: 37766484
Typing mistake in above.....

SELECT userID,
Case Column1 When Null Then 0 Else 1 End +
Case Column2 When Null Then 0 Else 1 End +
Case Column3 When Null Then 0 Else 1 End +
....
Case Column21 When Null Then 0 Else 1 End As CountTotal
from SomeTable
where UserId = 1000
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how the fundamental information of how to create a table.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now