Note that there are some explanatory texts on larger screens.

plurals
  1. POCounting if data exists in a row
    text
    copied!<p>Hey guys I have the below sample data which i want to query for.</p> <pre><code>MemberID AGEQ1 AGEQ2 AGEQ2 ----------------------------------------------------------------- 1217 2 null null 58458 3 2 null 58459 null null null 58457 null 5 null 299576 6 5 7 </code></pre> <p>What i need to do is to lookup the table and if any AGEx COLUMN contains any data then it counts the number of times there is data for that row in each column</p> <p>Results example:</p> <p>for memberID 1217 the count would be 1</p> <p>for memberID 58458 the count would be 2</p> <p>for memberID 58459 the count would be 0 or null</p> <p>for memberID 58457 the count would be 1</p> <p>for memberID 299576 the count would be 3</p> <p>This is how it should look like in SQL if i query the entire table</p> <p>1 Children - 2</p> <p>2 Children - 1</p> <p>3 Children - 1</p> <p>0 Children - 1</p> <p>So far i have been doing it using the following query which isnt very efficient and does give incorrect tallies as there are multiple combinations that people can answer the AGE question. Also i have to write multiple queries and change the is null to is not null depending on how many children i am looking to count a person has</p> <pre><code>select COUNT (*) as '1 Children' from Member where AGEQ1 is not null and AGEQ2 is null and AGEQ3 is null </code></pre> <p>The above query only gives me an answer of 1 but i want to be able to count the other columns for data as well</p> <p>Hope this is nice and clear and thank you in advance</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload