如何根据条件得到列的总和

时间:2014-01-27 07:17:45

标签: sql sql-server-2008 stored-procedures

ALTER PROCEDURE [dbo].[K_RT_GetProdutstogrid]
@branch int

AS
BEGIN

    SET NOCOUNT ON;

   select PS.sno, PD.productname,sum(PS.quantity) as quantity,PS.description from 
   K_RT_PurchaseDet PS 
   inner join K_RT_ProductDetails PD on PD.sno=PS.product where purchasedby=@branch 
   and PS.company=@company and modelno=@modelno 
   group by PS.quantity,PS.sno, PD.productname,PS.description
END

当公司和modelno相同时如何计算数量

1 个答案:

答案 0 :(得分:1)

尝试此操作,无需在GROUP BY中包含数量

ALTER PROCEDURE [dbo].[K_RT_GetProdutstogrid]
@branch int

AS
BEGIN

    SET NOCOUNT ON;

   select PS.sno, PD.productname,sum(PS.quantity) as quantity,PS.description from 
   K_RT_PurchaseDet PS 
   inner join K_RT_ProductDetails PD on PD.sno=PS.product where purchasedby=@branch 
   and PS.company=@company and modelno=@modelno and PS.company=modelno
   group by PS.sno, PD.productname,PS.description
END