1

こんにちは、

1 つのクエリで大きな問題が発生しています。これは、テーブルにあるデータです。

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 
)

どんな助けでも大歓迎です!

4

2 に答える 2

3
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
于 2011-05-06T07:53:05.707 に答える
0

これはどう?

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

EDIT : entityId の降順で並べ替えるように SQL を更新し、最初のものを取ると、指定されたケースに対して正しい答えが得られます

于 2011-05-06T07:54:38.413 に答える