0

EDIT

Yes, you are correct. What I am looking for in pseudo code is a list that

(st_area(geom)>0.1) OR (COUNT(*) > 1)

and in words:

return a list, that only has only states that have an area greater than 0.1, but don't exclude that state if it is the only one in the country (usually and island country, which has plenty room for labels next to it). The excluded states are places like Slovenia that has 100 provinces, but a tiny land area (Great Britain is also and offender).


I have a table for the entire world listing all states and provinces (I call the table states, but it also can mean province).

StateName, ContryName, Pop, geometry

The table is on PostGreSQL 9.2 PostGIS 2.0

I need to remove small states (area too small) to label. But if it is an island (one country, one state) then I want to leave it in.

My Naive query is like this, but there is a syntax error:

SELECT s.name,s.admin, st_area(geom)
FROM vector.states s
INNER JOIN (
SELECT ss.admin
FROM vector.states ss
GROUP BY ss.admin
HAVING (COUNT(*) > 1) AND (st_area(ss.geom) > 0.01)
) a ON a.admin = s.admin
ORDER BY s.admin ASC;

this is the syntax error (and I expected this to happen).

ERROR:  column "ss.geom" must appear in the GROUP BY clause or be used in an aggregate function
LINE 7: HAVING (COUNT(*) > 1) AND (st_area(ss.geom) > 0.01)
1
  • Like I requested under your previous question, you need to provide the table definition. The meaning of the column admin is unclear. Nov 22, 2013 at 19:28

3 Answers 3

1

Two problems:

  1. Just like the error message tells you, geom needs to be wrapped in an aggregate function if it is not listed in GROUP BY. You could just use min() ...
  2. You got your logic backwards. It needs to be COUNT(*) = 1 OR ..

But this can be solved more elegantly with an anti-semi-join using NOT EXISTS:

SELECT s.name, s.admin, st_area(geom)
FROM   vector.states s
WHERE  st_area(s.geom) > 0.01           -- state big enough ...
   OR NOT EXISTS (                      -- ... or there are no other counties
         SELECT 1 FROM vector.states s2
         WHERE  s2.admin = s.admin
         AND    s2.pk_column <> s.pk_column  -- exclude self-join
         )
ORDER BY s.admin;

Replace pk_column with your actual primary key column(s).

2
  • YES. You just saved Aruba, Gibraltar, Guam and the Vatican. (I.e. without the self-join) they would be too small to label, but since a lot of them our out in the ocean, they are likely to fit (this is an important first pass, to the map labeler, which then looks at text bound box overlaps, it cuts the work by log2().
    – Dr.YSG
    Nov 25, 2013 at 16:36
  • @Dr.YSG: Aruba would've been a real shame! ;) Nov 25, 2013 at 18:41
0

Editing the answer, I didn't understood the problem the 1st time sorry for that

Try this:

SELECT s.name,s.admin, st_area(geom)
FROM vector.states s
WHERE s.admin in (
SELECT ss.admin
FROM vector.states ss
GROUP BY ss.admin
HAVING (COUNT(*) > 1)
)
AND (st_area(s.geom) > 0.01)
ORDER BY s.admin ASC;

Hope it helps!

3
  • This doesn't fix the exception. count(*) doesn't have anything to do with the problem in the question. What's that even supposed to mean: postgreSQL is not a fanatic of count(*)? Nov 22, 2013 at 19:34
  • Yes Erwin you are totally right, i've edited the answer thank you! Nov 22, 2013 at 20:02
  • Makes a lot more sense now. But it's still incorrect, excluding all states with just one county instead on including them. Nov 22, 2013 at 20:40
0

This part: (st_area(ss.geom) > 0.01) doesn't belong in HAVING clause. HAVING is to limit results based on aggregate functions, like that COUNT, but for the rest, you have to use WHERE.

Then, i would use two separate queries to get those different values:

SELECT s.NAME, s.admin, st_area(geom)
FROM vector.states s
INNER JOIN (
  (SELECT ss.admin
    FROM vector.states ss
    WHERE st_area(ss.geom) > 0.01
    GROUP BY ss.admin
    HAVING COUNT(ss.admin) > 1)  
  UNION ALL  
  (SELECT ss.admin
    FROM vector.states ss
    GROUP BY ss.admin
    HAVING COUNT(ss.admin) = 1)
  ) a
ON a.admin = s.admin
ORDER BY s.admin ASC;
2
  • You are right, my assessment was sloppy. Only your second query is incorrect: the two joins to mutually exclusive sets alway result in no rows. Your first query seems correct - but it has to scan the states table three times, so it will be rather inefficient. Nov 22, 2013 at 20:54
  • @ErwinBrandstetter. Yep. Don't know what i was thinking on that second query. Nov 22, 2013 at 21:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.