Solved

Get number of columns with data

Posted on 2004-09-21
10
286 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
10 Comments
 
LVL 19

Expert Comment

by:NickUpson
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 12

Expert Comment

by:catchmeifuwant
Comment Utility
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
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 15

Expert Comment

by:jdlambert1
Comment Utility
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
Comment Utility
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 500 total points
Comment Utility
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
Comment Utility
Awesome as usual jdlambert1...thanks :)
0
 
LVL 15

Expert Comment

by:jdlambert1
Comment Utility
Glad I could help!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article describes some very basic things about SQL Server filegroups.
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

771 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

9 Experts available now in Live!

Get 1:1 Help Now