2009-11-20 104 views
1

该查询T-Sql,如何获得这些结果?

 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_id都有最新的行。 我应该运行什么来获得这些结果?

+0

你想获得最新的一个给定refPatient_Id?做一个rank()并选择rank == 1应该工作的位置 – 2009-11-20 11:52:08

+0

我想获取每个refPatient_id的最新行。你能写一个排名榜吗? – uzay95 2009-11-20 12:34:56

+0

您的14708结果:他们在第一个结果集中看起来不对。您不应该使用那个查询结果 – gbn 2009-11-20 12:35:06

回答

3

尝试是这样的

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 

你需要让每名患者的最大日期,然后再加入了这一点。

+0

对于某些患者它仍然有不止一行。 – uzay95 2009-11-20 11:56:45

+0

Tha可能是由于PatientClinicActs中出现多次MAX日期或包含针对患者的多个入口的PatientStatueReasons的链接,在这种情况下,您必须决定如何选择适当的记录。什么使它成为你需要的一个? – 2009-11-20 12:01:16

+0

我投你失望是因为我误读了......显然无法撤销它,所以我投你一票'为了公平而以最高价格检索行' – Peter 2009-11-20 12:07:04

-2

就拿PA.datee出一批by子句

+0

它没有工作。 – uzay95 2009-11-20 11:52:45

1

取而代之的是交叉的加入

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 
+0

即使它不能解决问题,也能为好的做法+1 +1 – gbn 2009-11-21 09:20:50

+0

非常感谢您的先生。我从你身上学到了很多东西,并且还在学习中。你的评论对我来说总是很有价值。再次感谢 – 2009-11-21 10:41:32

+0

它不是交叉连接 - 它是使用非ANSI JOIN语法的内部连接。 – 2009-11-23 05:00:45

0

对于这种情况下,还有另外一种方法,通过使用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