Solved

query syntax

Posted on 2014-09-15
6
114 Views
Last Modified: 2014-09-17
Hello,

Not really sure how Counts, sum and procents work.

I have a table with a few rows like this

id   x1       x2
1    null     0
2    1         1
3    1         null
4    0         null

Open in new window


where i am looking for the procentage of rows that have a 1 in the x1 and x2 columns.  Null rows should not be included.

so x1 procent should be 66% and x2 should be 50%

I am having trouble because i am not sure if there is a standard function that will do this, or what the most efficient way would be if one does not exist.

Its important that it is efficient because we are talking about a lot of rows and many more columns.

/thanks
0
Comment
Question by:soozh
[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
  • 3
  • 3
6 Comments
 
LVL 49

Expert Comment

by:PortletPaul
ID: 40324427
SELECT
      COUNT(CASE WHEN x1 = 1 THEN 1 END)                   AS x1_1
    , COUNT(x1)                                            AS x1
    , CASE WHEN COUNT(x1) > 0 THEN 
           COUNT(CASE WHEN x1 = 1 THEN 1 END) * 100.0 / COUNT(x1)
           ELSE 0 
      END                                                  AS x1_pct
    , COUNT(CASE WHEN x2 = 1 THEN 1 END)                   AS x2_1
    , COUNT(x2)                                            AS x2
    , CASE WHEN COUNT(x2) > 0 THEN 
           COUNT(CASE WHEN x2 = 1 THEN 1 END) * 100.0 / COUNT(x2)
           ELSE 0
      END                                                 AS x2_pct
FROM table1
;

Open in new window

The aggregate function COUNT()  will only count non-null values (in other words it ignores nulls)
So the result of the query above is:
| X1_1 | X1 |          X1_PCT | X2_1 | X2 | X2_PCT |
|------|----|-----------------|------|----|--------|
|    2 |  3 | 66.666666666666 |    1 |  2 |     50 |

Open in new window

But do remember that divide by zero isn't allowed so if COUNT(X1) is zero you cannot attempt the percentage calculation

see: http://sqlfiddle.com/#!3/439ed/1
0
 
LVL 51

Expert Comment

by:Vitor Montalvão
ID: 40324790
soozh, check if this works and isn't slow:
select (count(x1)*1.0 / count(1))*100 as PercentX1, (count(x2)*1.0 / count(1))*100 as PercentX2
from YourTableNameHere

Open in new window

0
 
LVL 49

Assisted Solution

by:PortletPaul
PortletPaul earned 300 total points
ID: 40324801
In case it isn't obvious, I included more columns than you requested so you could follow the calculations. The unwanted columns can be removed. e.g.
SELECT
      CASE WHEN COUNT(x1) > 0 THEN 
           COUNT(CASE WHEN x1 = 1 THEN 1 END) * 100.0 / COUNT(x1)
           ELSE 0 
      END                                                  AS x1_pct
    , CASE WHEN COUNT(x2) > 0 THEN 
           COUNT(CASE WHEN x2 = 1 THEN 1 END) * 100.0 / COUNT(x2)
           ELSE 0
      END                                                 AS x2_pct
FROM table1
;

Open in new window

0
Do you have a plan for Continuity?

It's inevitable. People leave organizations creating a gap in your service. That's where Percona comes in.

See how Pepper.com relies on Percona to:
-Manage their database
-Guarantee data safety and protection
-Provide database expertise that is available for any situation

 
LVL 51

Expert Comment

by:Vitor Montalvão
ID: 40324810
PortletPaul, why are you not counting the zeros?
The idea is not to return the percentage of rows that doesn't have NULL value for each column? Or I misunderstood?
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 40324864
because that is required, and non-null values of x1 or x2 are to be counted.

>>"so x1 procent should be 66% and x2 should be 50%"


X1 has 3 non-null values, 2 of those = 1 hence: 2/3 = 66%
x2 has 2 non-null values, 1 of those = 1 hence: 1/2 = 50%

you are counting all records through count(1) [ it's the same as count(*)] which is 4
you are then counting x1 or x2

so you get:
3 values of x1 / 4 rows = 75%
2 values of x2 / 4 rows = 50%

try it here: http://sqlfiddle.com/#!3/439ed/2
0
 
LVL 51

Accepted Solution

by:
Vitor Montalvão earned 200 total points
ID: 40324975
Ok. I can see that I misunderstood the question.
The only way to simplify your query is if the values are only 0 and 1, so you can SUM them instead of counting:
select (sum(x1)*1.0 / count(1))*100 as PercentX1, (sum(x2)*1.0 / count(1))*100 as PercentX2
from YourTableNameHere

Open in new window

0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

626 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