AVG join result in other table - mysql

I've got a query with avg results. In another table I have the results of that
avg result in text.
Right now this is my query:
select round(avg(breed_ratings.rating)) as result, breed_ratings.score_name, count(*) as total, breeds.name_en
from breed_ratings
inner join breeds on breeds.id = breed_ratings.breed_id
where breeds.id = 188
group by score_name, breeds.name_en
The rating_result table looks like this:
id
rating
result_text
How can I get the result_text in this query?
Please help me out.
--EDIT
I need to get the result from the image below in text.
So I have another table where this is stored I need to get the result_nl where it matches the rating:
Desired result (if rating is 5):
result: 5
score_name: ADULT_FRIENDLY
total: 117
name_en: American Staffordshire Terrier
result_nl: I am extremely dominant

I think you want to join the average to the first rating in rating_results where the average is bigger than the listed rating there. If so:
select br.*, rr.result_text
from (select round(avg(br.rating)) as result, br.score_name, count(*) as total, b.name_en
from breed_ratings br join
breeds b
on b.id = br.breed_id
where b.id = 188
group by br.score_name, b.name_en
) br left join
(select rr.*, lead(rr.rating) over (order by rr.rating) as next_rating
from rating_result rr
) rr
on br.result >= rr.rating and
(br.result < rr.next_rating or rr.next_rating is null)

Related

MYSQL Select max value in multi column in a date

Suppose I have table called "bowlmark" and the summary of table as below link:
(this table is recording the bowling marks for each bowler)
Here is the sample table summary:
bowler_id---record_date---round1mark---round2mark
101---2018-06-02---100---164
102---2018-06-02---102---120
101---2018-06-03---150---124
103---2018-06-03---200---122
I want to output the highest marks in each record day and show the bowler id
For the above example, I want the output to be:
date:2018-06-02, bowler_id:101, highestmark: 164
date:2018-06-03, bowler_id:103, highestmark: 200 .
How to write this SQL query? Thanks.
It's a little ugly, but I was able to do it this way:
SELECT b.bowler_id, a.* FROM (
SELECT record_date, MAX(GREATEST(round1mark, round2mark)) AS greatest
FROM bowlers
GROUP BY record_date
) a
INNER JOIN bowlers b ON
a.record_date = b.record_date AND
a.greatest = GREATEST(b.round1mark, b.round2mark)
SQLFiddle here: http://sqlfiddle.com/#!9/3718ec/17
You could use greatest and an inner join on the subquery for max round mark
select m.bowler_id, m.record_date
, greatest(round, round2, round3, round4, round5, round6) highest_mark
from bowlmark m
inner join (
select record_date
, max(greatest(round, round2, round3, round4, round5, round6)) as max_round
from bowlmark
group by record_date
) t on t.record_date = m.record_date
and greatest(round, round2, round3, round4, round5, round6) = t.max_round
anyway a proper datababase normalization could help you to avoid this kind of problems

The query is not giving a desired output which I want

