我目前有以下程序:
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] 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
在Inner Join中如何从apn.apnentrytime(DATETIME Field)返回MAX()日期?
目前,当我只希望它返回具有最高(最近)apnentrytime的行时,它返回两行数据。
答案 0 :(得分:0)
类似的东西:
WHERE apn.apnentrytime = MAX(apn.apnentrytime)
答案 1 :(得分:0)
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