Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

postgresql - Compute percents from SUM() in the same SELECT sql query

In the table my_obj there are two integer fields:

(value_a integer, value_b integer);

I try to compute how many time value_a = value_b, and I want to express this ratio in percents. This is the code I have tried:

select sum(case when o.value_a = o.value_b then 1 else 0 end) as nb_ok,
       sum(case when o.value_a != o.value_b then 1 else 0 end) as nb_not_ok,
       compute_percent(nb_ok,nb_not_ok)
from  my_obj as o
group by o.property_name;

compute_percent is a stored_procedure that simply does (a * 100) / (a + b)

But PostgreSQL complains that the column nb_ok doesn't exist.
How would you do that properly ?

I use PostgreSQL 9.1 with Ubuntu 12.04.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There is more to this question than it may seem.

Simple version

This is much faster and simpler:

SELECT property_name
      ,(count(value_a = value_b OR NULL) * 100) / count(*) AS pct
FROM   my_obj
GROUP  BY 1;

Result:

property_name | pct
--------------+----
 prop_1       | 17
 prop_2       | 43

How?

  • You don't need a function for this at all.

  • Instead of counting value_b (which you don't need to begin with) and calculating the total, use count(*) for the total. Faster, simpler.

  • This assumes you don't have NULL values. I.e. both columns are defined NOT NULL. The information is missing in your question.
    If not, your original query is probably not doing what you think it does. If any of the values is NULL, your version does not count that row at all. You could even provoke a division-by-zero exception this way.
    This version works with NULL, too. count(*) produces the count of all rows, regardless of values.

  • Here's how the count works:

     TRUE  OR NULL = TRUE
     FALSE OR NULL = NULL
    

    count() ignores NULL values. Voilá.

  • Operator precedence governs that = binds before OR. You could add parentheses to make it clearer:

    count ((value_a = value_b) OR FALSE)
    
  • You can do the same with

    count NULLIF(<expression>, FALSE)
    
  • The result type of count() is bigint by default.
    A division bigint / bigint, truncates fractional digits.

Include fractional digits

Use 100.0 (with fractional digit) to force the calculation to be numeric and thereby preserve fractional digits.
You may want to use round() with this:

SELECT property_name
      ,round((count(value_a = value_b OR NULL) * 100.0) / count(*), 2) AS pct
FROM   my_obj
GROUP  BY 1;

Result:

property_name | pct
--------------+-------
 prop_1       | 17.23
 prop_2       | 43.09

As an aside:
I use value_a instead of valueA. Don't use unquoted mixed-case identifiers in PostgreSQL. I have seen too many desperate question coming from this folly. If you wonder what I am talking about, read the chapter Identifiers and Key Words in the manual.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...