So I am in the process of rewriting an MS Access application to a SQL server backend. I am struggling with some unusual behavior in the way Access is using MAX() and how SQL Server is doing it. Maybe it is lack of sleep but I have been staring at this for hours and I can’t figure out why the SQL server results are different.
Sample Data:
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 query:
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
Results in Access:
Account TranID TranType MaxOfTranTime AddID MaxOfClearTime
12345678 55545124 W 12/12/2011 10:48:43 AM CZ36 12/12/2011 9:45:31 AM
The query in SQL server is slightly different in that my ByUsers are stored in a table to minimize typing in tons of queries.
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
The SQL Server results are:
Account TranID TranType MaxOfTranTime AddID MaxOfClearTime
12345678 55545124 W 12/12/2011 10:48:43 AM CZ36 12/12/2011 10:48:43 AM
When I go through this it seems to me that SQL server is correct in getting the MaxOfClearTime of 12/12/2011 10:48:43 AM but am I missing something? Does Access process MAX() differently? I feel like I am losing my mind with this so any other eyes on this would be great.
I put the data you provided in Access and ran the query, and it provided the output I’d expect i.e.
10 > 9.Of course I did assume that both ClearTime and TranTime are being stored as DateTime, If I change them to Text and alter the formatting to match your sample then I get the same results as you i.e.
"9" > "10".Could you check the schema of the table in MS Access?