sql - Rewriting the HAVING clause and COUNT function -


i'm trying conceptually understand how rewrite having clause , count function.

i asked "find names of reviewers have contributed 3 or more ratings. (as challenge, try writing query without having or without count.)" in relation this simple database: http://sqlfiddle.com/#!5/35779/2/0

the query having , count easy. without, i'm having difficulty.

help appreciated. thank you.

one option use sum(1) in place of count in subquery, , using where instead of having:

select b.name (select rid,sum(1) sum1       rating       group rid       )a join reviewer b   on a.rid = b.rid sum1 >= 3 

demo: sql fiddle

update: explanation of sum(1): adding constant select statement result in value being repeated every row returned, example:

select rid       ,1 col1 rating 

returns:

| rid | col1 | |-----|------| | 201 |    1 | | 201 |    1 | | 202 |    1 | | 203 |    1 | | 203 |    1 | ...... 

sum(1) applying constant 1 every row , aggregating it.


Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -