?
Solved

Get number of columns with data

Posted on 2004-09-21
10
Medium Priority
?
339 Views
Last Modified: 2012-06-21
I want to write a query that will tell me how many columns have data in it for each row.  

My columns are ID, DE6, DE16, DE26, DE36, DE46, DE56, DE66, DE76, DE86, DE96, DE106, DE116, DE126, DE136.

Thanks.
0
Comment
Question by:pansophy
[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
10 Comments
 
LVL 19

Expert Comment

by:NickUpson
ID: 12116952
select count(*) from table where id is not null
union
select count(*) from table where de6 is not null
union
... (etc)
0
 
LVL 15

Expert Comment

by:jdlambert1
ID: 12116989
SELECT CASE WHEN ID IS NOT NULL THEN 1 ELSE 0 END +
 CASE WHEN DE6 IS NOT NULL THEN 1 ELSE 0 END cnt +
 CASE WHEN DE16 IS NOT NULL THEN 1 ELSE 0 END cnt +
...
FROM TableA
0
 
LVL 15

Expert Comment

by:jdlambert1
ID: 12116996
Cut & paste errror. Try this:

SELECT CASE WHEN ID IS NOT NULL THEN 1 ELSE 0 END +
 CASE WHEN DE6 IS NOT NULL THEN 1 ELSE 0 END +
 CASE WHEN DE16 IS NOT NULL THEN 1 ELSE 0 END +
...
 CASE WHEN DE136 IS NOT NULL THEN 1 ELSE 0 END as Cnt
FROM TableA
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 12

Expert Comment

by:catchmeifuwant
ID: 12119844
select sum(decode(id,null,0,1)),sum(decode(de6,null,0,1)),sum(decode(de16,null,0,1))....
from mytable


0
 

Author Comment

by:pansophy
ID: 12126055
jdlambert1>  This works just fine, except I don't seem to be able to filter based on the value.  For instance, including only those rows with a count greater than 6.  When I enter >6 in the criteria, I get  "data type error in expression".

CASE WHEN ID >0 THEN 1 ELSE 0 END + CASE WHEN DE6 >0
                      THEN 1 ELSE 0 END + CASE WHEN DE16 >0 THEN 1 ELSE 0 END + CASE WHEN DE26 >0
                      THEN 1 ELSE 0 END + CASE WHEN DE36 >0 THEN 1 ELSE 0 END + CASE WHEN DE46 >0
                      THEN 1 ELSE 0 END + CASE WHEN DE56 >0 THEN 1 ELSE 0 END + CASE WHEN DE66 >0
                      THEN 1 ELSE 0 END + CASE WHEN DE76 >0 THEN 1 ELSE 0 END + CASE WHEN DE86 >0
                      THEN 1 ELSE 0 END + CASE WHEN DE96 >0 THEN 1 ELSE 0 END + CASE WHEN DE106 >0
                      THEN 1 ELSE 0 END + CASE WHEN DE116 >0 THEN 1 ELSE 0 END + CASE WHEN DE126 >0
                      THEN 1 ELSE 0 END + CASE WHEN DE136 >0 THEN 1 ELSE 0 END AS Cnt

0
 
LVL 15

Expert Comment

by:jdlambert1
ID: 12126539
I think I understand what you mean. Try this:

SELECT Cnt FROM (
SELECT CASE WHEN ID IS NOT NULL THEN 1 ELSE 0 END +
 CASE WHEN DE6 IS NOT NULL THEN 1 ELSE 0 END +
 CASE WHEN DE16 IS NOT NULL THEN 1 ELSE 0 END +
...
 CASE WHEN DE136 IS NOT NULL THEN 1 ELSE 0 END as Cnt
FROM TableA
) sub
WHERE Cnt > 6
0
 

Author Comment

by:pansophy
ID: 12126886
Can I do that as a subquery??  

Here is what the full query looks like minus the "Where cnt > 6":

SELECT     ID, CASE WHEN de6 > 0 OR
                      de16 > 0 OR
                      de26 > 0 THEN 1 ELSE 0 END AS hr72,
                      CASE WHEN ID > 0 THEN 1 ELSE 0 END + CASE WHEN DE6 > 0 THEN 1 ELSE 0 END + CASE WHEN DE16 > 0 THEN 1 ELSE 0 END + CASE WHEN DE26
                       > 0 THEN 1 ELSE 0 END + CASE WHEN DE36 > 0 THEN 1 ELSE 0 END + CASE WHEN DE46 > 0 THEN 1 ELSE 0 END + CASE WHEN DE56 > 0 THEN 1 ELSE
                       0 END + CASE WHEN DE66 > 0 THEN 1 ELSE 0 END + CASE WHEN DE76 > 0 THEN 1 ELSE 0 END + CASE WHEN DE86 > 0 THEN 1 ELSE 0 END + CASE
                       WHEN DE96 > 0 THEN 1 ELSE 0 END + CASE WHEN DE106 > 0 THEN 1 ELSE 0 END + CASE WHEN DE116 > 0 THEN 1 ELSE 0 END + CASE WHEN DE126
                       > 0 THEN 1 ELSE 0 END + CASE WHEN DE136 > 0 THEN 1 ELSE 0 END AS cnt
FROM         dbo.tblTemp
WHERE     (CASE WHEN de6 > 0 OR
                      de16 > 0 OR
                      de26 > 0 THEN 1 ELSE 0 END = 1)
0
 
LVL 15

Accepted Solution

by:
jdlambert1 earned 2000 total points
ID: 12126994
Yes, it can be a subquery. I've mixed your code and mine and tweaked your WHERE clause a little bit:

SELECT * FROM (
 SELECT ID,
  CASE WHEN de6 > 0 OR de16 > 0 OR de26 > 0 THEN 1 ELSE 0 END AS hr72,
  CASE WHEN ID > 0 THEN 1 ELSE 0 END +
  CASE WHEN DE6 > 0 THEN 1 ELSE 0 END +
  CASE WHEN DE16 > 0 THEN 1 ELSE 0 END +
  CASE WHEN DE26 > 0 THEN 1 ELSE 0 END +
  CASE WHEN DE36 > 0 THEN 1 ELSE 0 END +
  CASE WHEN DE46 > 0 THEN 1 ELSE 0 END +
  CASE WHEN DE56 > 0 THEN 1 ELSE 0 END +
  CASE WHEN DE66 > 0 THEN 1 ELSE 0 END +
  CASE WHEN DE76 > 0 THEN 1 ELSE 0 END +
  CASE WHEN DE86 > 0 THEN 1 ELSE 0 END +
  CASE WHEN DE96 > 0 THEN 1 ELSE 0 END +
  CASE WHEN DE106 > 0 THEN 1 ELSE 0 END +
  CASE WHEN DE116 > 0 THEN 1 ELSE 0 END +
  CASE WHEN DE126 > 0 THEN 1 ELSE 0 END +
  CASE WHEN DE136 > 0 THEN 1 ELSE 0 END AS cnt
 FROM dbo.tblTemp
 WHERE de6 > 0 OR de16 > 0 OR de26 > 0
) sub
WHERE Cnt > 6
0
 

Author Comment

by:pansophy
ID: 12127307
Awesome as usual jdlambert1...thanks :)
0
 
LVL 15

Expert Comment

by:jdlambert1
ID: 12127541
Glad I could help!
0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
In this article, I’ll look at how you can use a backup to start a secondary instance for MongoDB.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

650 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