所以我正在将MS Access应用程序重写为SQL Server后端。我正在努力解决Access使用MAX()
的方式以及SQL Server如何执行它的一些异常行为。可能是睡眠不足但我一直盯着这几个小时,我无法弄清楚为什么SQL服务器的结果不同。
示例数据:
Acct ByUser TranType TranID AddID ClearTime TranTime
12345678 CZ12 W 55545124 CZ36 12/12/2011 9:45:31 AM 12/12/2011 9:45:31 AM
12345678 CZ24 W 55545124 CZ36 12/12/2011 10:01:26 AM 12/12/2011 10:01:26 AM
12345678 CZ36 W 55545124 CZ36 12/12/2011 9:45:31 AM 12/12/2011 9:45:31 AM
12345678 MG12 W 55545124 CZ36 12/12/2011 10:48:43 AM 12/12/2011 10:48:43 AM
12345678 CZ25 W 55545124 CZ36 12/12/2011 9:45:31 AM 12/12/2011 9:45:31 AM
MS Access查询:
SELECT Acct, TypeID, TranType
, Max(TranTime) AS MaxOfTranTime
, AddID
, Max(ClearTime) AS MaxOfClearTime
FROM Cleared
WHERE
(
((ByUser) Like "CZ*" Or (ByUser) Like "TR*" Or
(ByUser) Like "RR*" Or (ByUser) Like "MG*" Or
(ByUser) Like "RN*" Or (ByUser) Like "PS*" Or
(ByUser) Like "OP*" Or (ByUser) Like "JA*" Or
(ByUser) Like "IC*" Or (ByUser) Like "IB*" Or
(ByUser) Like "FO*" Or (ByUser) Like "DV*" Or
(ByUser) Like "CD*" Or (ByUser) Like "BO*" Or
(ByUser) Like "2D*")
)
GROUP BY Acct, TypeID, TranType, AddID
访问结果:
Account TranID TranType MaxOfTranTime AddID MaxOfClearTime
12345678 55545124 W 12/12/2011 10:48:43 AM CZ36 12/12/2011 9:45:31 AM
SQL Server中的查询略有不同,因为我的ByUsers存储在一个表中,以最大限度地减少大量查询的输入。
SELECT C.Acct
, C.RequestId
, C.TypeCode
, Max(C.TranTime) as MaxTranTime
, C.AddUserId
, Max(C.ClearDate) As MaxClearDate
FROM Cleared C
WHERE EXISTS (SELECT *
FROM UserIdFilter U
WHERE ByUserId LIKE U.UserId)
GROUP BY Acct, RequestId, TypeCode, AddUserId
SQL Server结果是:
Account TranID TranType MaxOfTranTime AddID MaxOfClearTime
12345678 55545124 W 12/12/2011 10:48:43 AM CZ36 12/12/2011 10:48:43 AM
当我经历这一点时,在我看来,SQL服务器在获取MaxOfClearTime 12/12/2011 10:48:43 AM是正确的,但我错过了什么? Access处理MAX()
的处理方式有何不同?我觉得我正在失去理智,所以任何其他的眼睛都会很棒。
答案 0 :(得分:1)
我将您在Access中提供的数据放入并运行查询,并提供了我期望的输出,即10 > 9
。
当然我确实假设ClearTime和TranTime都存储为DateTime,如果我将它们更改为Text并更改格式以匹配您的样本,那么我得到的结果与您"9" > "10"
相同。
您可以在MS Access中检查表的架构吗?