【问题标题】:to get unique records from table after join加入后从表中获取唯一记录
【发布时间】:2014-02-04 10:48:42
【问题描述】:

我有表 eventsessionsessionspeakerspeakerEmployee event 表有 EventID 作为 PKsession 表可以有多个 Eventid 并且有 sessionid 作为 pkspeaker 表有 speakerid 作为 pk 并且有 empID 列,employee 表的 empIDPKsessionspeaker 表有 (sessionid, speakerid) 作为 pk。 现在我想拥有所有带有 Topmost emplID 的 eventid,这样事件 id 是唯一的

SELECT     emp.EmployeeID, e.EventID
FROM    Event AS e INNER JOIN Session ON e.EventID = Session.EventID left outer JOIN
SessionSpeaker ON Session.SessionID = SessionSpeaker.SessionID INNER JOIN
Speaker ON SessionSpeaker.EmployeeID = Speaker.EmployeeID INNER JOIN
Employee AS emp ON Speaker.EmployeeID = emp.EmployeeID 

这个查询返回

EmployeeID   EventID
5351         310
5351         310
5352         299
5352         300
5352         301
5352         302
5352             303
5352         314
5352         315

这是我不想要的,我想要唯一的事件 ID 和最顶层的员工 ID 请让我知道一个简单的方法来解决这个问题。 将在这件事上进行分组,有或计算帮助

【问题讨论】:

    标签: sql sql-server join


    【解决方案1】:

    我想要唯一的事件 ID 和最顶层的员工 ID

    只需GROUP BY e.EventID 并选择MAX(EmployeeId)

    SELECT 
      e.EventID, 
      MAX(emp.EmployeeID) AS TopMostEmpId, 
    FROM    Event AS e 
    INNER JOIN Session         ON e.EventID                 = Session.EventID 
    INNER JOIN SessionSpeaker  ON Session.SessionID         = SessionSpeaker.SessionID 
    INNER JOIN Speaker         ON SessionSpeaker.EmployeeID = Speaker.EmployeeID 
    INNER JOIN Employee AS emp ON Speaker.EmployeeID        = emp.EmployeeID
    GROUP BY e.EventID
    

    如果您想选择比eventIdemployeeId 更多的列,您可以使用类似这样的排名函数:

    WITH CTE AS
    (
        SELECT 
          e.EventID, 
          emp.EmployeeID,
          e.EventName,
          emp.EmployeeName,
          ...
          ROW_NUMBER() OVER(PARTITION BY e.EventID 
                            ORDER BY emp.EmployeeId DESC) AS RN
        FROM    Event AS e 
        INNER JOIN Session         ON e.EventID                 = Session.EventID 
        INNER JOIN SessionSpeaker  ON Session.SessionID         = SessionSpeaker.SessionID 
        INNER JOIN Speaker         ON SessionSpeaker.EmployeeID = Speaker.EmployeeID 
        INNER JOIN Employee AS emp ON Speaker.EmployeeID        = emp.EmployeeID
    ) 
    SELECT *
    FROM CTE
    WHERE RN = 1;
    

    【讨论】:

    • 谢谢 wa Jazakallah ukhair,第一个查询没问题,但是第二个查询消除了所有 RN > 1 的记录。但是我希望显示每个 RN > 1 的第一条记录
    • @shomaail El 3afo 我的兄弟,随时欢迎你:) 是的,第二个查询消除了所有有RN > 1 的记录,因为我写了WHERE RN = 1,但是你为什么要有RN > 的记录?你想达到什么目标?
    • 正如我在问题中所写,我只想要最上面的员工 ID
    • @shomaail - WHERE RN = 1 应该只给你最上面的employeeID,注意ORDER BY 应该是DESC 我错过了,看我的编辑。你试过了吗?
    【解决方案2】:
    SELECT     emp.EmployeeID, distinct e.EventID
    FROM    Event AS e INNER JOIN Session ON e.EventID = Session.EventID left outer JOIN
    SessionSpeaker ON Session.SessionID = SessionSpeaker.SessionID INNER JOIN
    Speaker ON SessionSpeaker.EmployeeID = Speaker.EmployeeID INNER JOIN
    Employee AS emp ON Speaker.EmployeeID = emp.EmployeeID 
    
    group by e.EventID
    

    【讨论】:

    • Msg 156, Level 15, State 1, Line 1 关键字“distinct”附近的语法不正确。
    • 这不行,你不能这样使用distinct。此外,它不会给出预期的结果。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-03
    • 1970-01-01
    • 2012-01-30
    • 2011-10-26
    • 1970-01-01
    相关资源
    最近更新 更多