Postgres表,选择不同的子查询和多个子查询

时间:2014-01-26 02:41:53

标签: postgresql

我在Postgres 9.2中有一个包含38个变量的表格,我需要选择“最佳”结果 我需要的是:
然后是不同的var1和var2:
min var3和同一行的var4 max var5和如果多于一个结果那么min var3,var6到var12来自同一行
var13按条件排序(3首先,6秒0最后)和同一行的var14-var18

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   .   .   .   .   .
...

结果应该是:

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   .   .   .   .   .
...

这里也是表格的图像,其中彩色字段显示应该选择的内容: image clarifies what should be selected from the table 希望这是有道理的。

修改

在另一篇文章中有一个指针,为表提供CREATE和INSERT。

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');

0 个答案:

没有答案