[英]How to get TOP (1) row within each Group in sql server 2000
I have some following set of data from where I want to select Top 1 row for each PK_PatientId based on the current order 我有一些以下数据集,从中我想根据当前顺序为每个PK_PatientId选择前1行
PK_PatientId PK_PatientVisitId PK_VisitProcedureId DateSort
------------ ----------------- ------------------- -----------------------
1 4 4 2009-06-22 00:00:00.000
1 3 3 2009-06-22 00:00:00.000
1 2 2 2010-03-11 00:00:00.000
1 1 1 2010-03-11 00:00:00.000
5 6 6 2009-05-24 00:00:00.000
5 5 5 2009-11-07 00:00:00.000
7 7 7 2009-05-24 00:00:00.000
8 8 8 2009-05-24 00:00:00.000
9 9 9 2009-05-24 00:00:00.000
10 10 10 2009-05-24 00:00:00.000
Query that lead me to this result is 导致我得出此结果的查询是
SELECT
P.PK_PatientId
, PV.PK_PatientVisitId
, MAX(TVP.PK_VisitProcedureId) AS PK_VisitProcedureId
, MAX(PV.LastUpdated) AS DateSort
--, Row_Number() OVER (Partition BY PK_PatientId ORDER BY PV.PK_PatientVisitId DESC) AS RowNo
FROM
dbo.M_Patient AS P
INNER JOIN
dbo.M_PatientVisit AS PV
ON
P.PK_PatientId = PV.FK_PatientId
INNER JOIN
dbo.TX_VisitProcedure AS TVP
ON
PV.PK_PatientVisitId = TVP.FK_PatientVisitId
WHERE
(P.IsActive = 1)
AND
(PV.IsActive = 1)
AND
(TVP.IsActive = 1)
GROUP BY
PK_PatientId
, PK_PatientVisitId
ORDER BY
PK_PatientId
, PK_PatientVisitId DESC
and I have to get the remaining functionality that I was doing with Row Number function by taking RowNo=1. 并且我必须通过使RowNo = 1来获得与行号功能相同的其余功能。 But Now I have to take this procedure to SQL 2000 due to which this function can't be used.
但是现在我必须将此过程带到SQL 2000,由于该过程无法使用此函数。
Desired Result is 所需结果为
PK_PatientId PK_PatientVisitId PK_VisitProcedureId DateSort RowNo
------------ ----------------- ------------------- ----------------------- --------------------
1 4 4 2009-06-22 00:00:00.000 1
5 6 6 2009-05-24 00:00:00.000 1
7 7 7 2009-05-24 00:00:00.000 1
8 8 8 2009-05-24 00:00:00.000 1
9 9 9 2009-05-24 00:00:00.000 1
which I am getting when using Row_Number in sql 2005. I want same result using sql 2000 only. 在sql 2005中使用Row_Number时得到的结果。仅在sql 2000中需要相同的结果。
I have to use SQL 2000 我必须使用SQL 2000
You just need to strap this to the end of your WHERE clause: 您只需要将此附加到WHERE子句的末尾即可:
AND NOT EXISTS (
SELECT *
FROM dbo.M_PatientVisit PV2
WHERE P.PK_PatientId = PV2.FK_PatientId
AND PV2.PK_PatientVisitId > PV.PK_PatientVisitId
)
...which will result in the query returning "the patient visits for patients where there does not exist another visit for that patient with a higher ID" - that is, you'll get the visits with the highest IDs. ...这将导致查询返回“不存在ID较高的患者的其他就诊的患者就诊”-即,您将获得ID最高的访问。
Note that you'll need to include the other logic in the WHERE clause in this subquery in order to ensure that the bits are active etc. 请注意,您需要在此子查询的WHERE子句中包含其他逻辑,以确保这些位处于活动状态等。
Would you mind use temporary table in your procedure? 您介意在过程中使用临时表吗? I mean that you can insert max(PatientVisitId) rows into a temporary table.
我的意思是,您可以将max(PatientVisitId)行插入临时表中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.