本文介绍了如何在MS SQL中加入/分组以相同的4个字符开头的2行或更多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 你好, 我正在尝试创建一个视图,并分组或加入两个或多个以相同的4个字符。 目前,我的代码是: select ( left (P.PartNum, 4 )),总和(P.OnhandQty) as PartNum2 from PartBin P group by PartNum 我正在尝试对它们进行分组/加入,这样如果有5个部分以AA11开头并且每个数量为2,那么输出将是: Part #Qty AA11 10 而不是: 零件编号数量 AA11T9 2 AA11B4 2 AA1143 2 AA1173 2 AA11A5 2 我尝试过: 选择 左(Q.PartNum, 4 ),sum(Q.OnhandQty) as PartNum3 来自 PartBin Q 其中 左(Q.PartNum, 4 )喜欢 左(Q.PartNum, 4 ) group by PartNum 解决方案 这应该有效: SELECT LEFT(Q.PartNum,4 )AS PartNumLeft, SUM(Q.OnhandQty)AS PartNumQty FROM PartBin AS Q GROUP BY LEFT(Q.PartNum,4) Hi there,I'm trying to create a view and to group or join two or more rows that start with the same 4 characters.Currently, the code that I have is: select (left(P.PartNum, 4)), sum(P.OnhandQty) as PartNum2 from PartBin P group by PartNumI'm trying to group/join them so that if there's 5 parts that start with AA11 and have a quantity of 2 each then the output would be: Part # QtyAA11 10Instead of:Part # QtyAA11T9 2AA11B4 2AA1143 2AA1173 2AA11A5 2What I have tried:select left(Q.PartNum, 4), sum(Q.OnhandQty) as PartNum3from PartBin Qwhere left(Q.PartNum, 4) like left(Q.PartNum, 4)group by PartNum 解决方案 这篇关于如何在MS SQL中加入/分组以相同的4个字符开头的2行或更多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 11-01 00:14