Solved

SQL:  how to count cells accross columns?

Posted on 2012-03-18
5
309 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 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

631 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