Top 10 players with the highest batting average - mysql

Batting average = total number of runs scored / number of times out.
Here we need to make sure to include run outs (on non-striker end)
Output like :
Batsman_name Average
KL Rahul 44
Error:
Error in SQL statement: AnalysisException: cannot resolve 'batsman_runs' given input columns: [_auto_generated_subquery_name.Batsman]; line 1 pos 21;
'Sort ['Average DESC NULLS LAST], true
select Batsman_, sum(batsman_runs)/count(player_dismissed) as Average
from
(
(select batsman as Batsman_ from IPL_BALL_BY_BALL)
union all
(select non_striker as Batsman_ from IPL_BALL_BY_BALL)
)
group by Batsman_
order by Average desc;

because from your subquery ( result of union) you only returning "Batsman_" column. you have to retunr all the column you need :
select Batsman_, sum(batsman_runs)/count(player_dismissed) as Average
from
(
(select batsman as Batsman_,batsman_runs,player_dismissed from IPL_BALL_BY_BALL)
union all
(select non_striker as Batsman_,batsman_runs,player_dismissed from IPL_BALL_BY_BALL)
)
group by Batsman_
order by Average desc;

select Batsman_, sum(batsman_runs)/count(player_dismissed) as Average from
((select batsman as Batsman_,batsman_runs,player_dismissed from IPL_BALL_BY_BALL) union all (select non_striker as Batsman_,batsman_runs,player_dismissed
from IPL_BALL_BY_BALL))
group by Batsman_ order by Average desc;
You forgot fields "batsman_run & player_dismissed" in your inner sql

Related

SQL numbering merge if string is the same

I want to create a stats page for my website. However I have the problem that if I run my query it will not be merged if the sting is the same. Any idea how to fix this?
My query:
SELECT * FROM (
SELECT dj_1, count(*) AS uren
FROM dj_rooster
WHERE week = '28' GROUP BY id_1
ORDER BY count(*) DESC
) x
UNION ALL
SELECT * FROM (
SELECT dj_1, count(*) AS uren
FROM djpaneel_shows_uren
WHERE week = '28' AND active = '1'
GROUP BY dj_1
ORDER BY count(*) DESC
) x
My results:
Jack - 7
Jeremy - 5
Jack - 1
Thanks in advance for your help
There is no need to aggregate twice or more.
Use UNION ALL to get all the rows from the 2 tables and then aggregate:
SELECT t.dj_1, COUNT(*) AS uren
FROM (
SELECT dj_1 FROM dj_rooster
WHERE week = '28'
UNION ALL
SELECT dj_1 FROM djpaneel_shows_uren
WHERE week = '28' AND active = '1'
) t
GROUP BY t.dj_1
ORDER BY uren DESC

SQL Group by returning wrong results

I have a salary table in which I am trying to return determine the lowest salary earned and by which industry for each year however despite getting the correct lowest salary earned I am receiving the wrong industry name.
I am aware that it is due to the fact that I have utilized GROUP BY without placing a constraint(?) on it hence it is returning me the wrong value but I am not sure how I can solve it.
SALARY TABLE
salaryID
salaryAmount
salaryYear
industryName (ForeignKey)
Can someone please guide me on the right path?
**(Problem Code)**
SELECT MIN(S.salary), S.industryName, S.salaryYear
FROM salary
GROUP BY S.salaryYear;
**(Attempted solution)**
SELECT S.salary
FROM salary
INNER JOIN
SELECT (min(S1.amount)), S1.year, S1.industryName, S1.salaryId
FROM salary S1
GROUP BY S1.year
ON S.salaryId = S1.salaryId);
Use a proper GROUP BY. Any non-aggregated columns must be included in GROUP BY.
SELECT MIN(amount), year
FROM salary
GROUP BY year
If you want to include industryName,
SELECT amount, year, industryName, salaryId
FROM (
SELECT amount, year, industryName, salaryId
, ROW_NUMBER() OVER(PARTITION BY year ORDER BY amount) AS rn
FROM salary
) a
WHERE rn = 1
Pre-MySQL 8 version
SELECT *
FROM salary s
INNER JOIN (
SELECT MIN(amount) AS minAmount, year
FROM salary
GROUP BY year
) m ON m.minAmount = s.amount AND m.year = s.year
I think you need a self-join :
SELECT s1.industryName, s2.min_salary, s2.salaryYear
FROM salary s1
JOIN
(
SELECT MIN(salary) as min_salary, salaryYear
FROM salary
GROUP BY salaryYear
) s2
ON s1.salary = s2.min_salary
AND s1.salaryYear = s2.salaryYear;
The Demo of this query with your sample data

my query doesn't return the max from value from the sum of working hours of the month

