MySQL display team standings from results table - php

I have two tables:
teams
---------------------
team_id team_name
---------------------
1 Lakers
2 Clippers
3 Grizzlies
4 Heat
results
...............................................................
game_id team_id1 team_id2 team1_score team2_score
1 1 2 20 30
2 1 3 40 50
3 2 1 50 60
4 4 2 20 30
5 1 2 20 30
My question is, how can I then create standings results for this tables based on results sorted by points, like this:
...............................................................
Position team_name total_points games_played
1 Lakers 140 4
2 Clippers 110 3
3 Grizzlies 50 1
4 Heat 20 1

I guess what you would like to do is something like this:
EDITED
SET #num :=0;
SELECT (#num := #num + 1) as Position,team_name,total_points,games_played
FROM (
SELECT teams.team_name, SUM(p) as total_points, count(*) as games_played
FROM (
SELECT team_id1 as id, team1_score as p FROM results
UNION ALL
SELECT team_id2 as id, team2_score as p FROM results
) t
INNER JOIN teams ON t.id = teams.team_id
GROUP BY id,teams.team_name ) t2
ORDER BY total_points DESC;
SQLFiddle is here : http://www.sqlfiddle.com/#!2/5bf2c/1
If you would like to display all teams, even if some teams did not play a single game, you could go like this:
SET #num :=0;
SELECT (#num := #num + 1) as Position,team_name,total_points,games_played
FROM (
SELECT
teams.team_name,
SUM(p) as total_points,
SUM(f) as games_played
FROM (
SELECT team_id1 as id, team1_score as p, 1 as f FROM results
UNION ALL
SELECT team_id2 as id, team2_score as p, 1 as f FROM results
UNION ALL
SELECT team_id as id, 0 as p, 0 as f FROM teams
) t
INNER JOIN teams ON t.id = teams.team_id
GROUP BY id,teams.team_name ) t2
ORDER BY total_points DESC;
SQLFiddle is here: http://www.sqlfiddle.com/#!2/8ecf5d/9
Hope this helps.

I may have renamed a couple of your columns... oh, and teams 1 and 2 are tied so you need to say a little more about how to resolve that...
SELECT t.*
, COUNT(*) p
, SUM(score) pts
FROM
(
SELECT home_team_id team_id,home_team_score score FROM results
UNION ALL
SELECT away_team_id,away_team_score FROM results
) x
JOIN teams t
ON t.team_id = x.team_id
GROUP
BY t.team_id
ORDER
BY pts DESC;

select team_id,team_name,sum(sc1) ,sum(ct1) from
(select team_id,teams.team_name,sc1,ct1 from
(select team_id1,sum(team1_score)as sc1,count(team_id1) as ct1 from results group by team_id1) as srtbl1,teams
where srtbl1.team_id1 =teams.team_id
union
select team_id,teams.team_name,sc2,ct2 from
(select team_id2,sum(team2_score)as sc2 ,count(team_id2) as ct2 from results group by team_id2) as srtbl2,teams
where srtbl2.team_id2 =teams.team_id) sctbl
group by team_id;
and I think you make a mistake in calculating .

Related

sum multiple columns of same table in mysql

I am working on developing sports site.
In this site I've two tables one is tbl_team which stores the team details and another table is tbl_tournamentmatches which stores the details of different rounds of tournament matches.
In my tbl_tournamentmatches I'm storing team1_id, team2_id, team1_score and team2_score.
My Table having entry like this:
tbl_tournamentmatches
match_id team1_id team2_id team1_score team2_score
5 4 9 15 5
6 9 16 15 5
7 4 16 5 15
8 4 16 5 15
tbl_team
team_id team_title
4 KKR
9 RR
16 CSK
I want Result Should look like this:-
Team name Score
CSK 35
KKR 25
RR 20
I'd used this query :-
select * from
(
SELECT sum(team1_points) as totalpoints,t.team_title
from tbl_team t
left join tbl_tournamentmatches m
on t.team_id = m.team1_id
where tournament_id = 3 AND agegroup_id = 36
group by team1_id
union
SELECT sum(team2_points) as totalpoints,t.team_title
from tbl_team t
left join tbl_tournamentmatches m
on t.team_id = m.team2_id
where tournament_id = 3 AND agegroup_id = 36
group by team2_id
) s
But i got result like this :-
KKR 25
RR 15
RR 5
CSK 35
Any Help will be appreciated. Thanks in advance.
Do the joins to get the team and points for each match, and then do the sum on the results of that:-
SELECT team_title, sum(team_points) as totalpoints
FROM
(
SELECT team1_id AS team_id, team1_points AS team_points, t.team_title
FROM tbl_team t
LEFT JOIN tbl_tournamentmatches m
ON t.team_id = m.team1_id
WHERE tournament_id = 3 AND agegroup_id = 36
UNION ALL
SELECT team2_id AS team_id, team2_points AS team_points, t.team_title
FROM tbl_team t
LEFT JOIN tbl_tournamentmatches m
ON t.team_id = m.team2_id
WHERE tournament_id = 3 AND agegroup_id = 36
) s
GROUP BY team_id, team_title
try this query
SELECT t.team_name
,SUM(CASE
WHEN t.team_id = tn.team1_id THEN tn.team1_score
WHEN t.team_id = tn.team2_id THEN tn.team2_score
END) AS score
FROM team t
LEFT JOIN tournament tn ON t.team_id = tn.team1_id OR t.team_id = team2_id
GROUP BY t.team_name
You almost got it.
In your query I see fields that you don't include in your table (e.g. agegroup_id), but you only are a step of a solution: change your outter "select * from " for a: "select sum(totalpoints), team_title" and add a group by at the end: "group by team_title"
select sum(totalpoints) as totalpoints, team_title from
(
SELECT sum(team1_score) as totalpoints, t.team_title
from tbl_team t
join tbl_tournamentmatches m
on t.team_id = m.team1_id
--where tournament_id = 3 AND agegroup_id = 36
group by team1_id
union
SELECT sum(team2_score) as totalpoints, t.team_title
from tbl_team t
join tbl_tournamentmatches m
on t.team_id = m.team2_id
--where tournament_id = 3 AND agegroup_id = 36
group by team2_id
) s
group by team_title;
Use Union all to make all team and score as different rows.
Then use an Inner Join.
Query
select t2.team_title as TeamName,
sum(team_score) as Score
from
(
select match_id,team1_id as team_id, team1_score as team_score
from tblMatch
union all
select match_id,team2_id as team_id, team2_score as team_score
from tblMatch
)t1
join tblTeam t2
on t1.team_id = t2.team_id
group by t2.team_id;
SQL Fiddle

grouping by Leg but also joining on the site that belongs to the earliest collection time

I need to do the following.
I have 9 records linked by a schedule_id.
these 9 records are broken into 3 sets of 3 for example of Leg 1 leg 2 and leg 3,
so i have the following
order sched leg site date
1 1 1 1 2014-01-01
2 1 2 34 2014-12-31
3 1 1 15 2014-04-23
4 1 3 4 2014-07-12
5 1 2 89 2014-04-10
6 1 1 13 2014-09-09
7 1 3 9 2014-03-12
8 1 3 10 2014-11-10
9 1 2 11 2014-08-08
In my sql I group by leg so I land up with 3 records.
I need to extract the site ids that belong to the earliest and latest date in each leg group and then join my site information table to the resulting ids to extractcompanynames etc for these sites.
my sql code for the legs is as follows
select l.leg_no,
l.start_region,
r.region_name as end_region,
l.rate,
sum(x.est_distance) as distance,
sum(x.est_weight) as weight,
count(x.order_id) as count,
min(x.req_col_time) as earliesttime,
sum(x.total_rpb) as total_rpb,
max(x.req_del_time) as latesttime from
(select ord2.* from loads as l1
left join orders as ord2 on ord2.load_id = l1.load_id
left join debrief_docs as db2 on db2.oid = ord2.order_id
where l1.schedule_id = '.$id.'
union
select ord3.* from loads as l2
left join drops as drop1 on drop1.load_id = l2.load_id
left join orders as ord3 on ord3.drop_id = drop1.drop_id
left join debrief_docs as db3 on db3.oid = ord3.order_id
where l2.schedule_id = '.$id.')as x
join loads as l on l.schedule_id = '.$id.'
join regions as r on r.region_id = l.region_id
group by l.leg_no asc
Any ideas on how to achieve this would be greatly appreciated. ;)
thank you all
Let's approach the important part of the question, which is getting the first and last site ids for each leg group. I would use variables for this purpose. The following query enumerates the leg ids:
select l.*,
(#rn := if(#l = leg_id, #rn + 1,
if(#l := leg_id, 1, 1)
)
) as seqnum
from loads l cross join
(select #rn := 0, #l := 0) vars
order by schedule_id, leg_id, date;
The following summarizes the records and produces two columns, the first and last site:
select schedule_id, leg_id,
max(case when seqnum = 1 then site_id end) as first_site,
max(case when seqnum = 3 then site_id end) as last_site
from (select l.*,
(#rn := if(#l = leg_id, #rn + 1,
if(#l := leg_id, 1, 1)
)
) as seqnum
from loads l cross join
(select #rn := 0, #l := 0) vars
order by schedule_id, leg_id, date
) l
group by schedule_id, leg_id;
You can then join in the rest of the tables to get the additional information that you want.

Sql request into 2 tables to get percentage of each rows

I have 2 SQL tables like this:
___Rooms:
ROO_Id ROO_Number
1 101
2 201
___Bookings:
BOO_Id BOO_RoomNumber
1 1
2 1
3 2
I want to echo a table with all rooms I have with percentage for each.
BOO_RoomNumber percentage count
101 66% 2
201 33% 1
Thanks.
Here is the answer:
UPDATED
select r.ROO_Number BOO_RoomNumber, ((ifnull(cnt_book,0)*100)/(select count(*) from Bookings)) percentage, ifnull(cnt_book,0) `count`
from Rooms r left join (select BOO_RoomNumber, count(*) cnt_book
from Bookings
group by BOO_RoomNumber) cnt on r.ROO_Id=cnt.BOO_RoomNumber
Working SQLFiddle
Try to use this:
SELECT r.id as BOO_RoomNumber, ((booked/(select count(id) from BOOKINGS))*100) as percentage, booked as `count`
FROM ROOMS as r
LEFT JOIN
(
SELECT count(id) as booked, roomId
FROM BOOKINGS
GROUP BY roomId
) as b on b.roomId=r.id

How to show rank using mysql query

user_id | date | point
1 20 4
2 20 3
3 20 2
1 21 1
2 21 3
3 21 5
1 23 2
2 23 4
3 23 5
And query:
SELECT user_id, SUM(point) AS point, #row:=#row+1 rank FROM users GROUP BY user_id
How to show rank in this query ?
first you need to delcare #row as variable first
Something like set #row=0; select user_id,SUM(point) as point,#row := #row + 1 as rank from users GROUP BY user_id order by SUM(point) desc;
try this
SET #row=0;
SELECT user_id, SUM(point) AS point, #row:=#row+1 rank FROM users GROUP BY user_id
You can work around the declaring part using this way:
SELECT user_id, SUM(point) AS point, #row:=#row+1 rank FROM users, (SELECT #row := 0) r GROUP BY user_id
I guess you've to write it like:
SELECT user_id, SUM(point) AS point, #row:=#row+1 AS rank FROM users GROUP BY user_id JOIN (SELECT #row := 0) r;
Try this..
SELECT user_id,points, #row:=#row+1 AS Row
from (
SELECT user_id,
SUM(point) AS points rank
FROM users
GROUP BY user_id
Sort by points);
Should have an AS to alias #row to rank, and an order by on point to get them in the right order to rank:-
SELECT user_id, SUM(point) AS point, #row:=#row+1 AS rank
FROM users
CROSS JOIN (SELECT #row:=0) Sub1
GROUP BY user_id
ORDER BY point DESC

adding and multiplying COUNT() of several tables

Is it possible to add and multiply the count of different tables where the id is the same?
Imagine:
Table_1 Table_2 Table_3
id id id
1 1 1
1 2 2
2 2 3
3 2 3
3 2 3
3 3 3
So that the end result would be this table with 2 columns:
id (COUNT(Table_1.id) + 2*COUNT(Table_2.id) + 3*COUNT(Table_3.id))
1 7
2 12
3 17
I don't know if I understood you correctly but give this a try,
SELECT a.ID,
a.aa + (2 * b.bb) + (3 * c.cc)
FROM
(
SELECT ID, COUNT(*) aa
FROM table1
GROUP BY ID
) a LEFT JOIN
(
SELECT ID, COUNT(*) bb
FROM table2
GROUP BY ID
) b ON a.ID = b.ID
LEFT JOIN
(
SELECT ID, COUNT(*) cc
FROM table3
GROUP BY ID
) c ON a.ID = c.ID
SQLFiddle Demo
SELECT id, counts_1.number + 2 * counts_2.number + 3 * counts_3.number
FROM
(SELECT id, COUNT(*) AS number FROM Table_1 GROUP BY id) AS counts_1
JOIN
(SELECT id, COUNT(*) AS number FROM Table_2 GROUP BY id) AS counts_2 USING (id)
JOIN
(SELECT id, COUNT(*) AS number FROM Table_3 GROUP BY id) AS counts_3 USING (id)
Note that this solution requires that every id exists at least once in each of the tables, otherwise it will be left out of the result. Changing this would require a FULL OUTER JOIN that MySQL is incapable of. There are ways around that limitation, though.

Categories