简体   繁体   中英

Access 2003 Query Dilemma

I am, having difficulty with multiple values returning for each user. I have tried variations of Select Distinct to no avail. I need my code to only retun one value per userID.

SELECT Roster.UserID, 
    EventLog.Type, 
    IIf([EventLog]![LogType] Like "Acc*" And [EventLog]![Action] Like "Wri*",[EventLog]![Date],Date()-183) AS [Review Date]
FROM EventLog 
INNER JOIN Roster 
    ON EventLog.UserID = Roster.UserID
GROUP BY Roster.UserID, 
    EventLog.Type, 
    IIf([EventLog]![LogType] Like "Acc*" And [EventLog]![Action] Like "Wri*",[EventLog]![Date],Date()-183)
HAVING (((EventLog.Type) Like "Att*"));

The code is returning both values from the IIf when they meet the first criteria. How can I limit to one or the other?

You're getting multiple values per user because you've listed multiple fields in your GROUP BY clause:

    GROUP BY Roster.UserID, 
        EventLog.Type, 
        IIf([EventLog]![LogType] Like "Acc*" And 
[EventLog]![Action] Like "Wri*",[EventLog]![Date],Date()-183)

I'm not sure why you're grouping at all, as you're not performing any aggregate functions. Would SELECT DISTINCT work?

Thanks for the feedback folks. Basedon what Beth replied and cleaning up my tables and querys to send a copy of the dBasethe problem seemed to work itself out with a small addition of code.

SELECT Roster.UserID, 
EventLog.Type, 
Max(IIf(EventLog!LogType Like "Acc*" And EventLog!Action Like "Wri*",EventLog!Date,Date()-183)) AS [Review Date]
FROM EventLog 
INNER JOIN Roster 
ON EventLog.UserID = Roster.UserID
GROUP BY Roster.UserID,
EventLog.Type
HAVING (((EventLog.Type) Like "Att*"));

The clean up of the multi fields from Beth worked a treat!!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM