简体   繁体   中英

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 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. But Now I have to take this procedure to SQL 2000 due to which this function can't be used.

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.

I have to use SQL 2000

You just need to strap this to the end of your WHERE clause:

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.

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.

Would you mind use temporary table in your procedure? I mean that you can insert max(PatientVisitId) rows into a temporary table.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM