开发者

SQL Procedure: Selecting Highest DATE within INNER JOIN

开发者 https://www.devze.com 2023-02-22 17:56 出处:网络
I currently have the following procedure: CREATE TABLE #Appointments ( [aptUniqueID] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,

I currently have the following procedure:

        CREATE TABLE #Appointments (
    [aptUniqueID] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [aptTime] datetime,
    [aptWorkToDo] [varchar] (512) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [aptStateDispChar] [varchar] (1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [aptStateTextColor] [varchar] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [aptStateBgcolor] [varchar] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [aptPatientID] [varchar] (8) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,     
    [aptPatientFullName] [varchar] (130) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [aptEntryTime开发者_如何学C] datetime,
) ON [PRIMARY]

-- Insert appointments data into temp table
INSERT INTO #Appointments  ( aptUniqueID, aptTime, aptWorkToDo, aptStateDispChar, aptStateTextColor, aptStateBgcolor, aptPatientID, aptPatientFullName, aptEntryTime )
SELECT  CONVERT(varchar(36), apt.UniqueID), 
        apt.atime, 
        RTRIM(apt.apwork) + ' ' + RTRIM(apt.apwrk2),
        aps.apsdispchar,
        RTRIM(LTRIM(aps.apstextcolor)),
        RTRIM(LTRIM(aps.apsbgcolor)),
        apT.apid,
        dbo.MakeCaseString(pat.pfname, pat.pfnamcase) + ' ' + dbo.MakeCaseString(pat.plname, pat.plnamcase),
        apn.apnentrytime
        FROM apt INNER JOIN pat ON pat.pid = apt.apid INNER JOIN aps on ((apt.aconfstat IS NOT NULL AND apt.aconfstat = aps.apsid) OR (apt.aconfstat IS NULL AND aps.apsid = ' ')) INNER JOIN apn ON (apn.apnpid = apt.apid AND apn.apndate = apt.adate AND apn.apntime = apt.atime)
WHERE apt.adid = @ProviderIDParam   AND apt.adate = @DateParam
ORDER BY apt.atime ASC

Inside my Inner Join how can I return the MAX() date from apn.apnentrytime (DATETIME Field)?

At the moment, it's returning the two rows of data when I only want it to return the row with the highest (most recent) apnentrytime.


Something like:

WHERE apn.apnentrytime = MAX(apn.apnentrytime)


select  convert(varchar(36), apt.uniqueid), 
        apt.atime, 
        rtrim(apt.apwork) + ' ' + rtrim(apt.apwrk2),
        aps.apsdispchar,
        rtrim(ltrim(aps.apstextcolor)),
        rtrim(ltrim(aps.apsbgcolor)),
        apt.apid,
        dbo.makecasestring(pat.pfname, pat.pfnamcase) + ' ' + dbo.makecasestring(pat.plname, pat.plnamcase),
        apn.apnentrytime
from apt 
inner join pat on pat.pid = apt.apid 
inner join aps on
    (apt.aconfstat is not null and apt.aconfstat = aps.apsid) 
    or 
    (apt.aconfstat is null and aps.apsid = ' ')
inner join (
    select apnpid, apndate, apntime, max(apnentrytime) as apnentrytime
    from apn
    group by apnpid, apndate, apntime
    ) apn on (apn.apnpid = apt.apid and apn.apndate = apt.adate and apn.apntime = apt.atime)
where apt.adid = @provideridparam and apt.adate = @dateparam
order by apt.atime asc
0

精彩评论

暂无评论...
验证码 换一张
取 消