Query with OR which outputs wrong
SELECT DISTINCT
sm___employees.id,
sm___employees.employee_code,
sm___employees.leaving_date,
sm___employees.name_of_employee,
sm___employees.position,
sm___employees.rating,
sm___employees.entry_date
FROM
sm___employees
JOIN
sm___employee_skills
ON
sm___employees.id=sm___employee_skills.employee_id
WHERE
((sm___employee_skills.skill_id=1 AND sm___employee_skills.ans LIKE '%MBA%')
**OR**
(sm___employee_skills.skill_id=5 AND sm___employee_skills.ans IN (3)))
AND
sm___employees.rating IN (1)
ORDER BY
sm___employee_skills.date DESC
But I want it by And
SELECT DISTINCT
sm___employees.id,
sm___employees.employee_code,
sm___employees.leaving_date,
sm___employees.name_of_employee,
sm___employees.position,
sm___employees.rating,
sm___employees.entry_date
FROM
sm___employees
JOIN
sm___employee_skills
ON
sm___employees.id=sm___employee_skills.employee_id
WHERE
((sm___employee_skills.skill_id=1 AND sm___employee_skills.ans LIKE '%MBA%')
**AND**
(sm___employee_skills.skill_id=5 AND sm___employee_skills.ans IN (3)))
AND
sm___employees.rating IN (1)
ORDER BY
sm___employee_skills.date DESC
When am using first query with OR of MBA or 3, It gives me result for both which is correct as per OR operation
I want only those records which are having MBA AND 3 which gives me blank records when there are records available with this comparison
So please help me to resolve this.
Thank you in advance
To start with: DISTINCT often indicates a badly written query. This is the case here. You are joining records only to dismiss them later. If you want employee records, then select from the employee table. If you have criteria on the skills table check this in the WHERE clause. Don't join.
Then the WHERE clause looks at one row at a time. So neither skill_id = ... AND skill_id = ... nor skill_id = ... OR skill_id = ... can work for you. You must look up the skills table twice:
SELECT
id,
employee_code,
leaving_date,
name_of_employee,
position,
rating,
entry_date
FROM sm___employees
WHERE rating IN (1)
AND id IN
(
SELECT employee_id
FROM sm___employee_skills
WHERE skill_id = 1 AND ans LIKE '%MBA%'
)
AND id IN
(
SELECT employee_id
FROM sm___employee_skills
WHERE skill_id = 5 AND ans IN (3)
);
And here is a way to look up skills just once:
SELECT
id,
employee_code,
leaving_date,
name_of_employee,
position,
rating,
entry_date
FROM sm___employees
WHERE rating IN (1)
AND id IN
(
SELECT employee_id
FROM sm___employee_skills
WHERE (skill_id = 1 AND ans LIKE '%MBA%')
OR (skill_id = 5 AND ans IN (3))
GROUP BY employee_id
HAVING COUNT(DISTINCT skill_id) = 2 -- both skills
);
It seems strange though that you consider ans to be a string in one place (ans LIKE '%MBA%') and a number in another (ans IN (3)).
UPDATE: If you want to sort by skill date, you should consider by which skill's date. For this to happen, you would join, but not join the skills table, but the skills aggregate result. E.g.:
SELECT
e.id,
e.employee_code,
e.leaving_date,
e.name_of_employee,
e.position,
e.rating,
e.entry_date
FROM sm___employees e
JOIN
(
SELECT employee_id, MAX(date) AS max_date
FROM sm___employee_skills
WHERE (skill_id = 1 AND ans LIKE '%MBA%')
OR (skill_id = 5 AND ans = 3)
GROUP BY employee_id
HAVING COUNT(DISTINCT skill_id) = 2 -- both skills
) s ON s.employee_id = e.id
WHERE e.rating = 1
ORDER BY s.max_date;
Please try this :
SELECT DISTINCT
sm1.id,
sm1.employee_code,
sm1.leaving_date,
sm1.name_of_employee,
sm1.position,
sm1.rating,
sm1.entry_date
FROM sm___employees sm1
LEFT JOIN sm___employee_skills sm2 ON sm1.id = sm2.employee_id
WHERE ((sm2.skill_id=1 AND sm2.ans LIKE '%MBA%')
AND (sm2.skill_id=1 AND sm2.ans=3))
AND sm1.rating IN (1)
ORDER BY sm2.date DESC;

Issue with Mysql SUM and GROUP_CONCAT in query

I am having issue with my query having both SUM and GROUP_CONCAT function.
The sum values changes as GROUP_CONCAT values increases.
Below is my code:
SELECT ul.display_name,
ul.photo,
ul.user_id,
Sum(ulr.level_score) AS level_scores,
Sum(ulr.level_timer) AS level_timer,
Group_concat(ulr.level_completed) AS levels,
Group_concat(DISTINCT c.bit_id) AS bit_id
FROM user_level_responses AS ulr
INNER JOIN user_login AS ul
ON (
ul.user_id=ulr.user_id)
INNER JOIN c_member AS cm
ON (
cm.user_id=ul.user_id
AND cm.user_approval='Y'
AND cm.delete_status='0'
AND cm.status='1')
INNER JOIN ctree ct
ON (
cm.circuit_id=ct.circuit_id )
INNER JOIN cir AS c
ON (
c.circuits_id=cm.circuit_id
AND c.builtin=0
AND c.delete_status='0'
AND c.status='1')
WHERE Match(ct.circuit_path) against ('_902_')
AND ulr.institution_id=321
AND ulr.delete_status=0
AND ulr.status=1
AND ul.delete_status=0
GROUP BY ulr.user_id
ORDER BY level_scores DESC,
level_timer ASC,
ul.display_name limit 500
If the actual score is 900 and if i have 2 ids in GROUP_CONCAT then actual score is double the original.
Expected OUTPUT:
user1 2010.cs,btech 960 00:01:08 Completed
user2 btech 920 00:01:08 Completed
OUTPUT GETTING:
user1 2010.cs,btech 1920 00:01:08 Completed
user2 btech 920 00:01:08 Completed
twice the actual amount ie 960.
Your problem is that your multiple ids are doubling the rows that your result has before the grouping. You can solve this problem by joining in all of the external data in a subquery.
I have absolutely no idea the structure of your database, nor all of the functions, but this is a stab in the dark at reorganizing your query. If you need, I can write up a much simpler SQLFiddle to show you what I mean.
SELECT ul2.display_name,
ul2.photo,
ul2.user_id,
Sum(ulr.level_score) AS level_scores,
Sum(ulr.level_timer) AS level_timer,
Group_concat(ulr.level_completed) AS levels,
ul2.bit_id
FROM user_level_responses AS ulr
INNER JOIN (
SELECT
ul.display_name,
ul.photo,
ul.user_id,
GROUP_CONTACT(DISTINCT c.bit_id) as bit_id
FROM user_login AS ul
INNER JOIN c_member AS cm
ON (
cm.user_id=ul.user_id
AND cm.user_approval='Y'
AND cm.delete_status='0'
AND cm.status='1')
INNER JOIN ctree ct
ON (
cm.circuit_id=ct.circuit_id )
INNER JOIN cir AS c
ON (
c.circuits_id=cm.circuit_id
AND c.builtin=0
AND c.delete_status='0'
AND c.status='1')
WHERE Match(ct.circuit_path) against ('_902_')
AND ul.delete_status=0
GROUP BY ul.user_id
) AS ul2
ON (
ulr.user_id = ul2.user_id )
WHERE ulr.institution_id=321
AND ulr.delete_status=0
AND ulr.status=1
GROUP BY ulr.user_id
ORDER BY level_scores DESC,
level_timer ASC,
ul.display_name limit 500

