多联接中的子查询

时间:2018-06-26 21:51:32

标签: subquery

我对SQL还是很陌生,并且享受新的学习曲线,尽管它可能还很晚。 我希望有人能帮助我。

我的桌子Assessment

AssessmentID    Serial  Episode    EventID  AssessmentDate
------------------------------------------------------------
592              628     1          6043    05/06/2017 0:00
593              628     1          6047    05/09/2017 0:00
595              628     1          6050    12/07/2017 0:00
615              628     1          6173    28/02/2018 0:00

这是我的查询:非常感谢其中有许多联接,它运行得很好,除了我需要缩小Assessment表中的某些记录。

SELECT    
    Client.KeyworkerName, Client.Service, Client.Serial, Client.Forename, 
    Client.Surname, Client.CurrentEpisode AS CE,Client.Status, 
    Referral.SubstitutePrescribingDetails   AS 'PFR' , 
    Assessment.StageClientBeginningMonth    AS 'Stage Start Month' ,
    Assessment.StageClientEndMonth          AS 'Stage End Month',
    Assessment.StageClientCompleted         AS 'Stages Completed',
    GP.Surgery, 
    Referral.ReferralDate
FROM 
    DADB.dbo.Client Client 
LEFT OUTER JOIN 
    DADB.dbo.Referral Referral ON Client.Serial = Referral.Serial 
                               AND Referral.Episode = Client.CurrentEpisode   
LEFT JOIN 
    DADB.dbo.GP GP ON GP.GPID = Referral.GPID 
                   AND Referral.Episode = Client.CurrentEpisode   
LEFT OUTER JOIN 
    DADB.dbo.Assessment Assessment ON Client.Serial = Assessment.Serial 
                                   AND Assessment.Episode = Client.CurrentEpisode
WHERE
    client.service <> 'Training' 
ORDER BY
    Serial DESC

我的问题是我只想包括Assessment表中的一条记录,这应该是AssessmentID最高的一条。我只是看不到在哪里或如何构造子查询。

对于客户Michael,我从客户文件中获取了4条记录,我知道那是因为他有4条Assessment记录。

在此先感谢您的帮助

0 个答案:

没有答案