简体   繁体   中英

Combining Rows in MS Access SQL Query

Below I have a screenshot of a query in MS Access. I am trying to combine the two "Kim Wong" rows into one row that has one time range in the Monday column and the other time range in the Tuesday column. There will when done have time ranges in each of the cells.

在此处输入图片说明

Here is what the query looks like.

在此处输入图片说明

And here is the actual code

SELECT qryEmployed.EmployeeName, First(IIf([EventDate]=DateAdd("d",0,[Forms]![tblEvents]![tbxDate]),IIf([Event]="Scheduled",Format([StartTime],"h:nn") & " - " & Format([EndTime],"h:nn"),[Event]),"")) AS Monday, First(IIf([EventDate]=DateAdd("d",1,[Forms]![tblEvents]![tbxDate]),IIf([Event]="Scheduled",Format([StartTime],"h:nn") & " - " & Format([EndTime],"h:nn"),[Event]),"")) AS Tuesday, First(IIf([EventDate]=DateAdd("d",2,[Forms]![tblEvents]![tbxDate]),IIf([Event]="Scheduled",Format([StartTime],"h:nn") & " - " & Format([EndTime],"h:nn"),[Event]),"")) AS Wednesday, First(IIf([EventDate]=DateAdd("d",3,[Forms]![tblEvents]![tbxDate]),IIf([Event]="Scheduled",Format([StartTime],"h:nn") & " - " & Format([EndTime],"h:nn"),[Event]),"")) AS Thursday, First(IIf([EventDate]=DateAdd("d",4,[Forms]![tblEvents]![tbxDate]),IIf([Event]="Scheduled",Format([StartTime],"h:nn") & " - " & Format([EndTime],"h:nn"),[Event]),"")) AS Friday, First(IIf([EventDate]=DateAdd("d",5,[Forms]![tblEvents]![tbxDate]),IIf([Event]="Scheduled",Format([StartTime],"h:nn") & " - " & Format([EndTime],"h:nn"),[Event]),"")) AS Saturday, First(IIf([EventDate]=DateAdd("d",6,[Forms]![tblEvents]![tbxDate]),IIf([Event]="Scheduled",Format([StartTime],"h:nn") & " - " & Format([EndTime],"h:nn"),[Event]),"")) AS Sunday
FROM tblEvents INNER JOIN qryEmployed ON tblEvents.Employee = qryEmployed.EmployeeName
GROUP BY qryEmployed.EmployeeName, qryEmployed.Position, tblEvents.Event, tblEvents.EventDate, tblEvents.StartTime, tblEvents.EndTime, tblEvents.Lunch, IIf([Event]="Scheduled",((IIf([EndTime]<[StartTime],1,0)+[EndTime])-[StartTime]-IIf([Lunch],1/48,0))*24,0)
HAVING (((tblEvents.EventDate)=DateAdd("d",0,[Forms]![tblEvents]![tbxDate]))) OR (((tblEvents.EventDate)=DateAdd("d",1,[Forms]![tblEvents]![tbxDate]))) OR (((tblEvents.EventDate)=DateAdd("d",2,[Forms]![tblEvents]![tbxDate]))) OR (((tblEvents.EventDate)=DateAdd("d",3,[Forms]![tblEvents]![tbxDate]))) OR (((tblEvents.EventDate)=DateAdd("d",4,[Forms]![tblEvents]![tbxDate]))) OR (((tblEvents.EventDate)=DateAdd("d",5,[Forms]![tblEvents]![tbxDate]))) OR (((tblEvents.EventDate)=DateAdd("d",6,[Forms]![tblEvents]![tbxDate])));

Here is a sample of tblEmployees . 在此处输入图片说明

Here is the qryEmployed . 在此处输入图片说明

And here is the tblEvents . 在此处输入图片说明

It's a little hard to tell based on all the gunk that access is throwing in your actual sql, but I believe that you are getting this issue because you are starting your query with the Event table and then joining into the qryEmployed table. Joining in that fashion is creating two rows because Kim Wong is assigned to two different events, hence her name gets entered onto two different rows. You need to drop the Event column from your table as a start, that will likely fix it.

If you could show the whole table returned or at least the query tables it would go a long way in helping further diagnose what to change.

I agree with hdizzle, your query is incredibly hard to read because it seems it was generated from the MS Access GUI. I highly suggest you avoid the GUI and learn SQL syntax directly.

Your query is essentially aggregating among multiple values that return if the individual has a value for each day. You need to 'group by' all returned columns that are not being aggregated.

I re-formatted the query a bit and I removed all of the extraneous GROUP BY clauses that I don't believe do anything. Then I added an aggregate clause to each 'DayOfTheWeek' return column. The query below should return the time an event occurred for the individual if an event occurred or it should return a blank. If multiple events occurred, it would return the last one alphabetically (you never explained how you wanted that scenario to be handled).

Further, you are linking on the name field, I highly suggest you link on an ID field. Names change relatively often and I am fairly sure linking on an integer has better performance.

Try something like

SELECT qryEmployed.EmployeeName, 
MAX(First(
    IIf(EventDate)]=DateAdd("d",0,[Forms]![tblEvents]![tbxDate]), IIf([Event]="Scheduled",Format([StartTime],"h:nn") & " - " & Format([EndTime],"h:nn"),[Event]),"")
)) AS Monday,
MAX(First(
    IIf([EventDate]=DateAdd("d",1,[Forms]![tblEvents]![tbxDate]),IIf([Event]="Scheduled",Format([StartTime],"h:nn") & " - " & Format([EndTime],"h:nn"),[Event]),"")
)) AS Tuesday,
MAX(First(
    IIf([EventDate]=DateAdd("d",2,[Forms]![tblEvents]![tbxDate]),IIf([Event]="Scheduled",Format([StartTime],"h:nn") & " - " & Format([EndTime],"h:nn"),[Event]),"")
)) AS Wednesday, 
MAX(First(
    IIf([EventDate]=DateAdd("d",3,[Forms]![tblEvents]![tbxDate]),IIf([Event]="Scheduled",Format([StartTime],"h:nn") & " - " & Format([EndTime],"h:nn"),[Event]),"")
)) AS Thursday,
MAX(First(
    IIf([EventDate]=DateAdd("d",4,[Forms]![tblEvents]![tbxDate]),IIf([Event]="Scheduled",Format([StartTime],"h:nn") & " - " & Format([EndTime],"h:nn"),[Event]),"")
)) AS Friday, 
MAX(First(
    IIf([EventDate]=DateAdd("d",5,[Forms]![tblEvents]![tbxDate]),IIf([Event]="Scheduled",Format([StartTime],"h:nn") & " - " & Format([EndTime],"h:nn"),[Event]),"")
)) AS Saturday, 
MAX(First(
    IIf([EventDate]=DateAdd("d",6,[Forms]![tblEvents]![tbxDate]),IIf([Event]="Scheduled",Format([StartTime],"h:nn") & " - " & Format([EndTime],"h:nn"),[Event]),"")
)) AS Sunday
FROM tblEvents INNER JOIN qryEmployed ON tblEvents.Employee = qryEmployed.EmployeeName
GROUP BY 
qryEmployed.EmployeeName

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