Update Table by Join and Group by

A Company has many Reviews which has Rating Column itself.
CompID Ratig
12 3
13 3
17 4
22 4
23 5
24 3
28 3,2
This is what I need to be set to each company by id. Now Rating In Company Column is NULL.
I've written something like this:
UPDATE Companies c
JOIN Reviews r on c.CompanyID = r.CompanyID
SET c.Rating = AVG(r.rating)
group by r.CompanyID
This should do what you want using a simple nested query, in this case probably simpler than a JOIN.
UPDATE Companies
SET Rating =
(SELECT AVG(Rating)
FROM Ratings
WHERE Companies.CompanyId = Ratings.CompId)
Simple SQLfiddle demo here.
EDIT: If you really want to use a JOIN/UPDATE FROM, it'd look something like this;
UPDATE c
SET c.Rating = r.Rating
FROM Companies c
JOIN (SELECT AVG(Rating) Rating, CompID FROM Ratings GROUP BY CompId) r
ON c.CompanyId = r.CompId
At least to me, somewhat more complicated to read, and afaik it only works on SQL Server, but here's the SQLfiddle for that too :)
UPDATE ComisionesxColaboradorxLineaPrescripciones
SET CANTIDAD_PRODUCTOS_CORE_CUMPLE = CANTIDAD
FROM #ComisionesxColaboradorxLineaPrescripciones ComisionesxColaboradorxLineaPrescripciones
INNER JOIN
(SELECT TAB_L.COD_COLAB AS COD_COLAB,TAB_L.TIPO_COLABORADOR AS TIPO_COLABORADOR, COUNT(TAB_P.COD_SEG) AS CANTIDAD
FROM #ComisionesxColaboradorxLineaPrescripciones TAB_L
INNER JOIN #ComisionesxColaboradorxLineaxProductoPrescripciones TAB_P
ON TAB_L.COD_COLAB=TAB_P.COD_COLAB AND
TAB_L.TIPO_COLABORADOR=TAB_P.TIPO_COLABORADOR
GROUP BY TAB_L.COD_COLAB,TAB_L.TIPO_COLABORADOR
) AGRUPADO
ON ComisionesxColaboradorxLineaPrescripciones.COD_COLAB = AGRUPADO.COD_COLAB AND
ComisionesxColaboradorxLineaPrescripciones.TIPO_COLABORADOR = AGRUPADO.TIPO_COLABORADOR

avg in query - mysql

I have this query
SELECT salary
FROM worker W
JOIN single_user U ON u.users_id_user = W.single_user_users_id_user
JOIN university_has_single_user US ON US.single_user_users_id_user = U.users_id_user
JOIN course C ON C.id_course = US.course_id_course
JOIN formation_area FA ON FA.id_formation_area = C.formation_area_id_formation_area
WHERE FA.area = "Multimédia"
GROUP BY users_id_user
...that gave this output:
salary
--------
1400.00
800.00
How can I calculate the avg of this output? If I add:
SELECT round(avg (salary), 0)
...the output is again 1400.00 and 800.00, not the avg (because the group by).
Use:
SELECT AVG(DISTINCT salary)
FROM worker W
JOIN single_user U ON u.users_id_user = W.single_user_users_id_user
JOIN university_has_single_user US ON US.single_user_users_id_user = U.users_id_user
JOIN course C ON C.id_course = US.course_id_course
JOIN formation_area FA ON FA.id_formation_area = C.formation_area_id_formation_area
WHERE FA.area = "Multimédia"
Because the salary column is not wrapped in an aggregate, per the documentation, the values you see are arbitrary (can't be guaranteed 100% of the time).
Usually, you'd need a derived table to get the average of the distinct values but MySQL's AVG supports using DISTINCT within it.