0
以下語句是我目前正在嘗試確定自約會日期以來已過去的天數的查詢的一部分。在日期/時間查詢中計算大於0的值
, DATEDIFF(D, a.AppointmentDate, GETDATE()) as DaysSinceApptDate
但是,我得到了負面和正面的結果。我需要更改查詢以包含正面結果。所以,任何大於0的東西都是我想要的。
我的整個查詢是:
DECLARE @StartDate DATETIME = '2014-12-1'
SELECT
ROW_NUMBER() OVER(ORDER BY ORDERS.ORDERDATE, PatientProfile.searchname) as RowNumber
, PatientProfile.searchname as PatientName
, PatientProfile.Birthdate
, ORDERS.ORDERDATE as OrderDate
, DATEDIFF(D, ORDERS.ORDERDATE, ORDERS.DB_UPDATED_DATE) AS DaysToCurrentStatus
, ORDERS.DESCRIPTION as ReferralType
, CASE WHEN ORDERS.STATUS = 'U' Then 'Unsigned'
WHEN ORDERS.STATUS = 'H' Then 'Admin Hold'
WHEN ORDERS.STATUS = 'S' Then 'InProcess'
WHEN ORDERS.STATUS = 'C' Then 'Complete'
WHEN ORDERS.STATUS = 'X' Then 'Cancelled'
ELSE NULL END AS Status
, df.ListName as OrderingProvider
, ORDERS.DB_UPDATED_DATE as LastUpdated
, ORDERS.CLINCOMMENTS as Comments
, RIGHT(RTRIM(REPLACE(REPLACE(ORDERS.CLINCOMMENTS,CHAR(10),''),CHAR(13),'')), 18) as AppointmentDate /* Get rid of carriage returns, line breaks, and trailing spaces */
, Coordinator = CASE
WHEN ORDERS.ADMINCOMMENTS like 'Leticia%' then 'Leticia'
WHEN ORDERS.ADMINCOMMENTS = 'Je%' then 'Jessica'
WHEN ORDERS.ADMINCOMMENTS = 'Da%' then 'Dahlia'
WHEN ORDERS.ADMINCOMMENTS = 'sandra' OR ORDERS.ADMINCOMMENTS = 'sc' THEN 'Sandra'
ELSE NULL END
, LOCREG.ABBREVNAME as Location
INTO #TEMP
FROM (CentricityPS.dbo.PatientProfile PatientProfile
INNER JOIN CentricityPS.dbo.ORDERS ORDERS
ON PatientProfile.PId=ORDERS.PID)
INNER JOIN DoctorFacility df
ON ORDERS.AUTHBYUSRID = DF.PVId
INNER JOIN CentricityPS.dbo.LOCREG LOCREG
ON DF.LocationId=LOCREG.LOCID
WHERE ORDERS.ORDERDATE >= @StartDate
AND ORDERS.ORDERTYPE='R' /*Referral*/
AND ORDERS.XID= 1000000000000000000
ORDER BY OrderingProvider
/* Cast only valid dates as AppointmentDates*/
SELECT RowNumber, CAST(AppointmentDate as DateTime) as AppointmentDate
INTO #Appts
FROM #TEMP
WHERE ISDATE(AppointmentDate) = 1
SELECT
t.PatientName
, t.Birthdate
, t.OrderDate
, t.ReferralType
, t.Status
, t.OrderingProvider
, t.Comments
, a.AppointmentDate as AppointmentDate
, DATEDIFF(D, a.AppointmentDate, GETDATE()) as DaysSinceApptDate
, t.Coordinator
, t.Location
FROM #TEMP t
LEFT JOIN #Appts a
ON a.RowNumber = t.RowNumber
DROP TABLE #TEMP,#Appts 有沒有辦法在SQL查詢來做到這一點?
您正在使用哪種RDBMS? – Barranka
'where a.AppointmentDate> GETDATE()'? –