Counting Number of Users Whose Average is Greater than X in Postgres

Ecognium

I am trying to find out the number of users who have scored an average of 80 or higher. I am using Having in my query but it is not returning the count of number of rows.

The Schema looks like:

Results

user
test_no
question_no
score

My Query:

SELECT "user" FROM results WHERE (score >0) GROUP BY "user" 
        HAVING (sum(score) / count(distinct(test_no))) >= 80;

I get:

user
2
4
8
(3 rows)

Instead I would like to get 3 (number of rows) as the output. If I do count("user"), I get the count of number of tests for each user.

I understand this is related to use Group By but I need it for my Having clause. Any suggestions how I can do this is appreciated.

Update: Here is some sample data: http://pastebin.com/k1nH5Wzh (-1 means unanswered)

Thanks!

Erwin Brandstetter

The query you found is good. Some minor simplifications:

SELECT count(*) AS ct
FROM  (
   SELECT 1
   FROM   result
   WHERE  score > 0
   GROUP  BY user_id
   HAVING (sum(score) / count(DISTINCT test_no)) >= 80
   ) sub
  • DISTINCT does not require parentheses.

  • You can SELECT a constant value in the subquery. The value is irrelevant, since you are only going to count the rows. Slightly shorter and cheaper.

Don't use the reserved word user as column name. That's asking for trouble. I am using user_id instead.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Number of Subarray whose sum greater than given value

From Dev

counting the number of values greater than 0 in R in multiple columns

From Dev

Finding a number greater than x in a range

From Dev

diff - if file contains a number greater than x, then

From Dev

Return number in array that is greater than X

From Dev

diff - if file contains a number greater than x, then

From Dev

Return number in array that is greater than X

From Dev

I am trying to list all the tutor names whose salary is greater than the average salary and show how much the salary is greater than by

From Dev

Postgres greater than or null

From Dev

Postgres greater than or null

From Dev

How to find elements whose sibling index is less than x and greater than y

From Dev

average if greater than zero vba

From Dev

Having count() greater than average of

From Dev

average if greater than zero vba

From Dev

In cmd.exe with codepage 437, why are characters whose "number" is greater than 127 prepended with a ┬ when using type?

From Dev

Average of integers not counting 0 as a number

From Dev

Is it possible to nth child a table cell if it contains a number greater than "x"?

From Dev

Find first number greater than x and return another value in that row

From Dev

print line only if number in third field is greater than X

From Dev

How to return the number of values greater than X with multiple criteria

From Dev

Counting the number of columns greater than 0 for each row in SQL Server 2012

From Dev

Counting the number of concurrent users on a page

From Dev

PHP: if greater than x, then x

From Dev

AppleScript less than number or greater than number

From Dev

Display the greater than the average salary by deptid wise

From Dev

Match number if greater than zero

From Dev

Check if number is greater than 4

From Dev

greater than condition with number and decimal

From Dev

RegEx for number greater than 1120

Related Related

  1. 1

    Number of Subarray whose sum greater than given value

  2. 2

    counting the number of values greater than 0 in R in multiple columns

  3. 3

    Finding a number greater than x in a range

  4. 4

    diff - if file contains a number greater than x, then

  5. 5

    Return number in array that is greater than X

  6. 6

    diff - if file contains a number greater than x, then

  7. 7

    Return number in array that is greater than X

  8. 8

    I am trying to list all the tutor names whose salary is greater than the average salary and show how much the salary is greater than by

  9. 9

    Postgres greater than or null

  10. 10

    Postgres greater than or null

  11. 11

    How to find elements whose sibling index is less than x and greater than y

  12. 12

    average if greater than zero vba

  13. 13

    Having count() greater than average of

  14. 14

    average if greater than zero vba

  15. 15

    In cmd.exe with codepage 437, why are characters whose "number" is greater than 127 prepended with a ┬ when using type?

  16. 16

    Average of integers not counting 0 as a number

  17. 17

    Is it possible to nth child a table cell if it contains a number greater than "x"?

  18. 18

    Find first number greater than x and return another value in that row

  19. 19

    print line only if number in third field is greater than X

  20. 20

    How to return the number of values greater than X with multiple criteria

  21. 21

    Counting the number of columns greater than 0 for each row in SQL Server 2012

  22. 22

    Counting the number of concurrent users on a page

  23. 23

    PHP: if greater than x, then x

  24. 24

    AppleScript less than number or greater than number

  25. 25

    Display the greater than the average salary by deptid wise

  26. 26

    Match number if greater than zero

  27. 27

    Check if number is greater than 4

  28. 28

    greater than condition with number and decimal

  29. 29

    RegEx for number greater than 1120

HotTag

Archive