oracle - SQL help with MAX query -



oracle - SQL help with MAX query -

i have table of countries named bbc(name, region, area, population, gdp)

i want table region, name , population of largest ( populated) countries region. far i've tried this:

select region, name, max(population) bbc grouping part

it gave me error message : ora-00979: not grouping expression

i tried alter grouping region, name, doesn't give me right table

here's easiest , shortest way it, since oracle has tuple testing, can create code shorter:

first, max population on each region:

select region, max(population) bbc grouping part

then test countries against it:

select region, name, population bbc (region, population) in (select region, max(population) bbc grouping region) order part

if want back upwards many rdbms, utilize exists:

select region, name, population bbc o exists (select null -- neutral. doesn't invoke cargo cult programming ;-) bbc part = o.region grouping part having o.population = max(population) ) order part

query tested here, both have similar output: http://sqlzoo.net/0.htm

http://www.ienablemuch.com/2010/05/why-is-exists-select-1-cargo-cult.html

sql oracle aggregate-functions ora-00979

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

c# - Can ProtoBuf-Net deserialize to a flat class? -

javascript - Change element in each JQuery tab to dynamically generated colors -