Home > database >  When I use SUM() it shows me only the higher result
When I use SUM() it shows me only the higher result

Time:06-25

I'm trying to SUM grouped results from DB and it only shows me the higher value in the column.

SELECT *, SUM(`pr_n`) FROM `request-list`
                        GROUP BY `request_id` ORDER BY `timestamp` DESC

I have 2 records with same value in "request_id". I'm trying to sum the values in "pr_n" column which are "5" and "1" and when I display the result it shows 5 only.

Here are the columns in the DB https://prnt.sc/upBR9jwfOTPU and thats what it display --> https://prnt.sc/_n_7r5TXkWXj

How can i fix that?

CodePudding user response:

You're doing something odd (and I'm surprised MySQL lets you do this): pr_n is in *, and at the same time, you're taking its SUM. So you're saying you want to sum over single rows, which is useless.

Instead of *, explicitly specify columns other than pr_n. I hate SQL for being so primitive, but that's the way it is.

CodePudding user response:

Try this query and it will work fine.

SELECT `request_id`, SUM(`pr_n`) 
FROM `request-list`
GROUP BY `request_id`;
  • Related