尊敬的堆栈溢出社区,
我正在寻找患者编号,其中第一个日期之后的两个连续日期少于7天。
因此第二date <= 7
天和第一date <= 7
天之间的差异
以及第三和第二ID Date
1 9/8/2014
1 9/9/2014
1 9/10/2014
2 5/31/2014
2 7/20/2014
2 9/8/2014
天之间的差异
示例:
R.prototype.callSync = function(_opts) {
var opts = _opts || {};
this.options.env.input = JSON.stringify([this.d, this.path, opts]);
var child = child_process.spawnSync("Rscript", this.args, this.options);
if (child.stderr) throw child.stderr;
return(JSON.parse(child.stdout));
};
对于患者1,相隔的两个日期相隔不到7天。
但是对于患者2,以下日期相隔7天以上(50天)。
我正在尝试编写一个仅输出患者ID“ 1”的SQL查询。
感谢您的帮助:)
答案 0 :(得分:0)
您可以尝试使用窗口函数lag()
select * from
(
select id,date,lag(date) over(order by date) as prevdate
from tablename
)A where datediff(day,date,prevdate)<=7
答案 1 :(得分:0)
您想使用lead()
,但这很复杂,因为您只想在前三行使用它。我想我会去:
select t.*
from (select t.*,
lead(date, 1) over (partition by id order by date) as next_date,
lead(date, 2) over (partition by id order by date) as next_date_2,
row_number() over (partition by id order by date) as seqnum
from t
) t
where seqnum = 1 and
next_date <= date + interval '7' day and
next_date2 <= next_date + interval '7' day;