Solved

Get number of columns with data

Posted on 2004-09-21
10
320 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
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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Creating and Managing Databases with phpMyAdmin in cPanel.
Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
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…

839 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