1

In order to get final results I need to write lots of queries:

SELECT min(x) from table WHERE column1='ab' AND column2='3';
SELECT min(y) from table WHERE column1='ab' AND column2='3';
SELECT max(x) from table WHERE column1='ab' AND column2='3';
SELECT max(y) from table WHERE column1='ab' AND column2='3';
SELECT min(x) from table WHERE column1='ab' AND column2='4';
SELECT min(y) from table WHERE column1='ab' AND column2='4';
SELECT max(x) from table WHERE column1='ab' AND column2='4';
SELECT max(y) from table WHERE column1='ab' AND column2='4';
...

where column2 goes from 3-8.

I guess I can CASE that somehow, and maybe do some sort of FOR loop for column2, but I have no success with it. Or I can do something else with it?

Wanted result:

column2 |  minx  |   miny |   maxx |   maxy |
      3 | number | number | number | number |
      4 | number | number | number | number |
      5 | number | number | number | number |
      6 | number | number | number | number |
      7 | number | number | number | number |
      8 | number | number | number | number |

Any help appriciated!

2 Answers 2

2

Not sure how you'd use a case in that but seems like a simple use for a group by statement.

SELECT Column2, Min(x), Min(y), Max(x), Max(y)
FROM table 
WHERE Column1='ab' AND Column2 > 2 AND Column2 < 9 GROUP BY Column2

I normally do T-SQL for MS-SQL but this is pretty basic so I expect this will work on Postgres

Sign up to request clarification or add additional context in comments.

Comments

1

Why case? You can just group by column2.

select
    column2,
    min(x) as minx,
    min(y) as miny,
    max(x) as maxx,
    max(y) as maxy
from table
where column1 = 'ab' and column2 between 3 and 8
group by
    column2

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.