简体   繁体   English

在MS Access SQL查询中合并行

[英]Combining Rows in MS Access SQL Query

Below I have a screenshot of a query in MS Access. 下面是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. 我想这两个“金皇”行合并到具有在一个时间范围中的一行Monday列,并在其他时间段Tuesday列。 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 . 这是tblEmployees的示例。 在此处输入图片说明

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

And here is the tblEvents . 这是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. 基于所有麻烦,很难说出访问实际上是在您的sql中抛出的,但是我相信您会遇到此问题,因为您是使用Event表开始查询,然后加入qryEmployed表。 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. 以这种方式加入会创建两行,因为Kim Wong被分配给两个不同的事件,因此她的名字被输入到两个不同的行中。 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. 我同意hdizzle,您的查询难以理解,因为它似乎是从MS Access GUI生成的。 I highly suggest you avoid the GUI and learn SQL syntax directly. 我强烈建议您避免使用GUI并直接学习SQL语法。

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. 我对查询进行了重新格式化,并删除了所有我不相信会做的无关紧要的GROUP BY子句。 Then I added an aggregate clause to each 'DayOfTheWeek' return column. 然后,我向每个“ DayOfTheWeek”返回列添加了一个聚合子句。 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. 此外,您在名称字段上进行链接,我强烈建议您在ID字段上进行链接。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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