MySQL Sum of Values In a Single Column For Every 50 Rows - mysql

I have a very simple query like this for my event_prizes table:
SELECT id, prize FROM event_prizes WHERE event_prizes.event_id = x;
Instead of getting the individual price amounts, I need to show the total amount of the given prize for every 50 rows in this query. How can I use the SUM function to calculate the total value of every 50 rows?

assuming you have a colum with the row number (this id db depending )
You could try grouping by the floor(your_row_num/50)
SELECT floor(your_row_num/50), sum(prize )
FROM event_prizes WHERE event_prizes.event_id = x
GROUP BY floor(your_row_num/50);
if you have a mysql version 8 you could use ROW_NUMBER otherwise
use a var and increment

Related

get distinct user rows from table while counting a different row

I am trying to count rows from a SQL Table named "reports", sorted by "working" being 0 / 1 / 2.
Currently I have this SQL query which works okay to give me three rows, each with a counter of how many there are that had "working" as either 0 / 1 / 2.
SELECT `working`, COUNT(`working`) AS `total` FROM `reports`
WHERE `appid` = 379720
GROUP BY `working`
ORDER BY `report_id` DESC LIMIT 30
So it currently (correctly as per the SQL) gives me something like:
Working
Total
0
12
1
34
2
18
What I want to do though, is have only one row per user counted, which I can't quite wrap my head around. I can't use a distinct select on an "author_id" field as that ends up included and I can't group by it since I need it grouped by the working int.
To be clear: I want the same results display, but only count one per unique "author_id" from each row.
Any pointers?
You seem to want count(distinct):
SELECT `working`, COUNT(DISTINCT author_id) AS `total`
FROM `reports`
WHERE `appid` = 379720
GROUP BY `working`
ORDER BY `report_id` DESC

Selecting multiple same value rows out of the same column

I have a table, where one of the columns is named mid. It has a lot of values, some of them repeat themselves. Theres also a column named chashrate. It has a different value for each mid row. Theres also a column named pid, which shows the id of each row.
I've tried pulling out specific value rows with HAVING, but I can only do one value at a time or multiple values that dont match each other
$miner = $pdo->query("SELECT * FROM data WHERE pid='6'")->fetchall();
What I need to do is collect all the same MID column value rows, with the id pid=6 so for example all of the mid = 8; pid=6, collect their chashrate and sum it up. So for example I would get mid(8)=17394, mid(6)=28424 etc.
Here's a photo of the table: https://i.imgur.com/9xX6sYm.png
The same colored rows need to be selected and their chashrate values summed up.
Try using SUM to sum the cashrate values and GROUP BY to group them by mid.
SELECT mid
, SUM(`cashrate`) AS total
FROM `data`
WHERE pid = 6
GROUP BY mid;
Check it here.
For the given data on the image, this query will output the following result:
mid | total
6 | 981
8 | 374
You seem to want aggregation:
select mid, sum(chashrate) as sum_chashrate
from data
where pid = 6
group by pid, mid;
This will return multiple rows, one for each mid value.
You can do this for multiple pids -- or even all of them, by removing or changing the where clause.

GroupBy and get percentage for each

I have my SQL table like this:
**CLIENTS:**
id
country
I want to echo a table with all countries I have with percentage fo each.
For example, if I have 2 Canadians and 1 French in my table, I want:
1 - Canada - 66%
2 - France - 33%
What I tried:
SELECT country FROM `mytable` GROUP BY `Country`;
It works, but how to have the percentage for each ?
Thanks.
You can use subquery:
SELECT
country,
COUNT(id) * 100 / (SELECT COUNT(id) FROM `mytable`) AS `something`
FROM
`mytable`
GROUP BY
`Country`;
You don't specify a falvor of SQL, but years ago microsoft posted their suggested solution:
select au_id
,(convert(numeric(5,2),count(title_id))
/(Select convert(numeric(5,2),count(title_id)) from titleauthor)) * 100
AS "Percentage Of Total Titles"
from titleauthor group by au_id
To calculate the percentage of total records contained within a group
is a simple result that you can compute. Divide the number of records
aggregated in the group by the total number of records in the table,
and then multiply the result by 100. This is exactly what the
preceding query does. These points explain the query in greater
detail:
The inner nested query returns the total number of records in the
TitleAuthor table: [ Select convert(numeric(5,2),count(title_id)) from
titleauthor ]
The value returned by the COUNT(title_id) in the outer
GROUP BY query returns the number of titles written by a specific
author.
The value returned in step 2 is divided by the value returned
in step 1, and the result is multiplied by 100 to compute and display
the percentage of the total number of titles written by each author.
The nested SELECT is executed once for each row returned by the outer
GROUP BY query
The CONVERT function is used to cast the values
returned by the COUNT aggregate function to the numeric data type with
a precision of 5 and a scale of 3 to achieve the required level of
precision.

How to use SQL MAX(SUM()) function

Hi this is driving me crazy consultation, such as selecting the maximum of this consultation, I made a temporary table to which the maximum then sack him, but quiciera know if the same query can be removed without temporary tables.
I have 2 tables N AND ACCRUED PAYROLL D
Table I occupy the payroll payroll 201314-201320
and table I need the EMPLOYEEIDNO accrued; cod_tiponomina; cod_nomina and accrued employee wages are earned in 14 days;
Good help is that I need to get the maximum of the salary calculation with the sum ();
thanks for the help.
SELECT MAX((SUM(d.devengado) / (COUNT(d.devengado)*14))*30) salario_30dias
FROM devengados d
JOIN nominas n
ON n.cod_nomina = d.cod_nomina
WHERE d.cod_empleado = 564
AND d.cod_tiponomina = 1
AND d.cod_nomina BETWEEN 201314 AND 201320
AND d.devengado > 0
GROUP
BY YEAR(n.fecha_cierre)
, MONTH(n.fecha_cierre);
'Max' and 'Sum' are always executed according to the Group By Clause.
You can nest 2 Selects to get the Maximum of Different Sums:
SELECT MAX(sum) FROM
(SELECT SUM(column) AS sum FROM table GROUP BY crit1)
GROUP BY sum
But a better Way would be to sort the sums, and pick the first one to achieve the same as a sourrounding MIN/MAX (It would not require the nesting of selects):
SELECT SUM(column) AS sum FROM table GROUP BY crit1 ORDER BY sum DESC LIMIT 0,1
(for MIN you would need to sort ASC)

Mysql group by query optimization

I have query like this in mysql
select count(*)
from (
select count(idCustomer)
from customer_details
where ...
group by idCustomer having max(trx_staus) = -1
) as temp
So basically finding customer count that fulfill certain where condition (one or two) with max transaction state = -1 (other can be 2,3,4). but this query takes about 30 min on my local machine and 13 sec on high configuration server (about 20 gb ram and 8 core processor). i have 13 lac rows in table. i know group by and having max function are too costly. what can i do to optimize this query. any suggestion?
The inner query has to inspect all rows to determine the aggregate maximum; if you want to optimize this, add a calculated field that contains the maximum to your customer table and select on that.
The trick is then to keep that field up-to-date :)