Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# SQL:  how to count cells accross columns?

Posted on 2012-03-18
Medium Priority
312 Views
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
Question by:arthurh88
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 3

LVL 93

Expert Comment

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
``````
0

LVL 13

Accepted Solution

Ashok earned 2000 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

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

ID: 37736180
that worked great.  thank you
0

LVL 13

Expert Comment

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
###### Suggested Courses
Course of the Month5 days, 16 hours left to enroll