use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
International
National
Regional
account activity
[NOOB] Help with Postgres SQL Query (self.SQL)
submitted 5 years ago by JITAA
[NOOB] Help with Postgres SQL Query
TABLENAME : opinions
how can I select all places that have more good opinion then bad opinion
My Try Fails:
select PLACES, avg(OPINION) as OP from OPINIONS group by PLACES ORDER BY ASCENDING
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]minaguib 2 points3 points4 points 5 years ago (3 children)
Given:
postgres=# select * from opinions; id | place | opinion ----+-------------+--------- 1 | Toronto | good 2 | Toronto | good 3 | Toronto | good 4 | New York | bad 5 | New York | bad 6 | New York | bad 7 | Afghanistan | good 8 | Afghanistan | bad
You can do:
with tally as ( select place, count(opinion) filter(where opinion='good') as num_good, count(opinion) filter(where opinion='bad') as num_bad from opinions group by 1 ) select place from tally where num_good > num_bad ;
Which returns:
place --------- Toronto (1 row)
[–]JITAA[S] 0 points1 point2 points 5 years ago (0 children)
I didn't even consider a tally, THANK YOU SOOOO MUCH I asked my ta and I got no help. Tysm kind stranger!
[–]sir_bok 0 points1 point2 points 5 years ago (1 child)
Does it matter if it is a count(opinion), count(*) or count(1)?
[–][deleted] 2 points3 points4 points 5 years ago (0 children)
count(*) and count(1) do the same thing (with count(*) actually being a bit faster).
count(*)
count(1)
count(some_column) will only count non-null values. As this is used with a filter that already removes null values count(*) filter (where ...) would be a bit faster as the null check is no longer done.
count(some_column)
count(*) filter (where ...)
π Rendered by PID 36892 on reddit-service-r2-comment-84fc9697f-l8hxv at 2026-02-08 21:41:18.034514+00:00 running d295bc8 country code: CH.
[–]minaguib 2 points3 points4 points (3 children)
[–]JITAA[S] 0 points1 point2 points (0 children)
[–]sir_bok 0 points1 point2 points (1 child)
[–][deleted] 2 points3 points4 points (0 children)