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
5
Medium Priority
?
312 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
[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
  • Learn & ask questions
  • 3
5 Comments
 
LVL 93

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

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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

688 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