0

I have a table in Postgres 9.2 with 38 variables and I need a selection of the "best" results.
What I need is:
 distinct var1 and var2 then from that:
  min var3 and also var4 from that same row
  max var5 and if more than one result then where min var3, var6 to var12 from that same row
  var13 sorted by conditions (3 first, 6 second 0 last) and also var14-var18 from that same row

v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 ...
1  1  2  a  2  a  .  .  .  .   .   .   0   .   .   .   .   .
1  1  1  b  1  b  .  .  .  .   .   .   3   .   .   .   .   .
1  2  4  c  3  c  .  .  .  .   .   .   3   .   .   .   .   .
1  2  3  d  4  d  .  .  .  .   .   .   6   .   .   .   .   .
2  1  1  a  3  a  .  .  .  .   .   .   3   .   .   .   .   .
3  1  3  a  2  a  .  .  .  .   .   .   6   .   .   .   .   .
3  1  2  b  4  b  .  .  .  .   .   .   0   .   .   .   .   .
4  1  3  a  4  a  .  .  .  .   .   .   3   .   .   .   .   .
4  1  6  b  2  b  .  .  .  .   .   .   0   .   .   .   .   .
4  2  2  c  2  c  .  .  .  .   .   .   0   .   .   .   .   .
4  3  5  d  3  d  .  .  .  .   .   .   3   .   .   .   .   .
4  3  4  e  4  e  .  .  .  .   .   .   6   .   .   .   .   .
4  3  7  f  4  f  .  .  .  .   .   .   3   .   .   .   .   .
...

The result should be:

v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18
1  1  1  b  2  a  .  .  .  .   .   .   3   .   .   .   .   .
1  2  3  d  4  d  .  .  .  .   .   .   3   .   .   .   .   .
2  1  1  a  3  a  .  .  .  .   .   .   3   .   .   .   .   .
3  1  2  b  4  b  .  .  .  .   .   .   6   .   .   .   .   .
4  1  3  a  4  a  .  .  .  .   .   .   3   .   .   .   .   .
4  2  2  c  2  c  .  .  .  .   .   .   0   .   .   .   .   .
4  3  4  e  4  e  .  .  .  .   .   .   3   .   .   .   .   .
...

here is also an image of the table where the colored fields show what should be selected: image clarifies what should be selected from the table Hope this makes sense.

EDIT:

Got a pointer in another post to provide CREATE and INSERT for the table.

create table parent (
  v1 character varying,
  v2 character varying,
  v3 character varying,
  v4 character varying,
  v5 character varying,
  v6 character varying,
  v7 character varying,
  v8 character varying,
  v9 character varying,
  v10 character varying,
  v11 character varying,
  v12 character varying,
  v13 character varying,
  v14 character varying,
  v15 character varying,
  v16 character varying,
  v17 character varying,
  v18 character varying
);

insert into parent values('1','1','2','a','2','a','x1','x1','x1','x1','x1','x1','0','x1','x1','x1','x1','x1');
insert into parent values('1','1','1','b','1','b','x2','x2','x2','x2','x2','x2','3','x2','x2','x2','x2','x2');
insert into parent values('1','2','4','c','3','c','x3','x3','x3','x3','x3','x3','3','x3','x3','x3','x3','x3');
insert into parent values('1','2','3','d','4','d','x4','x4','x4','x4','x4','x4','6','x4','x4','x4','x4','x4');
insert into parent values('2','1','1','a','3','a','x1','x1','x1','x1','x1','x1','3','x1','x1','x1','x1','x1');
insert into parent values('3','1','3','a','2','a','x1','x1','x1','x1','x1','x1','6','x1','x1','x1','x1','x1');
insert into parent values('3','1','2','b','4','b','x2','x2','x2','x2','x2','x2','0','x2','x2','x2','x2','x2');
insert into parent values('4','1','3','a','4','a','x1','x1','x1','x1','x1','x1','3','x1','x1','x1','x1','x1');
insert into parent values('4','1','6','b','2','b','x2','x2','x2','x2','x2','x2','0','x2','x2','x2','x2','x2');
insert into parent values('4','2','2','c','2','c','x3','x3','x3','x3','x3','x3','0','x3','x3','x3','x3','x3');
insert into parent values('4','3','5','d','3','d','x4','x4','x4','x4','x4','x4','3','x4','x4','x4','x4','x4');
insert into parent values('4','3','4','e','4','e','x5','x5','x5','x5','x5','x5','6','x5','x5','x5','x5','x5');
insert into parent values('4','3','7','f','4','f','x6','x6','x6','x6','x6','x6','3','x6','x6','x6','x6','x6');
2
  • 2
    And what did you try ?
    – Houari
    Jan 26, 2014 at 10:19
  • 1
    I've tried so many various ways I thought it would complicate the question too much including that. I've tried joins, union, with, sub queries, window functions and what not. I can solve various parts but finding a single solutions has proved unsuccessful.
    – thorgilsv
    Jan 26, 2014 at 14:06

0

Your Answer

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

Browse other questions tagged or ask your own question.