压缩除少数列之外的2个数据行

时间:2016-07-22 15:55:53

标签: mysql

我有一组显示体育裁判比赛时间表的数据。在几乎所有情况下,每场比赛有2名裁判在一起工作。这是我的疑问:

SELECT DISTINCT g.GroupName, date(s.EventDate) AS EventDate, DATE_FORMAT(s.EventDate,'%H:%i:%s') AS EventTime, l.SiteName, ss.SubSiteName, u.Email, u.FirstName, u.LastName, u.Phone1 
FROM Schedule s 
JOIN Groups g 
ON s.GroupID = g.GroupID 
LEFT OUTER JOIN Sites l 
ON s.SiteID = l.SiteID 
LEFT OUTER JOIN SubSites ss 
ON l.SiteID = ss.SiteID 
LEFT OUTER JOIN Users u 
ON s.UserID = u.UserID 
WHERE g.OrganizationID = 2
AND s.EventDate > DATE_ADD(NOW(), INTERVAL 0 HOUR) 
AND (s.SubSiteID = 0 OR s.SubSiteID = ss.SubSiteID) 
ORDER BY l.SiteName, ss.SubSiteName, s.EventDate, u.LastName

以下是该查询产生的一些记录:

GroupName       EventDate   EvtTime  SiteName               SubSite         Email             FirstName LastName    Phone1
==============================================================================================================================
Hoops Forever   8/10/2016   8:00     Kennedy High School    Court #33 BMJ   ref1@comcast.net  Jeff      Smith       8014445555
Hoops Forever   8/10/2016   8:00     Kennedy High School    Court #33 BMJ   ref2@yahoo.com    Kris      Jones       8013331111
Hoops Forever   8/10/2016   9:20     Kennedy High School    Court #33 BMJ   ref1@comcast.net  Jeff      Smith       8014445555
Hoops Forever   8/10/2016   9:20     Kennedy High School    Court #33 BMJ   ref2@yahoo.com    Kris      Jones       8013331111
Hoops Forever   8/10/2016   10:40    Kennedy High School    Court #33 BMJ   ref1@comcast.net  Jeff      Smith       8014445555
Hoops Forever   8/10/2016   10:40    Kennedy High School    Court #33 BMJ   ref3@yahoo.com    Kassy     Hanks       8019997777
Hoops Forever   8/10/2016   12:00    Kennedy High School    Court #33 BMJ   ref4@gmail.com    Michael   Rogers      8010001111
Hoops Forever   8/10/2016   12:00    Kennedy High School    Court #33 BMJ   ref2@yahoo.com    Kris      Jones       8013331111
Hoops Forever   8/10/2016   13:20    Kennedy High School    Court #33 BMJ   ref4@gmail.com    Michael   Rogers      8010001111
Hoops Forever   8/10/2016   13:20    Kennedy High School    Court #33 BMJ   ref2@yahoo.com    Kris      Jones       8013331111

现在查看前2行并注意除了最后4列(特定于裁判)之外,2行之间的所有值都相同。所以我尝试添加这个:

GROUP BY GroupName, EventDate, EvtTime, SiteName, SubSite

那让我走到了一半,但是我在每个分组中输掉了第二名裁判:

GroupName       EventDate   EvtTime  SiteName               SubSite         Email             FirstName LastName    Phone1
==============================================================================================================================
Hoops Forever   8/10/2016   8:00     Kennedy High School    Court #33 BMJ   ref1@comcast.net  Jeff      Smith       8014445555
Hoops Forever   8/10/2016   9:20     Kennedy High School    Court #33 BMJ   ref1@comcast.net  Jeff      Smith       8014445555
Hoops Forever   8/10/2016   10:40    Kennedy High School    Court #33 BMJ   ref1@comcast.net  Jeff      Smith       8014445555
Hoops Forever   8/10/2016   12:00    Kennedy High School    Court #33 BMJ   ref4@gmail.com    Michael   Rogers      8010001111
Hoops Forever   8/10/2016   13:20    Kennedy High School    Court #33 BMJ   ref4@gmail.com    Michael   Rogers      8010001111

不用多说,这就是我真正需要的东西:

GroupName       EventDate   EvtTime  SiteName               SubSite         Email1            FN1       LN1      Phone1       EMail2          FN2     LN2      Phone2
=========================================================================================================================================================================
Hoops Forever   8/10/2016   8:00     Kennedy High School    Court #33 BMJ   ref1@comcast.net  Jeff      Smith    8014445555   ref2@yahoo.com  Kris    Jones    8013331111
Hoops Forever   8/10/2016   9:20     Kennedy High School    Court #33 BMJ   ref1@comcast.net  Jeff      Smith    8014445555   ref2@yahoo.com  Kris    Jones    8013331111
etc...........

请注意,在我想要的结果集中,4个不同的列(ref-specific而不是游戏特定的)被附加到"压缩"行。逻辑上它是有道理的:有1个游戏,但有2个参考。我希望1个游戏的信息在1行中,我希望2个参考的信息也在同一行,附加到结尾。在实际的DB中有2行,但我希望它们合并并附加。如果没有裁判信息,我只想要空白/空白 - 没什么特别的。

那么为此构建的sql是怎么样的?非常感谢提前!

1 个答案:

答案 0 :(得分:1)

如果可以将两个引用存储在同一列中,则可以使用:

GROUP_CONCAT(column SEPARATOR ',') 

确定组中只有两行时的另一种解决方案:

  1. 获取第一个的最大值(电子邮件)和第二个的最小值(电子邮件)
  2. 通过电子邮件从已加入的表中获取其余的裁判字段。