Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I am having difficulty writing a nested postgis query. I have a single table from which I need to create a view in PostgreSQL.

I have created the view with the following command

create view rrop08.rrop08_lws_f AS
select * from rrop08.rrop08_gk3_polygon 
where planznr='04.01' OR planznr ='04.02';

The problem here is that the table contains another field called "bez". I need to carry out the first query but also with bez=0; so that none of the 04.01 or 04.02 rows have a bez value other than 0.

I have tried this...

create view rrop08.rrop08_lws_f AS
select * from rrop08.rrop08_gk3_polygon 
where planznr='04.01' OR planznr ='04.02' AND bez=0;

but this retrieves all rows with bez=0 and not just the 04.01 or 04.02 ones. I need to first select 04.01 or 04.02 and then on this query get those with bez=0...ie.nested.

I´m a bit lost on how to do this.

THanks for any help,

Rob

share|improve this question
add comment

1 Answer 1

up vote 3 down vote accepted

Use parenthesis to group the logical expressions or they usually get evaluated from left to right like you experienced:

create view rrop08.rrop08_lws_f AS select * from rrop08.rrop08_gk3_polygon where (planznr='04.01' OR planznr ='04.02') AND bez=0;
share|improve this answer
    
Oh God! how easy!....thanks for the insight! –  Robert Buckley Jul 11 '12 at 10:52
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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