Sql 服务器参考请求 /group by 随着最大的存在。/
你好,
我有一个请求的巨大问题,这些是我在我的表中的数据:
所以我想这样做,这是为了获得最高的字符串 groupDepth, 指定时 entityId 和 groupId. 结果示例:
我想到了这样的事情:
任何帮助都会非常感激!
我有一个请求的巨大问题,这些是我在我的表中的数据:
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
所以我想这样做,这是为了获得最高的字符串 groupDepth, 指定时 entityId 和 groupId. 结果示例:
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
/
任何帮助都会非常感激!
没有找到相关结果
已邀请:
2 个回复
奔跑吧少年
赞同来自:
如果我正确理解你,应该工作
EDIT
董宝中
赞同来自:
EDIT
: 更新 SQL 订购 entityId 下降并采取第一个将为这些案例提供正确的答案。