Solved

Get number of columns with data

Posted on 2004-09-21
10
326 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Teradata converting character to integer 2 31
Merging spreadsheets 8 67
Help With Simple Database Design 7 59
SQL syntax question 6 44
Creating and Managing Databases with phpMyAdmin in cPanel.
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
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…

726 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