为单个用户选择一个不同的MAX NoteID

时间:2019-05-08 20:00:53

标签: sql sql-server tsql greatest-n-per-group

我正在尝试在SQL Server中生成带有多个JOIN语句的查询,该查询为我们数据库中的人才(又名用户)提供最新的noteid,以及其他一些数据点,例如人才名称,类别,状态,部门和我查询中的followupdate。

到目前为止,我已经尝试了以下查询,但是我继续收到同一人才的多条记录。

SELECT 
    MAX(n.NoteID) as Note, 
    t.FirstName, t.LastName, s.Name AS Status, 
    tc.Name AS Category, d.Name, n.FollowUpDate AS FollowUpDate
FROM 
    Notes n
JOIN 
    TalentNotes tn ON n.NoteID = tn.NoteID
JOIN 
    Talents t ON t.TalentID = tn.TalentID
JOIN 
    TalentStatuses s ON s.TalentStatusID = t.TalentStatusID
JOIN 
    TalentCategories tc ON tc.TalentCategoryID = t.TalentCategoryID
JOIN 
    Divisions d ON d.DivisionID = t.DivisionID
WHERE 
    tc.Name = 'G1'
    AND t.EmailAddress NOT LIKE '%x3%'
    OR tc.Name = 'X1'
    AND t.EmailAddress NOT LIKE '%x3%'
GROUP BY 
    t.FirstName, t.LastName, s.Name, tc.Name, d.name, n.FollowUpDate

结果:

enter image description here

最终,我希望看到一个表格,该表格显示了具有最大NoteID的不同才能,但是目前我继续为同一人才收到多个不同的NoteID。在此方面的任何帮助将不胜感激!

4 个答案:

答案 0 :(得分:2)

尝试一下:

SELECT A.NoteId, A.FirstName, A.LastName, A.Status, A.Category, A.Name, A.FollowUpDate
FROM (
SELECT n.NoteId, t.FirstName, t.LastName, s.Name as Status, tc.Name as Category, d.Name, n.FollowUpDate as FollowUpDate, 
row_number() over (partition by t.FirstName, t.LastName order by n.NoteId DESC) rnk
    FROM Notes n
    JOIN TalentNotes tn ON n.NoteID=tn.NoteID
    JOIN Talents t ON t.TalentID=tn.TalentID
    JOIN TalentStatuses s ON s.TalentStatusID=t.TalentStatusID
    JOIN TalentCategories tc ON tc.TalentCategoryID=t.TalentCategoryID
    JOIN Divisions d ON d.DivisionID=t.DivisionID
    WHERE tc.Name = 'G1'
    AND t.EmailAddress NOT LIKE '%x3%'
    OR tc.Name = 'X1'
    AND t.EmailAddress NOT LIKE '%x3%')A
    where A.rnk = 1

答案 1 :(得分:0)

尝试通过以下方法从该组中删除follupdate:

SELECT MAX(n.NoteID) as Note, t.FirstName, t.LastName, s.Name as Status, tc.Name as Category, d.Name
FROM Notes n
JOIN TalentNotes tn ON n.NoteID=tn.NoteID
JOIN Talents t ON t.TalentID=tn.TalentID
JOIN TalentStatuses s ON s.TalentStatusID=t.TalentStatusID
JOIN TalentCategories tc ON tc.TalentCategoryID=t.TalentCategoryID
JOIN Divisions d ON d.DivisionID=t.DivisionID
WHERE tc.Name = 'G1'
AND t.EmailAddress NOT LIKE '%x3%'
OR tc.Name = 'X1'
AND t.EmailAddress NOT LIKE '%x3%'
GROUP BY t.FirstName, t.LastName, s.Name, tc.Name, d.name

答案 2 :(得分:0)

您需要确定如何处理后续日期。就目前而言,它是按分组的,因此每个不同的日期您将与其他组成员一排。

也许:

MAX(n.FollowUpDate) as FollowUpDate

答案 3 :(得分:0)

看起来像按“跟进日期”分组是让您感到困惑的原因。我建议获取CTE中每个人才的最大笔记ID,然后选择所需的列并加入CTE:

WITH Max_Note AS (
    SELECT MAX(n.NoteID) AS NoteID, tn.TalentID
    FROM Notes n
    JOIN TalentNotes tn ON n.NoteID=tn.NoteID
    GROUP BY tn.TalentID
)
SELECT n.NoteID as Note, t.FirstName, t.LastName, s.Name as Status, tc.Name as Category, d.Name, n.FollowUpDate as FollowUpDate
FROM Max_Note n
JOIN TalentNotes tn ON n.NoteID=tn.NoteID
JOIN Talents t ON t.TalentID=tn.TalentID
JOIN TalentStatuses s ON s.TalentStatusID=t.TalentStatusID
JOIN TalentCategories tc ON tc.TalentCategoryID=t.TalentCategoryID
JOIN Divisions d ON d.DivisionID=t.DivisionID
WHERE tc.Name = 'G1'
AND t.EmailAddress NOT LIKE '%x3%'
OR tc.Name = 'X1'
AND t.EmailAddress NOT LIKE '%x3%'