Average of average with a condition - php

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)

Related

eloquent query for getting difference of two columns from different table

I've to show the remaining quantity of books using eloquent query.
I can get total number of purchased books from this table
ordered_books
BOOKCODE[varchar(10)] QUNATITY[varchar(6)]
111 25
423 15
201 10
111 10
423 10
201 5
158 12
At first I've to sum the total books for each of the book code. Then from the another table I've to calculate the difference.
books_out
DISTRIBUTOR[varchar(50)] BOOKCODE[varchar(10)] QUNATITY[varchar(6)]
25 158 2
35 201 5
45 158 5
55 111 10
35 111 5
15 423 1
25 423 10
Again, from this table I've to calculate the total number of books taken by distributors then I can get the actual number of books present. How shall I write eloquent query for this scenario?
Try to use Raw query like this
==================================
$data=DB::select(DB::Raw("select t1.BOOKCODE,(total-selled) as available from (select BOOKCODE, SUM(QUNATITY) as total FROM `ordered_books` group by `BOOKCODE`) as t1,(SELECT BOOKCODE,SUM(QUNATITY) as selled FROM `books_out` group by `BOOKCODE`) as t2 where t1.BOOKCODE=t2.`BOOKCODE`"));
print_r($data);
//To get total books for each of the book code
SELECT BOOKCODE,SUM(QUNATITY) FROM `ordered_books` group by `BOOKCODE`
//To get total books_out for each of the book code
SELECT BOOKCODE,SUM(QUNATITY) FROM `books_out` group by `BOOKCODE`
Regarding your scenario, I think you require this query, please try it and comment whether it was helpful or not.
$countBooks = DB::select(DB::Raw(" SELECT A.TOTAL, IFNULL(B.SOLD,0) AS SOLD, (A.TOTAL - IFNULL(B.SOLD,0)) AS REMAINING FROM
(
select t1.BookID, SUM(t1.QUNATITY) AS TOTAL from ordered_books t1
GROUP BY t1.BookID) A
LEFT JOIN
(
SELECT BookCode, IFNULL(SUM(Quantity),0) AS SOLD FROM books_outs GROUP BY BookCode) B
ON A.BookID = B.BookCode
"));
dump($countBooks);
Also, better to display the name of books rather than code. It will be more user friendly. Thank You!!!

Cumulative average mysql 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

Query and table - MAX and Join

Still very new to all of this so bear with me.
Have 3 tables
table 1: member
Mem_index, Mem_name
1 joe
2 Mark
Table 2: Course
Course_index, Course_Name
1 Math
2 Reading
Table 3 : data
Data index,Member,Course,Score
1 1 1 85
2 1 2 75
3 2 1 95
4 1 2 65
SO what I would like to do is create a table:
Do a query and gather all of the courses, find the max score for each course and attribute the member name to it.
Table result should look like:
Course, Max score,name
Math 95 Mark
Reading 75 Mark
I can do the query individually but unsure of how to loop it and then propogate the data into the table.
How about this query for SQL?
SELECT c.course_name, MAX( d.score ), m.mem_name
FROM members m
JOIN data d on m.mem_id = d.member
JOIN course c on c.course_id = d.course
GROUP BY d.course
ORDER BY d.score, m.mem_name, c.course_name
Not sure if the field names match up but you get the idea - tested this in sql with some dummy data.
Data
Index Member Course Score
1 1 1 60
1 1 1 85
Course
course_id course_name
1 Math
2 English
3 Science
Members
mem_id mem_name
1 Mark
2 James
You will get the following
Course Name Score Member
Math 85 Mark
Try this query :
SELECT c.course_Name , MAX(d.score),m.mem_name
FROM data d
JOIN course c ON d.course=c.course_index
JOIN members m ON m.mem_index = d.member
GROUP BY d.course
ORDER by MAX(d.score) DESC

PHP MySQL: Take highest values from multiple categories and write an array?

I have a table of student scores. Students can take the test multiple times, we only care about the highest score.
So the table looks something like this:
UserID CatID Score
20 5 60
20 5 85
20 5 80
20 6 90
20 7 80
20 7 75
20 7 90
22 5 75
22 5 85
Want to get back one row based on a UserID variable that looks like this:
UserID Cat5 Cat6 Cat7
20 85 90 90
Here's what I'm using so far as a query:
SELECT score AS Score
, catid
FROM `quiz_result`
WHERE userid=65 and catid=5
ORDER BY score DESC
LIMIT 0, 1
Not sure how to get the other categories in one row...thoughts?
Thanks!
Try this piece:
SELECT catID, MAX( score ) FROM `quiz_result`
GROUP BY catID;
The output isn't exactly same as in your sample, but it pretty much gives what you need.
EDIT
Output will be like:
catID MAX(score)
Cat5 85
Cat6 90
Cat7 90
To get other columns, just include their names in the SELECT query.
Try this :
SELECT CONCAT_WS(',', catID) AS catID, CONCAT_WS(',', MAX(score)) FROM `quiz_result` GROUP BY catID;

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.

Categories