I am using the following results table
Results Table
__________________________________________
player_a| player_b | player_c | year
________|________ |__________|___________|
100 150 150 2015
100 -50 -50 2015
100 350 250 2014
200 350 250 2014
What i would like to do is get the sum for each player per year.
Using the following Select i get the desired results.
SELECT (
SELECT SUM( player_a )
FROM `results`
WHERE year =2015
) AS player_a_2015, (
SELECT SUM( player_a)
FROM `results`
WHERE year =2014
) AS player_a_2014
How can i get the maximum sum result only ?
ie For player_a should be 300
SELECT SUM(player_a) AS spa
FROM results
GROUP BY year
ORDER BY spa DESC
LIMIT 1
You may wanna use,
SELECT MAX(SUM_A),YEAR FROM (
SELECT SUM( player_a ) as SUM_A, YEAR
FROM results
GROUP BY YEAR
) A_SCORE;
This will do it in one go(obviously for one player).
This should do it.
Make sure you wrap your OR condition in parenthesis so it evaluates them both.
SELECT SUM(player_a) AS sum_score
FROM results
WHERE (year = '2015' OR year = '2014')
EDIT
If you're wanting the maximum score by year, and then to sum that - this should be OK.
SELECT MAX(max_sum_score) AS max_sum_score
FROM ( SELECT year
, SUM(player_a) AS max_sum_score
FROM results
WHERE (year = '2015' OR year = '2014')
GROUP BY year
) a
Related
I have four tables with the following structure.
Table 1:
Project - have unique project names (prj_name)
Table 2:
my_records - have the following fields:
record_id,prj_name,my_dept,record_submit_date,record_state
Table 3:
record_states have multiple states where 'Completed' is one.
Table 4:custom_dept_list
dept_name
I need to get the percentage of (records have state as completed) and (Total records) grouped by my_project where my_dept in custom_dept_list and record_submit_date is greater than "some date"
I have tried the following:
Query:
select prj_name,count(record_id) as total,((select count(record_id) from
my_records where record_state='Completed')/(count(record_id)))*100 as
percent from my_records,custom_dept_list where record_state='Completed'
and record_submit_date >= ( CURDATE() - INTERVAL 15 DAY ) and
my_dept=dept_name group by prj_name order by percent desc;
Total records for project A = 50
Total records for project A with record_state='Completed' = 30
Ratio is not coming - (30/50)*100 = 60
It is giving some very big value.
Below is the data from my_records, i have removed record_submit date to make it simple:
|1|prj1|dept1|Completed
|2|prj1|dept1|XYZ
|3|prj1|dept1|Completed
|4|prj1|dept2|XYZ
|5|prj1|dept2|Completed
|6|prj1|dept1|XYZ
|7|prj1|dept1|XYZ
|8|prj1|dept1|XYZ
|9|prj1|dept2|XYZ
|10|prj1|dept2|XYZ
|11|prj1|dept2|Completed
|12|prj1|dept2|Completed
|13|prj1|dept2|Completed
|14|prj1|dept3|XYZ
|15|prj1|dept4|Completed
|16|prj1|dept4|XYZ
|17|prj1|dept5|Completed
|18|prj1|dept6|XYZ
|19|prj1|dept7|XYZ
|20|prj1|dept8|XYZ
|21|prj1|dept10|XYZ
|22|prj1|dept2|XYZ
|23|prj1|dept2|Completed
|24|prj1|dept2|Completed
|25|prj1|dept2|Completed
Data From Custom_dept_List:
dept_name
dept1
dept3
dept4
dept5
dept6
dept8
dept10
I have tried the following queries :
Query 1
select count(record_id) as count,prj_name from my_records,custom_dept_list where my_dept=dept_name group by prj_name order by count desc;
Ouput -- 13
Query 2
select count(record_id) as count,prj_name from my_records,custom_dept_list where my_dept=dept_name and record_state='Completed' group by prj_name order by count desc;
Output -- 4
Query 3
select prj_name,count(record_id) as total,count(case when record_state='Completed' then record_id end) /count(record_id) *100 as percent from my_records join custom_dept_list on my_dept = dept_name where record_state = 'Completed' group by prj_name order by percent desc;
Output :
prj_name total percent
prj1 4 100.0000
First of all, please use proper join instead of multiple tables in your from clause.
Then, you don't need that inner query to get the count with a specific record_state, you can use a case inside the count:
select prj_name,
count(record_id) as total,
count(case when record_state='Completed' then record_id end) /
count(record_id) * 100 as percent
from my_records
join custom_dept_list
on my_dept = dept_name
where record_submit_date >= ( CURDATE() - INTERVAL 15 DAY )
group by prj_name
order by percent desc;
Your problem was probably caused by that inner query, that was not counting each project's completed records, but all the completed records instead.
you do not need this record_state = 'Completed' condition because of this you get only completed record as total recoded. so try without it.
select prj_name,
count(record_id) as total,
count(case when record_state='Completed' then record_id end) /
count(record_id) * 100 as percent
from my_records
join custom_dept_list
on my_dept = dept_name
where record_submit_date >= ( CURDATE() - INTERVAL 15 DAY )
group by prj_name
In MySQL, I got a table similar to :
id user_id date
1 1 2014-09-27
2 1 2014-11-05
3 1 2014-11-14
4 2 2014-12-03
5 1 2014-12-23
I would like to select the total monthly amount of people.
ExpectedOutput : 4
2014-09 = 1 user
2014-10 = 0 user
2014-11 = 1 user //user 1 is present twice in november, but I want him only once per month
2014-12 = 2 user
total expected = 4
So far, my request is :
SELECT count(id)
FROM myTable u1
WHERE EXISTS(
SELECT id
FROM myTable u2
WHERE u2.user_id = u1.user_id
AND DATE_SUB(u2.date, INTERVAL 1 MONTH) > u1.date
);
It ouput the correct amount, but on my (not so heavy) table, it take hours to execute. Any hints to make this one lighter or faster ?
Bonus :
Since INTERVAL 1 MONTH is not available in DQL, is there any way to do it with a Doctrine QueryBuilder ?
Try this!
It should give you exactly what you need...
SELECT
EXTRACT(YEAR FROM dates) AS the_year,
EXTRACT(MONTH FROM dates) AS the_month,
COUNT( DISTINCT user_id ) AS total
FROM
myTable
GROUP BY
EXTRACT(YEAR FROM dates),
EXTRACT(MONTH FROM dates);
For you problem, what I would do is :
Creating a subrequst grouping the distinct sum of people by month
Creating a request making the sum of the sub-result.
Here is a working example (with your datas) sqlFiddle
And here is the request :
SELECT SUM(nb_people)
FROM (
-- This request return the number of distinct people in one month.
SELECT count(distinct(user_id)) AS nb_people, MONTH(`date`), YEAR(`date`)
FROM test
GROUP BY MONTH(`date`)
) AS subQuery
;
SELECT COUNT(DISTINCT user_id), YEAR(date) + '-' + MONTH(date)
FROM MyTable
GROUP BY YEAR(date), MONTH(date)
I am getting count of some column by using group by. It returns results of the form,
col1 count
date1 1
date2 2
I would like to further count on this result so that it returns 3.
How would I go about accomplishing this?
SELECT t.p_date
,count(t.p_date) AS saturday
FROM t_p_booking t
WHERE t.p_id IN (
220
,221
)
AND dayofweek(t.p_date) = 7
AND date_format(t.p_date, '%Y%m') = : ccyymm
GROUP BY t.p_date
Add WITH ROLLUP to the end of your GROUP BY clause
Would that not just be:
SELECT count(*)
FROM t_p_booking t
WHERE t.p_id IN (
220
,221
)
AND dayofweek(t.p_date) = 7
AND date_format(t.p_date, '%Y%m') = : ccyymm
GROUP BY t.p_date
I have a table storing weatherdata in 5min intervals.
Now I need result with total raindays grouped by Year like this:.
2010 100 raindays
2011 120 raindays
2013 90 raindays
This is my current query:
SELECT date, SUM(rain) FROM $tableName GROUP BY date HAVING SUM(rain) > 0";
as expected, this gives me following result:
2010-01-02 1.2 (mm)
2010-01-05 1.6 (mm)
2010-02-10 2.6 (mm)
How I have to change my query, to have this grouped by year(date) and counted days ?
Thanks all for your help
PM
You can group by YEAR(date), to sum the rain grouped by year. Then to count the number of days you can COUNT DISTINCT the days without the time part, using DATE(date) function.
SELECT YEAR(date), SUM(rain), COUNT(DISTINCT DATE(date)) days
FROM $tableName
WHERE rain>0
GROUP BY YEAR(date)
Please see fiddle here.
The following query might help you if you want to find maximum element is each group,
select
e.date, e.rain
from
TableName e
join
(select
date, MAX(rain) MS
from
TableName
group by date
Having rain > 0) m ON e.date = M.date and e.rain = m.rain
I'm looking for a way to write one query to compare the results of multiple mysql subqueries, and return users that are in each query.
I have a that contains fantasy football stats for players. To simplify, in this case there are 3 columns I'm using: player, points, year.
I'm looking to run a query that returns a list of players who finished in the top 50 (based on points) in both 2010 and 2011.
I've done lots of searching around on playing with subqueries, doing joins on one table, etc but am still coming up at a loss on how to approach this.
You can do something like this:
SELECT a.player
FROM (SELECT player FROM players WHERE Year = 2010 ORDER BY points DESC LIMIT 50) a
JOIN
(SELECT player FROM players WHERE Year = 2011 ORDER BY points DESC LIMIT 50) b
ON a.player = b.player
Here is an example. I assumed that you calculate top50 based on sum of points and you have several entries for each player in each year.
select y2010.player
from (
select player, sum from (
select st1.player player, sum(st1.points) sum from stats st1 where st1.year = 2010 group by st1.player order by sum desc
) t1 limit 50 offset 0
) y2010, (
select player, sum from (
select st1.player player, sum(st1.points) sum from stats st1 where st1.year = 2011 group by st1.player order by sum desc
) t1 limit 50 offset 0
) y2011
where y2010.player = y2011.player
You can use a UNION ALL, this will get you the Top 50 in both years and put them in the same result set, no joining required:
(
select player, year, points
from players
where year = 2010
order by points desc
limit 50
)
union all
(
select player, year, points
from players
where year = 2011
order by points desc
limit 50
);
It's slightly ambiguous whether you want:
all players who finished in the top 50 in 2010, as well as all players who finished in the top 50 in 2011:
SELECT *
FROM scores
WHERE year = 2010
AND points >= (SELECT MIN(points) FROM (
SELECT points
FROM scores
WHERE year = 2010
ORDER BY points DESC
LIMIT 50
) t)
UNION ALL
SELECT *
FROM scores
WHERE year = 2011
AND points >= (SELECT MIN(points) FROM (
SELECT points
FROM scores
WHERE year = 2011
ORDER BY points DESC
LIMIT 50
) t)
all players who finished in the top 50 in both 2010 and 2011, in which case you'll need to further group the results:
SELECT player
FROM (
-- query as above
) t
GROUP BY player
HAVING COUNT(DISTINCT year) = 2