Cumulative average mysql php - php

I have a table name meal
id name carbs protein fat
1 one 10 30 18
2 one 17 4 2
3 one 27 6 7
Now I want to get Cumulative Average of carbs
my query is
SELECT (100*(sum(m.carbs)/(sum(m.carbs)+sum(m.fat)+sum(m.protein))))AS Percantage_carbs FROM `meal` AS m
My query gives result 52.2580645
But I t will be
52.8848076

You Query will result in 44.628099173554 which is absolutely correct.
Total: 10+30+18+17+4+2+27+6+7 = 121
Carbs Only: 10+17+27 = 54
(54 / 121) * 100 = 44,63%
SQL Fiddle: http://sqlfiddle.com/#!2/3d6e5/3

The result of the data given and the query you're using is: 44,628099174
I guess your query should be something like this (your missing the group by):
SELECT (
100 *
(sum(m.carbs)
/
(sum(m.carbs)+ sum(m.fat)+ sum(m.protein)))
) AS Percantage_carbs
FROM `meal` AS m
GROUP BY name

Related

Mysql query to get closest record

I have a table
id startmileage endmileage price
1 1 2 30
2 2 3 50
3 3 4 70
4 4 5 100
5 5 25 4.5
6 25 35 7
In this table i have stored data to get price between mileages (from start to end mile)
I am able to get the price between 1 difference value like 1 to 2, 2 to 3. But for the values between 5 to 25 and 25 to 35 i have tried a query which will work for both the closest and exact value like this
SELECT *
FROM table
ORDER BY ABS( startmileage - myValue )
LIMIT 1
But this query only works for one input (startmileage), As i need to use both start and end mileage to get the best closest record.
Can someone please tell me the best query for this ?
ORDER BY
(
greatest(startmileage - $myvalue ,0)
+
greatest($myvalue - endmileage ,0)
) ASC , startmileage DESC
if the $myvalue is within boundaries, this will be equivalent to zero. If it is before startmileage, first part will rose while second is zero. If $myvalue is after endmileage, first part will be zero and second will rose up.
function greatest( ??? , 0 ) will return ??? only when it is greater then zero, returning zero otherwise.
edit: A bit optimized query (works the same way):
ORDER BY
greatest(startmileage - $myvalue , $myvalue - endmileage ) ASC
, startmileage DESC

Average of average with a condition

