Multiple SUM commands - mysql

I am trying to sum a column in my table, the problem being there are multiple sums that need to be done.
So for instance there may be 40 records with an ID of 1 and a point value of 20, And then it will change to a new person with an ID of 2 and a point value of 20. If that makes sense.
How I want to do the query, but it doesn't work is like this:
SELECT SUM(Value)
FROM Points WHERE RegNum IN('','','')
And then I would like it to show up just like a normal SUM command would, with the total summed up, but with a line for each ID. I have looked over other questions about SUM commands and just can't quite apply it to my situation.
Thank you for any help.

It seems like you need to use a GROUP BY in your case. Try
SELECT RegNum, SUM(Value) total
FROM Points
WHERE RegNum IN(1, 2, 3)
GROUP BY RegNum
Sample output:
| REGNUM | TOTAL |
------------------
| 1 | 17 |
| 2 | 9 |
| 3 | 1 |
Here is SQLFiddle demo

Try
SELECT RegNum, SUM(Value) as TotalRegNum
From Points
WHERE RegNum IN('1','2','3')
GROUP BY RegNum

Related

Need validation that interpretation for a Grouping Query is correct

I am running the following query and at first it appears to give the sub totals for customers and shows by date each customers payment amounts only if that total for all payments is greater than $90,000.
SELECT
Customername,
Date(paymentDate),
CONCAT('$', Round(SUM(amount),2)) AS 'High $ Paying Customers'
FROM Payments
JOIN Customers
On payments.customernumber = customers.customernumber
Group by customername, Date(paymentDate) WITH ROLLUP
having sum(amount)> 90000;
But upon looking at the records for Dragon Souveniers, Ltd. and Euro+ Shopping Channel is is actually showing the paydates that have amounts individually over $90000 as well as the subtotal for that customer as a rollup. For all other customers, their individual payment dates are not reported in the result set and only their sum is if it over $90000. For example Annna's Decorations as 4 payment records and none of them are over 90000 but her sum is reported as the value for the total payments in the query with the rollup. Is this the correct interpretation?
The HAVING clause work correct, It filters all records with a total no above 90000. It also does do this for totals.
When using GROUP BY .... WITH ROLLUP, you can detect the created ROLL UP lines by using the GROUPING() function.
You should add a condition in a way that the desired columns are not filtered.
Simple example:
select a, sum(a), grouping(a<3)
from (select 1 as a
union
select 2
union select 3) x
group by a<3 with rollup;
output:
+---+--------+---------------+
| a | sum(a) | grouping(a<3) |
+---+--------+---------------+
| 3 | 3 | 0 |
| 1 | 3 | 0 |
| 1 | 6 | 1 |
+---+--------+---------------+
this shows that the last line (with grouping(i<3) == 1) is a line containing totals for a<3.

Group by with sum doesn't return correct result

Say a table has this schema :
grp | number
1 | 10
1 | 10
1 | 10
2 | 30
2 | 30
3 | 20
Note that each unique grp has a unique number even if there are more than 1 grp. I'm looking to sum all numbers for each unique grp.
So I want to group my table by grp to have this :
grp | number
1 | 10
2 | 30
3 | 20
And then get the sum which is now 60, but without grouping it gets me 110 as it calculates the sum of everything without grouping. All in one query, with no sub-queries if possible.
I've tried doing the following :
SELECT sum(number) as f
FROM ...
WHERE ...
GROUP BY grp
But this doesn't work, it returns multiple results and not the single result of the sum. What am I doing wrong?
You can use subquery to select unique records & do the sum:
select sum(number)
from (select distinct grp, number
from table t
) t;
If you group by the group, then you'll get one result for each group. And it won't take into account the fact that you only want to use the value from each group once.
To get your desired result, taking one row from each group, you first need to make a subquery selecting DISTINCT group/number combinations from the table, and then SUM that.
SELECT
sum(`number`) as f
FROM
(SELECT DISTINCT `grp`, `number` FROM table1) g
This will output 60.
Demo: https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=8a3b346041731a4b4c85f4e151c10f70