I want to find name of that persons who worked most in a month. but the query doesn't returning max value from sum of value
I'm new in mysql
SELECT
x.name,
sec_to_time(MAX(x.sum_time)) maximum
FROM (
SELECT
name,
SUM(TIME_TO_SEC(ending_time) - TIME_TO_SEC(starting_time)) sum_time
FROM working_hours wh, employees
WHERE wh.employees_id = employees.id
AND project_id IS NOT NULL
GROUP BY employees_id
) x
GROUP BY x.name;
this is my query. i want to show just name of that persons who worked most in a month. but it returns all persons who worked in a month
Try making these changes to your query:
change name to MAX(name)
qualify employees_id with wh.employees_id
SELECT
x.name,
sec_to_time(MAX(x.sum_time)) maximum
FROM (
SELECT
MAX(name) AS name,
SUM(TIME_TO_SEC(ending_time) - TIME_TO_SEC(starting_time)) sum_time
FROM working_hours wh, employees
WHERE wh.employees_id = employees.id
AND project_id IS NOT NULL
GROUP BY wh.employees_id
) x
group by x.name;
Simply use Order by LIMIT -
SELECT X1.name, X1.maximum
FROM (SELECT name, SUM(TIME_TO_SEC(ending_time) - TIME_TO_SEC(starting_time)) maximum
FROM working_hours wh, employees
WHERE wh.employees_id=employees.id
GROUP BY name) X1
JOIN (SELECT SUM(TIME_TO_SEC(ending_time) - TIME_TO_SEC(starting_time)) sum_time
FROM working_hours wh, employees
WHERE wh.employees_id=employees.id
AND project_id is not null
GROUP BY employees_id
ORDER BY sum_time DESC
LIMIT 1) X2 ON X2.sum_time = X1.maximum;

Get maximum value from two values

I have a table which gives the no of rides by a rider at each stand point. I need to find the stand for each rider for which he has the maximum rides.
My first result is in this format: 1
I require my final result like this: 2
I'm currently using this query, but I know it can be done in a better manner. Any suggestions would be helpful.
select c.rider_id, c.end_stand, b.max_rides
from
(select rider_id, max(rides) as max_rides
from
(select rider_id, end_stand, count(id) as rides
from ride where end_stand is not null
group by 1,2) a
group by 1
order by 2 desc, 1) b
join
(select rider_id, end_stand, count(id) as rides
from ride where end_stand is not null
group by 1,2) c
on c.rider_id = b.rider_id and c.rides = b.max_rides
order by 3 desc, 2,1
Before window functions, one method is a correlated subquery in the having clause:
select rider_id, end_stand, count(*) as rides
from ride r
where end_stand is not null
group by rider_id, end_stand
having count(*) = (select count(*)
from ride r2
where r2.end_stand is not null and
r2.rider_id = r.rider_id
group by r2.rider_id, r2.end_stand
order by count(*) desc
limit 1
);
With window functions, this is, of course, much simpler:
select *
from (select rider_id, end_stand, count(*) as rides
rank() over (partition by rider_id order by count(*) desc) as seqnum
from ride r
where end_stand is not null
group by rider_id, end_stand
) r
where seqnum = 1;
Both these will return duplicates, if there are ties for the max. The second version is easy to fix, if you want only one row: use row_number() instead of rank().

Join 2 Select Queries in a Same Table in MySql

I have a Table Name Called tbl_events and it does have following columns
Id,Event_Name, District,Branch_Type,Points
I need a Sum of Points Column Where Branch_Type=2; and Sum of Points Divided by 10 Where Branch_Type=2 after that I have to Add those Two Values And Group that Result by District and Order by Desc. I tried this Query but Seems to something wrong Can anyone help, please?
Select (t1.B_Points + t2.D_Points) as T_Points,District From
(Select Sum(Points)*.1 as B_Points ,District From tblstudents Where Branch_Type=3 group by District)t1
Left Join(Select Sum(Points) as D_points, District From tblstudents Where Branch_Type=2 group by District)t2 on
(t1.District=t2.District) Order by Desc
You need to add column alias T_Points in order by
Select (t1.B_Points + t2.D_Points) as T_Points,District
From
(
Select Sum(Points)*.1 as B_Points ,District From tblstudents
Where Branch_Type=3 group by District
)t1
Left Join
(
Select Sum(Points) as D_points, District From tblstudents
Where Branch_Type=2 group by District
)t2 on t1.District=t2.District
Order by T_Points Desc
You seem to just want conditional aggregation:
select district,
sum(case when branch_type = 3 then 0.1 * points
when branch_type = 2 then points
else 0
end) as t_points
from tblstudents
group by district;
If you want to order by the points descending, then add:
order by t_points desc
Note: This assumes that you want districts that have neither branch type. If that is not an issue, move the logic to the where clause:
select district,
sum(points * (case when branch_type = 3 then 0.1 else 1.0 end) as t_points
from tblstudents
where branch_type in (2, 3)
group by district
order by t_points desc;