Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.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
合并来自Postgresql的两个子查询的结果_Sql_Postgresql - Fatal编程技术网

合并来自Postgresql的两个子查询的结果

合并来自Postgresql的两个子查询的结果,sql,postgresql,Sql,Postgresql,我使用Postgresql创建了两个子查询,返回的结果如下: firm_id type_1 fee_1 1 2 100 2 4 300 5 1 100 firm_id type_2 fee_2 1 3 200 2 3 200 3

我使用Postgresql创建了两个子查询,返回的结果如下:

firm_id      type_1      fee_1
   1           2          100
   2           4          300
   5           1          100

firm_id      type_2      fee_2
   1           3          200
   2           3          200
   3           2          150
   4           5          300
我想得出以下结果:

firm_id      type_1     type_2    total_fee
   1           2          3         300
   2           4          3         500
   3           0          2         150
   4           0          5         300
   5           1          0         100
感谢您的帮助

SELECT firm_id
    ,coalesce(t.type_1, 0) type_1
    ,coalesce(b.type_1, 0) type_2
    ,coalesce(t.fee_1, 0) + coalesce(b.fee_1, 0) total_fee
FROM (
    SELECT *   --Your first select query
    FROM tablea
    ) t
FULL JOIN (
    SELECT *   --Your second select query
    FROM tableb
    ) b using (firm_id)
:合并左侧和右侧外部联接的结果。 联接的表将包含两个表中的所有记录,并为任意一侧缺少的匹配项填充空值

函数返回其第一个非空参数。仅当所有参数都为Null时,才会返回Null。在检索数据以显示时,它通常用于将默认值替换为空值

SELECT coalesce( t1."firm_id", t2."firm_id" ) as firm_id,
       coalesce( t1."type_1", 0 ) as type_1,
       coalesce( t2."type_2", 0 ) as type_2,
       coalesce( t1."fee_1", 0 ) 
       +
       coalesce( t2."fee_2", 0 ) as total_fee
FROM table1 t1
FULL JOIN table2 t2
ON t1."firm_id" = t2."firm_id"
:合并左侧和右侧外部联接的结果。 联接的表将包含两个表中的所有记录,并为任意一侧缺少的匹配项填充空值

函数返回其第一个非空参数。仅当所有参数都为Null时,才会返回Null。在检索数据以显示时,它通常用于将默认值替换为空值

SELECT coalesce( t1."firm_id", t2."firm_id" ) as firm_id,
       coalesce( t1."type_1", 0 ) as type_1,
       coalesce( t2."type_2", 0 ) as type_2,
       coalesce( t1."fee_1", 0 ) 
       +
       coalesce( t2."fee_2", 0 ) as total_fee
FROM table1 t1
FULL JOIN table2 t2
ON t1."firm_id" = t2."firm_id"
其中,表1和表2必须替换为子查询 请参见演示:

其中,表1和表2必须替换为子查询 请参阅演示:

使用完全连接和合并:

使用完全连接和合并:


如果您有两个子查询,比如a和b,它们给出了集合a和集合b,那么您要寻找的是一个完整联接a x b,其中完整联接中至少有一个公司id不为空,计算列total_fee=fee_1+fee_2

我不太熟悉postgresql语法,但应该是

select 
    -- get rid of the columns you don't want
    a.firm_id, a.type_1, a.type_2, a.fee_1, 
    b.firm_id, b.type_1, b.type_2, b.fee_2, 
    a.fee_1 + b.fee_2 as total_fee
from ( subquery_1 here ) as a
full join ( subquery_2 here) as b 
on
    b.firm_id = a.firm_id and 
    b.type_1 <> a.type_1 
where 
    a.firm_id is not null or b.firm_id is not null 

如果您有两个子查询,比如a和b,它们给出了集合a和集合b,那么您要寻找的是一个完整联接a x b,其中完整联接中至少有一个公司id不为空,计算列total_fee=fee_1+fee_2

我不太熟悉postgresql语法,但应该是

select 
    -- get rid of the columns you don't want
    a.firm_id, a.type_1, a.type_2, a.fee_1, 
    b.firm_id, b.type_1, b.type_2, b.fee_2, 
    a.fee_1 + b.fee_2 as total_fee
from ( subquery_1 here ) as a
full join ( subquery_2 here) as b 
on
    b.firm_id = a.firm_id and 
    b.type_1 <> a.type_1 
where 
    a.firm_id is not null or b.firm_id is not null 

尝试此选择*从子查询1t完全联接子查询2b使用公司ID尝试此选择*从子查询1t完全联接子查询2b使用公司ID您可以提供一些上下文吗?您可以提供一些上下文吗?