Ismail Gunes
Ismail Gunes

Reputation: 558

Why adding SUM changes the number of retrieved rows?

I have this sql which gives me 6 rows without SUM clause (sur can give more if condition is true)

SELECT id, prodname, prodid, st_date, montant, tvaval, quantite, status, factureno
FROM StockData WHERE " + VenteWhere + " ORDER BY " + Order_by + " " + SortDir + "

PS: VenteWhere, Order_by abd SortDir are the variables.

BUt when I add SUM(quantite) I get only one row. Is there a way to have 6 rows data and the sum of 6 rows or I have to do another query for getting it

Upvotes: 0

Views: 40

Answers (1)

luckylwk
luckylwk

Reputation: 225

I think you can use

 SELECT id, prodname, prodid, st_date, montant, tvaval, SUM(quantite), status, factureno
 FROM StockData 
 WHERE " + VenteWhere + " 
 ORDER BY " + Order_by + " " + SortDir + "
 GROUP BY xxxxx

Where xxxxx represents the variable you want to group by per row.

Summing will remove the rows its sums over and only display the sum itself. So you have to group by some variable to make it split the results over that variables 'occurence'

Upvotes: 2

Related Questions