如何在postgresql中实现与same table的匹配?
How to achieve matching with same table in postgresql?
我想根据以下标准计算按国家划分的网站数量:-
对于 Trial_Start_Date = X 和 Site_Activated = Y 的每个 TrialSite,您应该计算满足这些条件的所有行:
- Trial_Start_Date <= Y, AND
- TrialSite_Activation_Date >= X
即所有与该行的试用开始日期到 TrialSite 激活日期有重叠时间段的行。
示例数据示例:
Trial id start_date country site_id trialSite_activation_date
Trial A 01-01-2017 India site1 01-02-2017 ----> 2 (only overlaps with itself, and with 2nd row)
Trial A 01-01-2017 India site 2 01-04-2017 ----> 4 (overlaps with all rows, including itself)
Trial B 02-03-2017 India site3 01-04-2017 ----> 3 (does not overlap with first row, since Trial_Start_Date > 01-02-2017)
Trial B 02-03-2017 India site4 01-04-2017 ----> 3
此数据可以包含多个国家,此逻辑需要应用于相同的国家记录。
您可以在 daterange
上使用“重叠”运算符 &&
:
SELECT t1, count(*)
FROM trial t1
JOIN trial t2
ON daterange(t1.trial_start_date, t1.trialsite_activation_date, '[]')
&& daterange(t2.trial_start_date, t2.trialsite_activation_date, '[]')
GROUP BY t1;
t1 | count
-----------------------------------------------+-------
("Trial A",2017-01-01,India,site1,2017-02-01) | 2
("Trial A",2017-01-01,India,site2,2017-04-01) | 4
("Trial B",2017-03-02,India,site3,2017-04-01) | 3
("Trial B",2017-03-02,India,site4,2017-04-01) | 3
(4 rows)
而不是在 SELECT
列表中使用整行引用 t1
,您可以在那里指定单独的列,但随后您将它们列在 GROUP BY
子句中作为嗯。
我想根据以下标准计算按国家划分的网站数量:-
对于 Trial_Start_Date = X 和 Site_Activated = Y 的每个 TrialSite,您应该计算满足这些条件的所有行:
- Trial_Start_Date <= Y, AND
- TrialSite_Activation_Date >= X 即所有与该行的试用开始日期到 TrialSite 激活日期有重叠时间段的行。
示例数据示例:
Trial id start_date country site_id trialSite_activation_date
Trial A 01-01-2017 India site1 01-02-2017 ----> 2 (only overlaps with itself, and with 2nd row)
Trial A 01-01-2017 India site 2 01-04-2017 ----> 4 (overlaps with all rows, including itself)
Trial B 02-03-2017 India site3 01-04-2017 ----> 3 (does not overlap with first row, since Trial_Start_Date > 01-02-2017)
Trial B 02-03-2017 India site4 01-04-2017 ----> 3
此数据可以包含多个国家,此逻辑需要应用于相同的国家记录。
您可以在 daterange
上使用“重叠”运算符 &&
:
SELECT t1, count(*)
FROM trial t1
JOIN trial t2
ON daterange(t1.trial_start_date, t1.trialsite_activation_date, '[]')
&& daterange(t2.trial_start_date, t2.trialsite_activation_date, '[]')
GROUP BY t1;
t1 | count
-----------------------------------------------+-------
("Trial A",2017-01-01,India,site1,2017-02-01) | 2
("Trial A",2017-01-01,India,site2,2017-04-01) | 4
("Trial B",2017-03-02,India,site3,2017-04-01) | 3
("Trial B",2017-03-02,India,site4,2017-04-01) | 3
(4 rows)
而不是在 SELECT
列表中使用整行引用 t1
,您可以在那里指定单独的列,但随后您将它们列在 GROUP BY
子句中作为嗯。