MySQL fetch results, sum it up - mysql

i'm trying to sum up values from one record (A) then add it to the sum values from another record (B), am wondering if this is possible?
ID ValueA ValueB ValueC
1 5 5 5
2 1 2 3
3 6 3 3
So what i'm trying to do here is to take ValueA, ValueB and ValueC of each record, adds it up so i can make an average.
So for
ID 1, i'll have 15 divide by 3 = 5
ID 2 i'll have 6 divide by 3 = 2
ID 3 i'll have 12 divide by 3 = 4
then i will have to add all 3 of these up
i'll get 11
and divide it by 3 and get an average of 3.67.
My Query
$result = mysql_query('SELECT * FROM teams WHERE UPPER(team)=UPPER("'.$team.'")');
while($row = mysql_fetch_array($result))
{
$ValueA = $row['ValueA'];
$ValueB = $row['ValueB'];
$ValueC = $row['ValueC'];
$All = $ValueA + $ValueB + $ValueC;
}
I know how to get the sum of 1 record, but not sure how can i do it with all 3 records. any help?
Edit : Sorry i forgot to add that i'll have to do average on each record first.

SELECT AVG(valueA + valueB + valueC)
FROM teams
should do the trick. Since you're selecting all records from the table, there's on grouping required.
Note that by default MySQL uses case-insensitive collations on tables, so your UPPER(team) business might not be required - removing the function calls would allow indexes (if any) to be applied to that particular match.

You can sum SQL functions together:
SELECT
(sum(ValueA) + sum(ValueB) + sum(ValueC)) / 3 as average
FROM
<your table>
WHERE
<your conditions>

SELECT SUM(ValueA + ValueB + ValueC) / (SELECT COUNT(*) FROM Teams)
FROM Teams
That will you the average you are looking for.

Related

mysql select max percent id and all row. or select latest insert

i have this table called "tblpercentage"
PercentID Dividend Rebates Interest
1 40 10 2
2 60 20 5
this is my query
SELECT MAX(PercentID)As PercentID,Interest FROM tblpercentage
but the problem is. it can only select PercentID = 2 and interest = 2 instead if 5. but i want is select all row with the max id. can anyone help me.
for get the row related to the max id you should use a subquery
select PercentID,Interest from tblpercentage
where PercentID = (SELECT MAX(PercentID) from tblpercentage)

how to move up row to find the last or first occurrence of the same number in mysql

I have a table name current_record in mysql database.
id num
1 0
2 1
3 1
4 1
5 0
my question is : how find the 1st id of num = 1 from last means down to up or 3 times up
the output should return like this
id = 2 num = 1
please write a sql query.
You can use MIN() combined with GROUP BY to achieve this. Something like this:
SELECT MIN(id) AS id, num FROM current_record GROUP BY num
Optionally you can add a WHERE clause if you specifically want just one value instead of one for each num:
WHERE num == 1

Mysql recursive substracting and multiplying grouped values

Couldn't really explain my problem with words, but with an example I can show it clearly:
I have a table like this:
id num val flag
0 3 10 1
1 5 12 2
2 7 12 1
3 11 15 2
And I want to go through all the rows, and calculate the increase of the "num", and multiply that difference with the "val" value. And when I calculated all of these, I want to add these results together, but grouped based on the "flag" values.
This is the mathematical equation, that I want to run on the table:
Result_1 = (3-0)*10 + (7-3)*12
Result_2 = (5-0)*12 + (11-5)*15
78 = Result_1
150 = Result_2
Thank you.
Interesting question. Unfortunately MYSQL doesn't support recursive queries, so you'll need to be a little creative here. Something like this could work:
select flag,
sum(calc)
from (
select flag,
(num-if(#prevflag=flag,#prevnum,0))*val calc,
#prevnum:=num prevnum,
#prevflag:=flag prevflag
from yourtable
join (select #prevnum := 0, #prevflag := 0) t
order by flag
) t
group by flag
SQL Fiddle Demo

mysql select while skipping some number of rows

Is there a mysql syntax that can hop some rows?
For example
id value
1 a
2 b
3 c
4 d
5 e
6 f
7 g
8 h
9 i
SELECT * FROM table HOP BY 2
so the result will be
id value
3 c
6 f
9 i
or
id value
1 a
4 d
7 g
Take note: We don't know the actual ID of a row so we can't use a WHERE clause like this
WHERE ID is a multiple of 3 or etc.
I didn't realize you could do math in sql queries. Learned something new. Cool. Here's code that would select 1, 4, and 7.
$stmt = mysqli_prepare($connection, "SELECT * FROM users WHERE (ID+2)%3 = 0 AND ID>1");
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
while($row = mysqli_fetch_assoc($result)){
echo $row['username'];
}
I don't see why it wouldn't work if id has gaps, as the man with ?mandarin? symbols for a name said.
Modulus(%), if you don't know, gives the number of decimals given by a division problem. So 3/3=1, with no decimals, so 3%3=0, whereas 4/3=1.333333..., so 4/3 equals infinity(not really in programming, but close enough).
SELECT * FROM hoptable WHERE ID%3 =0 AND ID>1
SET #row_idx := 0;
SELECT *, (#row_idx := #row_idx + 1) AS idx
FROM table
HAVING idx % 3 = 0;
You probably want to include an ORDER BY clause so the row index can actually be meaningful though. You can't rely on the result being ordered by id without specifying it.

SQL statement with formula

I am trying to write an SQL statement that will calculate the total value of a purchase order.
Example time!
SELECT PO_Number, LineItem, Quantity, Cost
FROM POs
Say on PO_Number=484 there are 2 units of LineItem-1 at $2 each. There are also 3 units of LineItem-2 at $5 each. Is there a way to output $19? (2 + 2 + 3 + 3 + 3 = 19)
SELECT SUM(Quantity*Cost) WHERE PO_Number = 484;
UPDATE
If you want to show the totals for multiple purchase orders, you need to "group" your results by purchase order:
SELECT SUM(Quantity*Cost)
WHERE PO_Number IN (484,485,486) -- if you want specified ones only, omit for all
GROUP BY PO_Number;
Or...
SELECT PO_Number, SUM(Quantity*Cost)
GROUP BY PO_Number;