HI(通过与具有MAX组),SQL服务器 - 查询帮助
我在与一个查询一个巨大的问题,这是我在我的表中的数据:
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
)
任何帮助将不胜感激!
如果我指定不中EntityGroup表中存在ENTITYID,我将不会收到NULL行 - 请参阅我的第二个示例 – MonkeyCoder 2011-05-06 08:08:25
查看我所做的更新是否有帮助 – 2011-05-06 08:14:35