My query is:
SELECT avg(result)
FROM `survey_result_full`
WHERE `comp_id`='$corow[id]'
AND rater_type='riwter'
AND survey_id='$survey'
AND rater_id in (
SELECT id
FROM raters
WHERE participate='$id'
AND `type`='$rt[id]'
)
Here I will get all the average of result:
id survey_id comp_id rater_id rater_type beh_id result
---- --------- ------- -------- ---------- ------ ------
6198 79 204 180 riwter 573 4
6576 79 204 181 riwter 573 4
6577 79 204 181 riwter 574 4
But I need to find the average of the averages for rows with identical beh_id.
If two rows have the same beh_id then I need to find the average of the result column of these two rows first then find average of all items.
If what you want is to get a result with [beh_id , average], then your query should be:
SELECT beh_id, avg(result)
FROM `survey_result_full`
WHERE `comp_id`='$corow[id]'
AND rater_type='riwter'
AND survey_id='$survey'
AND rater_id in (
SELECT id
FROM raters
WHERE participate='$id'
AND `type`='$rt[id]'
)
GROUP BY beh_id
maybe sth like
SELECT AVG(avg_results)
FROM (
SELECT srf.beh_id, AVG(srf.result) as avg_results
FROM `survey_result_full` srf, `raters` r
WHERE srf.`comp_id`= '$corow[id]'
AND srf.rater_type = 'riwter'
AND srf.survey_id = '$survey'
AND srf.rater_id = r.id
AND r.participate ='$id'
AND r.`type` = '$rt[id]'
GROUP BY srf.beh_id) AS avgs
since what you want seems to be an average of averages.
(I've also refactored a bit the query since what you want is a join on the rater table)

How can I select all the values of a database and choose one of them by a percent

What I want to do is to select a value of the database,
Lets say:
id ---- giftid ---- userid
1 1 481
2 1 422
3 7 123
4 9 542
5 1 122
6 1 455
For example, there are 4 users that want to have the same giftid:
1, 2, 5, 6
It means that each one will have 25% to be chosen.
How can I make the "percent selection"?
Assuming every userid can only claim a giftid once, you can use the ORDER BY RAND() in MySQL. This will firstly select all the rows from table table where the giftid is 1 and then the results are ordered randomly. The LIMIT 1 ensures that only the first record is returned
SELECT * FROM table
WHERE giftid = `1`
ORDER BY RAND()
LIMIT 1
Are you looking this?
SELECT giftid, 1.0 / COUNT(*) percentSelection
FROM tableName
GROUP BY giftid

summing two columns total in mysql

I want a resultset for this table:
ID Number_of_posts Number_of_user
1 100 21
2 23 34
as
ID Number_of_posts Number_of_user Number_of_posts_AND_Number_of_user
1 100 21 178
2 23 34 178
-----------------------------------------------
123 55
Is it possible to get the sum of two colums as another column/ as output in mysql?
To get cross-tab totals (horizontal and vertical):
select id,
number_of_posts as p,
number_of_users as u,
number_of_posts+number_of_users as p_and_u
from tbl
union all
select 99999 as id,
sum(number_of_posts) as p,
sum(number_of_users) as u,
sum(number_of_posts+number_of_users) as p_and_u
from tbl
order by 1
This will give you:
id p u p_and_u
----- --- --- -------
1 100 21 121
2 23 34 57
99999 123 55 178
You're complicating your query needlessly and using more memory that you have to. Pull the records in one query, then make another query to get the aggregates.
I know it doesn't answer your question, but it's what you should be doing instead. =)
SELECT id, number_of_posts, number_of_user,
(
SELECT SUM(number_of_posts + number_of_user)
FROM mytable
)
FROM mytable
SELECT SUM(Number_of_posts), SUM(Number_of_user) FROM table;
SELECT *,
(SELECT SUM(Number_of_posts) + SUM(Number_of_user) FROM TABLE) AS total
FROM table;
(Edit: Didn't originally notice it was the total total in the last column.)
Does MySQL support ROLLUP?
SELECT id,
SUM(Number_of_posts) Number_of_posts,
SUM(Number_of_user) Number_of_user,
SUM(Number_of_posts) + SUM(Number_of_user) Number_of_posts_AND_Number_of_user
FROM table
GROUP BY ROLLUP(id)
Edit: based on a quick search, in MySQL the last line might be GROUP BY id WITH ROLLUP.

PHP - SQL: fetching results in round robin fashion

I have a table, which consists of 3 fields:
id
name
status
Every time I get the results, it should give me 5 names whose status = 1.
Suppose the db contains following:
id name status
1 A 1
2 B 1
3 C 0
4 D 1
5 E 0
6 F 0
7 H 1
8 I 1
9 J 1
10 K 1
11 L 1
12 M 0
1st time, fetch should return: A,B,D,H,I (5 records)
2nd time, fetch should return: J,K,L,A,B (5 records)
UPDATE: I don't want typical pagenation. Consider I have 12 available names from A1 to A12. The first fetch should return A1-A5, second fetch A6-A10 and third fetch A11, A12, A1, A2, A3. So when I reach the end, I need to get records starting from the first to fill the 5 slots.
i am doing it in php with mysql
This looks like some sort of job allocation script?
You need 2 things:
the highest ID returned last time the script was run (lastID)
a number larger than the maximum ID in the table (bigNum)
Then you can write your query as
SELECT
id, name
FROM
table
WHERE
status=1
ORDER BY
(bignum + id) MOD (bigNum + lastID + 1)
LIMIT 5
Shazaam!
Keep track of the ids of the records returned, and for the following queries do:
select top 5 *
from (
select top 5 *
from MyTable
where status = 1
and id not in (1,2,4,7,8)
order by name
union
select top 5 *
from MyTable
where status = 1
order by name
) a
$q = mysql_query("SELECT name FROM table WHERE status = 1 LIMIT 5);
while ($row = mysql_fetch_row($q))
{
.... //first 5
}
$q = mysql_query("SELECT name FROM table WHERE status = 1 LIMIT 5,5);
while ($row = mysql_fetch_row($q))
{
.... //second 5
}
this uses the offset functionality of mysql- think of it as pagination for your results.

Categories