Using Rollup in Mysql

I'm having trouble using rollup in mysql. I want a table that looks something like this:
Person | Count
John | 3
Sam | 2
Total | 5
The code I wrote:
SELECT person, count(*), IFNULL(count(*),"Total") AS Count FROM ProductsSold GROUP BY
person WITH ROLLUP;
But Instead total is staying null and all my counts say BLOB in workbench.
My guess is you want to rename the person NULL which holds the ROLLUP result to Total. That means you have do your IFNULL construct on the person column:
SELECT IFNULL(person,"Total") as person, count(*) AS Count
FROM ProductsSold
GROUP BY person WITH ROLLUP
makes:
John 3
Sam 2
Total 5

Mysql Agregate function to select maximum and then select minimum price within that group

I am trying to get the maximum value out of a aggregate function, and then also get the min value out of a Price column which comes back in results.
id | discount | price
1 | 60 | 656
2 | 60 | 454
3 | 60 | 222
4 | 30 | 335
5 | 30 | 333
6 | 10 | 232
So in above table, I would like to separate Minimum Price vs Highest Discount.
This is the result I should be seeing:
id | discount | price
3 | 60 | 222
5 | 30 | 333
6 | 10 | 232
As you can see, its taken discount=60 group and separated the lowest price - 222, and the same for all other discount groups.
Could someone give me the SQL for this please, something like this -
SELECT MAX(discount) AS Maxdisc
, MIN(price) as MinPrice
,
FROM mytable
GROUP
BY discount
However, this doesnt separate the minimum price for each group. I think i need to join this table to itself to achieve that. Also, the table contains milions of rows, so the sql needs to be fast. One flat table.
This question is asked and answered with tedious regularity in SO. If only the algorithm was better at spotting duplicates. Anyway...
SELECT x.*
FROM my_table x
JOIN
( SELECT discount,MIN(price) min_price FROM my_table GROUP BY discount) y
ON y.discount = x.discount
AND y.min_price = x.price;
In your query, you cannot group by discount and then maximize the discount value.
This should get you the result you are looking for..
SELECT Max(ID) AS ID, discount, MIN(price) as MinPrice, FROM mytable GROUP BY discount
If you do not need the id, yo would do:
select discount, min(price) as minprice
from table t
group by discount;
If you want other columns in the row, you can either join back to the original table or use the substring_index()/group_concat() trick:
select substring_index(group_concat(id order by price), ',', 1) as id,
discount, min(price)
from table t
group by discount;
This will not always work because the intermediate result for group_concat() can overflow if there are too many matches within a column. This is controlled by a system parameter, which could be made bigger if necessary.

Custom select if sum>x

I have this table :
TICKETID | PRICE | NUMBER
So for each ticketid, the player can pay a price for each number on the ticketid.
So if the player wants to pay 1$, 3$ and 4$ for numbers 22,23 and 24 for ticketid 25, then the table will look like this :
TICKETID | PRICE | NUMBER
25 | 1 | 22
25 | 3 | 23
25 | 4 | 24
I want to select a random ticket that has TOTAL PRICE >50, to make it receive a prize.
I also want that the randomization to be fair, and that when doing this draw, each ticket would have only 1 apparition rate. If I don't use DISTINCT or GROUPBY, then a ticketid with 10 numbers will have more chances to get drawn than a ticket with 2 numbers.
I tried this but it's not working:
SELECT DISTINCT(ticketid),SUM(price) FROM table
WHERE SUM(price)>50 GROUP BY ticketid
I get the error message
invalid usage of GROUP BY function
Can anybody help?
What you want is the "HAVING" clause which is applied to any possible candidate records AFTER the group by aggregations have been processed. The Having is applied THEN and either included (or not) in the final result set.
SELECT
ticketid,
SUM(price) TotalPrice
FROM
table
group by
TicketID
HAVING
sum(price) > 50