代码之家  ›  专栏  ›  技术社区  ›  uzay95

T-SQL,如何得到这些结果?

  •  1
  • uzay95  · 技术社区  · 15 年前

    此查询

          SELECT PA.refPatient_id
                ,MAX(PA.datee) AS datee
                ,PR.temporary,PA.statue
    
            FROM PatientClinicActs AS PA
                ,PatientStatueReasons AS PR
    
           WHERE PA.refClinic_id = 25 
             AND PA.refreason_id = PR.reason_id
    
        GROUP BY PA.refPatient_id,PA.statue,PR.temporary
    

    返回这些结果:

    refPatient_id datee                 temporary statue
    ----------- ----------------------- ------ -----
    14706       2008-12-01 11:01:00     1      0
    14707       2009-05-18 16:21:00     1      0
    14708       2009-07-15 09:46:00     1      0
    14708       2009-07-29 16:12:00     1      0
    14716       2009-11-09 12:29:00     0      0
    14716       2009-09-01 11:15:00     1      0
    14716       2009-09-29 16:44:00     1      1
    

    但我想得到这些结果:

    refPatient_id datee                   temporary statue
    ----------- ----------------------- ------ -----
    14706       2008-12-01 11:01:00     1      0
    14707       2009-05-18 16:21:00     1      0
    14708       2009-07-29 16:12:00     1      0
    14716       2009-11-09 12:29:00     0      0
    

    有什么区别?=>对于这些结果中的每个refpatient,我都有最新的一行。 我该怎么跑才能得到这些结果?

    4 回复  |  直到 15 年前
        1
  •  3
  •   Adriaan Stander    15 年前

    试试这个

    SELECT  PA.refPatient_id,
            PA.datee,
            PR.temporary,
            PA.statue
    FROM    PatientClinicActs AS PA INNER JOIN
            (
                SELECT  PA.refPatient_id,
                        MAX(PA.datee) AS datee
                FROM    PatientClinicActs AS PA
                WHERE   PA.refClinic_id = 25          
                GROUP BY PA.refPatient_id,PA.statue,PA.datee,
            ) AS MaxDates ON PA.refPatient_id = MaxDates.refPatient_id AND PA.datee = MaxDates.datee INNER JOIN
            PatientStatueReasons AS PR ON PA.refreason_id = PR.reason_id
    

    您需要先获取每个患者的最大日期,然后再加入。

        2
  •  1
  •   priyanka.sarkar    15 年前

    而不是交叉联接

    FROM PatientClinicActs AS PA
                    ,PatientStatueReasons AS PR
    

    你能试试内连接吗

    FROM PatientClinicActs AS PA
    
    INNER JOIN PatientStatueReasons AS PR 
    
    ON PA.refreason_id = PR.reason_id
    
    WHERE PA.refClinic_id = 25 
    
        3
  •  0
  •   dance2die    15 年前

    对于这种情况,有另一种方法可以获得 最新的 使用记录 SQL Server Ranking functions .

    我已经用过 DENSE_RANK() 我的答案,但你可以用 RANK() 而不是针对你的特定问题。
    ( 笔记 :未测试以下代码。如果要为表提供模式,我将使用示例数据进行测试)

    ;with RankedResult as (
        SELECT  
        PA.refPatient_id
        , PA.datee
        , PR.temporary,
        , PA.statue
        --; Last datee has the lowest rank value of 1,
        , dense_rank() over 
            (partition by PA.refPatient_id order by PA.datee desc) as [Rank]
        FROM    PatientClinicActs AS PA
                join PatientStatueReasons AS PR on PA.refreason_id = PR.reason_id
        WHERE   PA.refClinic_id = 25 
    )
    select  distinct *
    from    RankedResult
    --; Get only the last result.
    where   [Rank] = 1
    
        4
  •  -2
  •   Sparky    15 年前

    将pa.date从group by子句中取出