通过显示重复项进行分组

时间:2019-08-16 13:09:09

标签: sql sql-server grouping

我正在尝试为此会议应用选择行。 会议有一个或多个议程项目,这些议程项目具有一个EventNumber(如果未链接任何事件,则为0;如果有链接,则为整数)

我每次会议只选择一行,但是如果在同一次会议上我们有一个议程项目的EventNumber为0,而另一个不同于0,则我得到的行不止一次。我只需要知道整个过程是否会议中,至少一个AgendaItems的EventNumber不为零。

查询即将到来。当会议仅具有EventNumbers = 0的AgendaItems时,它将起作用,但是当会议同时具有两者的混合时,它将带回重复项。

试图在分组之后添加HAVING COUNT(*)> 1,或者说MAX(MeetingAgendaItem.EventNumber),并在SELECt之后应用DISTNCT关键字。

SELECT
    Meeting.Id, 
    Meeting.Location, 
    Meeting.Name, 
    MeetingAgendaItem.EventNumber,
    Meeting.StartDate,
    Meeting.EndDate

FROM ((Meeting 
    LEFT JOIN MeetingInvitees ON (Meeting.Id = MeetingInvitees.MeetingId))
    LEFT JOIN MeetingAgendaItem ON (Meeting.Id = MeetingAgendaItem.MeetingId))

GROUP BY Meeting.Id, Meeting.Location, Meeting.Name, MeetingAgendaItem.EventNumber, Meeting.StartDate, Meeting.EndDate

ORDER BY Meeting.Id
Id  Location Name                   Meeting Name        EventNumber          StartDate              EndDate
19  Pont des Charrettes, 30700, FR  Meeting Generated   0                    29-06-2019 00:00:00    29-06-2019 23:59:59
19  Pont des Charrettes, 30700, FR  Meeting Generated   8747                 29-06-2019 00:00:00    29-06-2019 23:59:59

4 个答案:

答案 0 :(得分:1)

您可以采取以下措施来解决此问题:将MAX(MeetingAgendaItem.EventNumber)GROUP BY中删除。这将为您提供与议程项目相关联的最大EventNumber(如果它们全为0,则为0),这似乎符合您问题中的标准。将查询更改为此:

SELECT
    Meeting.Id, 
    Meeting.Location, 
    Meeting.Name, 
    MAX(MeetingAgendaItem.EventNumber),
    Meeting.StartDate,
    Meeting.EndDate

FROM ((Meeting 
    LEFT JOIN MeetingInvitees ON (Meeting.Id = MeetingInvitees.MeetingId))
    LEFT JOIN MeetingAgendaItem ON (Meeting.Id = MeetingAgendaItem.MeetingId))

GROUP BY Meeting.Id, Meeting.Location, Meeting.Name, Meeting.StartDate, Meeting.EndDate

ORDER BY Meeting.Id

答案 1 :(得分:0)

这应该有效:

select * from (
SELECT
    ROW_NUMBER() over (partition by Meeting.Id,Meeting.Location,Meeting.Name, Meeting.StartDate,Meeting.EndDate order by Meeting.Location) rn,
    Meeting.Id, 
    Meeting.Location, 
    Meeting.Name, 
    MeetingAgendaItem.EventNumber,
    Meeting.StartDate,
    Meeting.EndDate

FROM ((Meeting 
    LEFT JOIN MeetingInvitees ON (Meeting.Id = MeetingInvitees.MeetingId))
    LEFT JOIN MeetingAgendaItem ON (Meeting.Id = MeetingAgendaItem.MeetingId))

GROUP BY Meeting.Id, Meeting.Location, Meeting.Name, MeetingAgendaItem.EventNumber, Meeting.StartDate, Meeting.EndDate

ORDER BY Meeting.Id
) t where rn=1

答案 2 :(得分:0)

您可能还需要在联接上添加“ MeetingAgendaItem.EventNumber> 0”条件:

SELECT
Meeting.Id, 
Meeting.Location, 
Meeting.Name, 
MeetingAgendaItem.EventNumber,
Meeting.StartDate,
Meeting.EndDate

FROM ((Meeting 
LEFT JOIN MeetingInvitees ON (Meeting.Id = MeetingInvitees.MeetingId))
LEFT JOIN MeetingAgendaItem ON (Meeting.Id = MeetingAgendaItem.MeetingId AND MeetingAgendaItem.EventNumber > 0 ))

GROUP BY Meeting.Id, Meeting.Location, Meeting.Name, MeetingAgendaItem.EventNumber, 
Meeting.StartDate, Meeting.EndDate

ORDER BY Meeting.Id

答案 3 :(得分:0)

我建议一个相关的子查询:

SELECT m.*
       (SELECT MAX(mai.EventNumber)
        FROM MeetingAgendaItem mai
        WHERE m.Id = mai.MeetingId
       ) as max_EventNumber          
FROM Meeting m
ORDER BY m.Id;

注意:

  • 您没有使用被邀请者表,因此我将其删除。
  • 表别名使查询更易于编写和阅读。
  • 被邀请者表是重复的原因之一。您每次会议需要一行,因此不需要外部GROUP BY
  • 这应该比您的版本效率更高,因为它消除了不必要的重复记录和外部group by