我有一个具有以下架构的表:
CREATE TABLE Codes
(
diagnosis_code CHAR,
visit_date DATE,
visit_id INT,
patient_id int
);
我想在一定时间(例如15天)内以相同的patient_id
输出患者被重新接纳(这样的visit_id
不同)的diagnosis_code
。例如,如果我在表中有以下条目:
diagnosis_code visit_date visit_id patient_id
-------------- ---------- ----------- -----------
A 2018-01-01 1 1
B 2018-01-01 1 1
A 2018-01-07 2 1
C 2018-01-01 3 2
D 2018-01-01 4 3
D 2018-01-20 5 3
E 2018-01-01 6 4
E 2018-01-01 6 4
A 2018-01-07 7 1
查询将仅返回patient_id
= 1
,其原理如下:
1
,因为该患者在visit_id
1
和2
之间共享诊断代码A
。2
,因为该患者仅入院一次。3
,因为该患者虽然因再次诊断而再次入院,但在初次就诊后的15天内未再次入院。4
,因为该患者在同一次就诊中具有重复的诊断代码。patient_id
= 1
期间,visit_id
= 7
因同一诊断而被重新录入,但之前已经被计算过一次。答案 0 :(得分:1)
您可以尝试简单的联接,添加您描述的条件:
// Core location pages
$location_pages = array(
'book-appointment' => 'Book Appointment'
);
// Merge with services array
$my_fake_pages = array_merge($services,$location_pages);
add_filter('rewrite_rules_array', 'fsp_insertrules');
add_filter('query_vars', 'fsp_insertqv');
// Adding fake pages' rewrite rules
function fsp_insertrules($rules)
{
global $my_fake_pages;
$newrules = array();
foreach ($my_fake_pages as $slug => $title) {
$newrules['location/([^/]+)/' . $slug . '/?$'] = 'index.php?location=$matches[1]&fpage=' . $slug;
}
return $newrules + $rules;
}
// Tell WordPress to accept our custom query variable
function fsp_insertqv($vars)
{
array_push($vars, 'fpage');
return $vars;
}
答案 1 :(得分:0)
我用了滞后时间。
declare @Codes table
(
diagnosis_code CHAR,
visit_date DATE,
visit_id INT,
patient_id int
);
insert into @Codes
values
('A', '2018-01-01' ,1, 1)
,('B' , '2018-01-01', 1, 1)
,('A' , '2018-01-07', 2, 1)
,('C' ,'2018-01-01', 3, 2)
/*
D 2018-01-01 4 3
D 2018-01-15 5 3
E 2018-01-01 6 4
E 2018-01-01 6 4
A 2018-01-07 7 1
*/
select *
from (
select *
--,rn=row_number() over (partition by patient_ID,diagnosis_code order by visit_date)
,DaysSince = datediff(day,lag(visit_date,1) over (partition by patient_ID,diagnosis_code order by visit_date),visit_date)
from @Codes
) a
where a.DaysSince<=15
答案 2 :(得分:0)
您还可以使用内置的FIRST_VALUE
和DATEADD
函数来实现此目的:
SELECT
DISTINCT patient_id,diagnosis_code
FROM
(SELECT
FIRST_VALUE(visit_date) OVER (PARTITION BY patient_id,diagnosis_code ORDER BY visit_id ASC) AS Initial_Visit,
DATEADD(DAY,15,first_value(visit_date) OVER (PARTITION BY patient_id,diagnosis_code ORDER BY visit_id ASC)) Window
,* FROM Codes
)m
WHERE
Initial_Visit <> visit_date
AND visit_date <= Window