Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL+使用不同行集进行内部选择_Sql_Postgresql - Fatal编程技术网

SQL+使用不同行集进行内部选择

SQL+使用不同行集进行内部选择,sql,postgresql,Sql,Postgresql,我有一个包含3个内部选择的sql选择: select t1.a, t2.b, t3.c from (select aa as a from ...) as t1 <-- query 1 (select bb as b from ...) as t2 <-- query 2 (select cc as c from ...) as t3 <-- query 3 where t1.id = t2.id and t2.id = t3.id 我想显示以下内容: c

我有一个包含3个内部选择的sql选择:

select t1.a, t2.b, t3.c
from (select aa as a from ...) as t1 <-- query 1
     (select bb as b from ...) as t2 <-- query 2
     (select cc as c from ...) as t3 <-- query 3
where
t1.id = t2.id and t2.id = t3.id
我想显示以下内容:

client_id | kpi_1.name | kpi_1.value | kpi_2.name | kpi_2.value | kpi_3.name | kpi_3.value
----------+------------+-------------+------------+-------------+------------+------------
1         | 'kpi 1'    | a1_1        | 'kpi 2'    | b1_2        | <null>     | <null>
2         | 'kpi 1'    | a2_1        | 'kpi 2'    | b2_2        | 'kpi 3'    | c2_3 
3         | 'kpi 1'    | a3_1        | 'kpi 2'    | b3_2        | <null>     | <null>
我需要一个快速有效的sql,使用尽可能少的sql join命令,我想为此创建一个视图表

我使用postgreSQL server

Thx.

您想要一个:

除非您的子查询中有复杂的逻辑,否则如果您只需在FROM中指定表名,可能会更容易阅读。

JOIN

select aa.a, bb.b, cc.c
from aa
  inner join bb on aa.id = bb.id
  inner join cc on cc.id = bb.id
where
  t1.id = t2.id and t2.id = t3.id

听起来您希望在这些派生表之间进行左外部联接,甚至完全外部联接。请问哪种RDBMS?
select
    t1.a
   ,t2.b
   ,t3.c
from table1      as t1
left join table2 as t2
  on t1.id = t2.id
left join table3 as t3
  on t2.id = t3.id
select aa.a, bb.b, cc.c
from aa
  inner join bb on aa.id = bb.id
  inner join cc on cc.id = bb.id
where
  t1.id = t2.id and t2.id = t3.id