简体   繁体   中英

sum of column name using group by in SQL server?

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

AS
BEGIN

SET NOCOUNT ON;

 select PS.sno, PD.productname,sum(PS.quantity) as quantity,PS.modelno from   K_RT_PurchaseDet PS 
 inner join K_RT_ProductDetails PD on PD.sno=PS.product 
 where purchasedby=@purchasedby and PS.quantity!=0 and attrited='false'

 group by  PD.productname,PS.modelno,PS.company,PS.sno

END

by this iam getting out put as

sno    product   stock   modelno
1      Computer   2       Dell
2      Mobile     3       Nokia7100
3      Mobile     2       Nokia7100

but actually i want out put as

sno   product    stock    modelno 
1      Computer   2       Dell
2      Mobile     5       Nokia7100

I wrote like this but iam not getting please help me....

Just edit you group by clause in query. Make it to

group by PD.productname,PS.modelno

Query:

SELECT MIN(PS.sno) AS sno, 
       PD.productname,
       SUM(PS.quantity) as quantity,
       PS.modelno 
FROM K_RT_PurchaseDet PS 
 JOIN K_RT_ProductDetails PD on PD.sno=PS.product 
 WHERE purchasedby=@purchasedby 
  AND PS.quantity!=0 and attrited='false'
GROUP BY PD.productname,
         PS.modelno,
         PS.company

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM