Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/71.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,我有两个Postgresql表,详细描述了与主题对应的离散事件 假设表A有3行时间戳为t1、t4、t6,表B有4行时间戳为t0、t2、t3、t7、t8 我想创建一个视图,显示表B中的每个记录与表B中各自的记录连接,这样表a中带有时间戳t_I的行与带有时间戳的表B中的记录直接连接符号没有多大帮助,但如果我理解正确,可能是这样的: WITH tableA as ( Select 'a1' aid, getdate()-1 A_Date union all Select 'a4',getdate()

我有两个Postgresql表,详细描述了与主题对应的离散事件

假设表A有3行时间戳为t1、t4、t6,表B有4行时间戳为t0、t2、t3、t7、t8


我想创建一个视图,显示表B中的每个记录与表B中各自的记录连接,这样表a中带有时间戳t_I的行与带有时间戳的表B中的记录直接连接符号没有多大帮助,但如果我理解正确,可能是这样的:

WITH tableA as 
(
Select 'a1' aid, getdate()-1 A_Date union all
Select 'a4',getdate()-4 union all
Select 'a6',getdate()-6
)
,tableB as
(
Select 'b0' bid,getdate() b_date union all
Select 'b2' ,getdate()-2 union all
Select 'b3',getdate()-3 UNION ALL
SELECT 'b7',getdate()-7 union all
Select 'b8',getdate()-8 
)
, j as (
Select * ,row_number() over(partition by aid order by b_date desc) r# 
from tablea
join tableb on b_date<=a_date
)
Select * from j where r# =1

使用您的示例,您希望视图中有多少行?请回答您的问题并添加一些真实的行,而不仅仅是一些奇怪的符号和基于该数据的预期输出。请您的问题-请勿在评论中发布代码或附加信息
WITH tableA as 
(
Select 'a1' aid, getdate()-1 A_Date union all
Select 'a4',getdate()-4 union all
Select 'a6',getdate()-6
)
,tableB as
(
Select 'b0' bid,getdate() b_date union all
Select 'b2' ,getdate()-2 union all
Select 'b3',getdate()-3 UNION ALL
SELECT 'b7',getdate()-7 union all
Select 'b8',getdate()-8 
)
, j as (
Select * ,row_number() over(partition by aid order by b_date desc) r# 
from tablea
join tableb on b_date<=a_date
)
Select * from j where r# =1