Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于组记录的sql top 1_Sql_Sql Server - Fatal编程技术网

基于组记录的sql top 1

基于组记录的sql top 1,sql,sql-server,Sql,Sql Server,表ProductMst组合是唯一的-CodeNum、MAINTATID、SUBATID id CodeNum MainCatid SubCatid Desc Qty 1 001 1 1 prod1 5 2 001 1 2 prod2 10 3 001 2 3 p

表ProductMst组合是唯一的-CodeNum、MAINTATID、SUBATID

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
子表SUBATMST

期望结果

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
基本上,我想分组子代码,如果相同的话,并根据子代码求和数量。并且,根据MAXQty将前1个记录数据作为所有列

小结:请检查合并的前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
你的问题是:

我想分组子代码,如果相同的话

如果是一样的话?我假设这意味着如果codenum是相同的

不管确切的字段是什么,逻辑都是一样的。您可以使用窗口函数聚合数据并确定要选择的行:

select ps.*
from (select p.*, sc.subcode,
             sum(qty) over (partition by codenum, subcode) as qty,
             row_number() over (partition by codenum, subcode order by qty desc) as seqnum
      from ProductMst p join
           SubCatMst sc
           on p.subcatid = sc.id
     ) ps
where seqnum = 1;

显示您当前的查询尝试。@jarlh编辑了我的代码。我也尝试过类似于您的解决方案,只是在查找sumqty。
select ps.*
from (select p.*, sc.subcode,
             sum(qty) over (partition by codenum, subcode) as qty,
             row_number() over (partition by codenum, subcode order by qty desc) as seqnum
      from ProductMst p join
           SubCatMst sc
           on p.subcatid = sc.id
     ) ps
where seqnum = 1;