2
表ProductMst
(组合是唯一的 - CodeNum,MainCatid,SubCatid)SQL顶部1记录
id CodeNum MainCatid SubCatid Desc Qty
1 001 1 1 prod1 5
2 001 1 2 prod2 10
3 001 2 3 prod3 1
4 002 2 3 prod4 2
5 003 2 3 prod5 3
6 004 1 1 prod6 4
子表SubCatMst
id name subcode
1 scat1 00
2 scat2 00
3 scat3 02
4 scat4 03
所需的结果
id CodeNum MainCatid SubCatid Desc Qty subcode
2 001 1 2 prod2 15 00
3 001 2 3 prod3 1 02
4 002 2 3 prod4 2 02
5 003 2 3 prod5 3 02
6 004 1 1 prod6 4 00
基本上,我想分组subcode
如果相同,并且sum (Qty)
基于subcode
。并且,将TOP 1
记录数据作为基于MAX(Qty)
的所有列。
摘要:请检查结合的第2条记录。
查询尝试:
select * from (
select A.*, B.subcode,
ROW_NUMBER() OVER(PARTITION BY A.CodeNum, A.MainCatid, B.subcode ORDER BY A.Qty desc) as row
from ProductMst A
inner join SubCatMst B on A.SubCatid=B.id
) as A where row<=1
向我们显示您当前的查询尝试。 – jarlh
@jarlh编辑我的代码。 –