【问题标题】:Sql Server - Query help (group by with having MAX)Sql Server - 查询帮助(按 MAX 分组)
【发布时间】:2011-05-06 07:42:16
【问题描述】:

嗨,

我的一个查询遇到了很大的问题,这是我表中的数据:

entityId             groupId              groupDepth
-------------------- -------------------- -----------
NULL                 1090                 0
56                   1090                 1
222                  1090                 1
226                  1090                 1
227                  1090                 1
228                  1090                 1
234                  1090                 1
248                  1090                 2
249                  1090                 2
250                  1090                 2
251                  1090                 2
252                  1090                 1
256                  1090                 1
261                  1090                 1
288                  1090                 1
294                  1090                 1
300                  1090                 1
4691                 1090                 1
4694                 1090                 1
4697                 1090                 1

所以我想做的是在给定 entityId 和 groupId 时获取 groupDepth 最高的行。示例结果:

input: entityId = 294, groupId = 1090
entityId             groupId              groupDepth
-------------------- -------------------- -----------
294                  1090                 1


input: entityId = 113, groupId = 1090
entityId             groupId              groupDepth
-------------------- -------------------- -----------
NULL                 1090                 0

我在想这样的事情:

SELECT * FROM [dbo].[EntityGroup] a 
WHERE EXISTS
(
    SELECT   groupId
    FROM     [dbo].[EntityGroup] b
    WHERE    
              (b.entityId is null or b.entityId = 294) AND
              b.groupId = a.groupId
    GROUP BY  b.groupId
    HAVING    a.groupDepth = max(b.groupDepth) and
              a.entityId = b.entityId 
)

任何帮助将不胜感激!

【问题讨论】:

    标签: sql sql-server group-by exists having


    【解决方案1】:
    SELECT entityID, groupId, groupDepth
    FROM EntityGroup  t
    WHERE groupDepth = (SELECT MAX(groupDepth) FROM EntityGroup  e WHERE COALESCE(e.entityID,-1) = COALESCE(t.entityId,-1) AND e.groupId = t.groupID)
    GROUP BY entityID, groupId, groupDepth
    

    如果我理解正确的话应该可以工作

    编辑

    SELECT entityID, groupId, groupDepth
    FROM EntityGroup    t
    WHERE groupDepth = (SELECT MAX(groupDepth) FROM @Temp  e WHERE e.entityID = t.entityId AND e.groupId = t.groupID)
    GROUP BY entityID, groupId, groupDepth
    UNION
    SELECT entityID, groupId, groupDepth
    FROM EntityGroup    t
    WHERE groupDepth = (SELECT MAX(groupDepth) FROM @Temp e WHERE e.groupId = t.groupID AND e.entityId IS NULL) AND t.entityId IS NULL
    GROUP BY entityID,groupId, groupDepth
    

    【讨论】:

    • 如果我指定 EntityGroup 表中不存在的 entityId,我将不会收到 NULL 行 - 请参考我的第二个示例
    【解决方案2】:

    这个怎么样?

    DECLARE @entityId INT = 294
    DECLARE @groupId INT = 1090
    
    SELECT TOP 1 entityId, groupid, Max(groupDepth) AS groupDepth
    FROM EntityGroup
    WHERE (entityId is null OR entityId = @entityId)
    AND groupId = @groupId
    GROUP BY entityId, groupId
    order by entityId desc
    

    编辑:将 SQL 更新为按 entityId 降序排序并取第一个将给出指定案例的正确答案

    【讨论】:

    • 这很好,因为我在我的结果集中得到两行 - 一个具有 NULL 值作为 entityId,另一个具有 294,而我应该只得到一行。
    • @MonkeyCoder - 对。我已经编辑了答案,现在只得到 1 行
    猜你喜欢
    • 2011-10-01
    • 2016-08-10
    • 1970-01-01
    • 1970-01-01
    • 2016-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多