我想過濾一個表,首先按日期範圍,然後在該指定的時間段內選擇最大值記錄。下面的代碼顯示超出指定時間段的記錄。在兩個特定日期範圍內過濾SQL表最大記錄
所以在下面的例子中,我指定了1天,但它也顯示2014-11-11的記錄。
declare @BgnDate date = '2014-11-10'
declare @EndDate date ='2014-11-10'
SELECT EmployeeName, TargetTxDate, sum(ActualTarget) as Actual, sum(RequiredTarget) AS ReqTarget, sum(variance) AS Variance
FROM dbo.TargetsTx as TargetsTx
WHERE (Variance IN
(SELECT MAX (Variance) AS Expr1
FROM dbo.TargetsTx AS TargetsTx_1
WHERE (@BgnDate IS NULL OR TargetsTx_1.TargetTxDate >= @BgnDate) AND
(@EndDate IS NULL OR TargetsTx_1.TargetTxDate < DATEADD(d, 1, @EndDate))))
group by EmployeeName,TargetTxDate
你正在研究哪個dbms? MySQL的? MSSQL?甲骨文? ..... – Fred 2014-11-21 11:14:30