Solved

SQL:  how to count cells accross columns?

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

895 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