函数调用一个 ID,其日期位于另一个表的两个日期之间

问题描述 投票:0回答:1

我在 PostgreSQL 中有 2 个关于 KPI 的表,

第一个表tab1包含:

"Id", "Start_Date", "End_Date" ...

第二个表 tab2 包含:

"Kpi_Id" (fk tab1), "Date" ... 

所以在这里,我需要一个函数来调用

"Kpi_Id" (fk tab1) with "Date" between "Start_Date" and "End_Date" of tab1 

(实验:

"Date"='2023-01-05' is between "Start_Date"='2023-01-01' and "End_Date"='2023-01-08')
sql postgresql function date
1个回答
0
投票

您 simoky 连接两个表并可以访问两个表的所有 colzmn

SELECT tab1.*, tab2.'
FROM tab1  INNER JOIN tabl2 ON tab1."Id" = tab2."Kpi_Id"
WHERE tab2."Date" BETWEEN tab1."Start_Date" and tab1."End_Date" 
© www.soinside.com 2019 - 2024. All rights reserved.