Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/74.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/1/hibernate/5.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_Database_Postgresql_Join - Fatal编程技术网

Sql 在一个列值上连接两个表,并根据其他列进行排序

Sql 在一个列值上连接两个表,并根据其他列进行排序,sql,database,postgresql,join,Sql,Database,Postgresql,Join,我想把这两张桌子连接起来 表1 Subject_id time_x x_value ---------- ------ ------- 123 12:00 4 123 15:00 3 123 19:00 2 123 21:00 1 表2 Subject_id time_y y_value ---------- ------- -------- 123 13:00

我想把这两张桌子连接起来

表1

Subject_id  time_x   x_value
----------  ------   -------
  123       12:00     4
  123       15:00     3
  123       19:00     2
  123       21:00     1
表2

Subject_id time_y   y_value
---------- -------  --------
    123    13:00       22
    123    14:00       25
    123    15:00       24
    123    16:00       36
    123    17:00       22
    123    18:00       25
    123    19:00       26
    123    20:00       45
在列
subject\u id
上,结果必须如下所示

结果

Subject_id  x_value y_value time_x  time_y  
----------  ------- ------- ------  ------   
  123        4      null    12:00    12:00
  123       null     22     13:00    13:00  
  123       null     25     14:00    14:00  
  123        3       24     15:00    15:00  
  123       null     36     16:00    16:00  
  123       null     22     17:00    17:00  
  123       null     25     18:00    18:00  
  123        2       26     19:00    19:00  
  123       null     45     20:00    20:00  
  123        1      null    21:00    21:00

因此,您可以帮助我实现这些结果。

您可以始终使用select union进行获取,然后与每个表左键联接

  select t.subject_id, t1.x_value, t2.y_value, t.my_time time_x, t.my_time time_y
  from  (
    select  subject_id, time_x my_time 
    from table1 
    union  
    select subject_id, time_y  
    from table2 
  ) t 
  left join  table1 t1 on t.subject_id = t1.subject_id
    and t.my_time  = t1.time_x
  left join  table2 t2 on t.subject_id = t2.subject_id
    and t.my_time  = t2.time_y

我不明白你为什么有两个时间栏。所以,我想你想要:

select coalesce(t1.subject_id, t2.subject_id) as subject_id,
       t1.x_value, t2.y_value,
       coalesce(t1.time_x, t2.time_y) as time
from table1 t1 full join
     table2 t2
     on t2.subject_id = t1.subject_id and t2.time_y = t1.time_x;

如果愿意,您可以复制
时间
列,但我认为这没有必要。

尝试完全加入!!我试过了,但问题是x值和y值之间的时间偏差,而且x值被复制以填补频率差。你能帮我解决这个问题吗?谢谢