[英]How do you use a loop in SQL to compare a record to a previous record?
I am looking at a data set of Emergency Room visits.我正在查看急诊室就诊的数据集。 I only want to keep visits per ID that are 30 days apart.我只想保持每个 ID 相隔 30 天的访问。 So as an example say I have this below.举个例子,我在下面有这个。
If I start with ID=1:如果我从 ID=1 开始:
I have been trying to do this with the lag function but I can't figure out how to utilize the lag when I have to continue to use the 'anchor' row to evaluate several rows.我一直在尝试使用滞后 function 来做到这一点,但是当我必须继续使用“锚”行来评估几行时,我无法弄清楚如何利用滞后。
Top is what I have and bottom is what I want.顶部是我拥有的,底部是我想要的。 Any ideas?有任何想法吗?
I am using AZURE data studio.我正在使用 AZURE 数据工作室。
HAVE有
Row# ID DATE
1 1 1/1/2020
2 1 1/15/2020
3 1 1/17/2020
4 1 2/4/2020
5 1 3/15/2020
6 2 1/15/2020
7 2 3/15/2020
8 2 3/18/2020
WANT想
Row# ID DATE
1 1 1/1/2020
4 1 2/4/2020
5 1 3/15/2020
6 2 1/15/2020
7 2 3/15/2020
This tutorial page should get you started on a cursor based solution.本教程页面应该让您开始使用基于 cursor 的解决方案。
You don't use a loop.你不使用循环。 You continue to use LAG, you were on the right way initially.你继续使用 LAG,你一开始是对的。
;WITH dateLagged AS (
SELECT
ID
, Date
, Diff = ISNULL(DATEDIFF(day,LAG(Date,1) OVER(PARTITION BY ID ORDER BY ID, Date), Date),0)
FROM dbo.EmergencyRoom),
DiffCumulated AS (
SELECT
ID
, Date
, CumDiff = SUM(Diff) OVER(PARTITION BY ID ORDER BY ID, Date)
FROM dateLagged
),
AnchorsMarked AS (
SELECT
ID
, Date
, Marker = IIF(CumDiff = 0
OR CumDiff > 30 AND LAG(CumDiff,1) OVER(ORDER BY ID, Date) < 30
OR CumDiff - LAG(CumDiff,1) OVER(ORDER BY ID, Date) > 30, 1,0)
FROM DiffCumulated
)
SELECT
ID
, Date
FROM AnchorsMarked WHERE Marker = 1
As a rule of thumb: if you want to use looping in SQL then you've taken a wrong turn somewhere.根据经验:如果您想在 SQL 中使用循环,那么您在某处走错了路。 There are very few problems in SQL which do require looping, it is not one of them. SQL 中很少有问题需要循环,它不是其中之一。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.