SQL Get most frequent value from a column based on a condition - mysql

This query
SELECT
PlayerID, HeroTypeID, HeroTypeIDCount, Wins / (Losses + Wins) AS WinRate, Wins, Losses
FROM (
SELECT E.PlayerID AS PlayerID,
FK_HeroTypeID AS HeroTypeID,
COUNT(FK_HeroTypeID) AS HeroTypeIDCount,
SUM(CASE WHEN D.Result = 'LOSS' THEN 1 ELSE 0 END) AS Losses,
SUM(CASE WHEN D.Result = 'WIN' THEN 1 ELSE 0 END) AS Wins
FROM GamePlayerDetail D
JOIN Player E
ON D.FK_PlayerID = E.PlayerID
JOIN Game I
ON D.FK_GameID = I.GameID
WHERE PlayedDate BETWEEN DATE_SUB(CURDATE(), INTERVAL 7 DAY) AND CURDATE()
GROUP BY E.PlayerID, FK_HeroTypeID
) AS T
ORDER BY PlayerID
produces the following result:
# PlayerID, HeroTypeID, HeroTypeIDCount, WinRate, Wins, Losses
'1', '11', '1', '1.0000', '1', '0'
'1', '13', '3', '0.3333', '1', '2'
'1', '24', '5', '0.8000', '4', '1'
'1', '27', '1', '1.0000', '1', '0'
'2', '28', '1', '0.0000', '0', '1'
'2', '6', '1', '0.0000', '0', '1'
'2', '30', '1', '0.0000', '0', '1'
'2', '7', '1', '1.0000', '1', '0'
What I'd like to do is get the most frequent FK_HeroTypeID (which is also highest value of HeroTypeIDCount) per PlayerID, but in case of ties, the highest winrate should take precedence. Here's an example of what I'd like to get:
PlayerID, HeroTypeID, HeroTypeIDCount, WinRate, Wins, Losses
1, 24, 5, 0.8000, 4, 1
2, 7, 1, 1.0000, 1, 0
How should you write a query like this?
Edit:
Ok, here's a simple Create/Insert table for the produced result.
http://sqlfiddle.com/#!9/d644a

SELECT playerid
, herotypeid
, herotypeidcount
, winrate
, wins
, losses
FROM
( SELECT *
, CASE WHEN #prev=playerid THEN #i:=#i+1 ELSE #i:=1 END rank
, #prev:=playerid prev
FROM table1
, (SELECT #prev:=null,#i:=0) vars
ORDER
BY herotypeidcount DESC
, winrate DESC
) x
WHERE rank = 1;
Here's a 'hack' solution. It works, but really shouldn't be relied upon...
SELECT *
FROM
( SELECT *
FROM table1
ORDER
BY herotypeidcount DESC
, winrate DESC
) x
GROUP
BY playerid

Related

mysqli query for multiple cases with coditions

Need to select data if condition first true then check user id exist, if condition not true then check condition two then check user id not exist
Technologies used: Codigniter - Mysqli
SELECT *
FROM `tablename`
WHERE `user_id` = '1'
OR `user_id` IN('62', '63', '58', '6', '50', '19', '2', '17', '7', '3')
AND `user_id` NOT IN('35')
OR (CASE WHEN except_friends = 1 THEN FIND_IN_SET('1', friends_list), FALSE END CASE WHEN selected_friends = 1 THEN NOT FIND_IN_SET('1', friends_list), FALSE END) ORDER BY `id` desc
Mysql syntax error
I think there is syntax error in your query. try this one
SELECT * FROM tablename
WHERE user_id = '1'
OR user_id IN('62', '63', '58', '6', '50', '19', '2', '17', '7', '3')
AND user_id NOT IN('35')
OR
(CASE
WHEN except_friends = 1
THEN NOT FIND_IN_SET('1', friends_list)
WHEN selected_friends = 1
THEN NOT FIND_IN_SET('1', friends_list)
END) ORDER BY id desc

Ties on Hall of Fame (group player, max level then max score for each game when month is...)

Need to list a Hall of Fame of best players, the database contains each single game player in different games.
The level has the priority, if the level are the same, check the highest score.
I've a database with user_id, level, score, game and data. Schema here:
CREATE TABLE IF NOT EXISTS `docs` (`user_id` int(6) unsigned NOT NULL,
`level` int(3) unsigned NOT NULL,`game` varchar(30) NOT NULL,
`score` int(5) unsigned NOT NULL,
`data` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `docs` (`user_id`, `level`, `game`, `score`,`data`) VALUES
('1', '7', 'pacman', '8452','2018-02-14 15:00:00'),
('1', '9', 'pacman', '9999','2018-02-10 16:30:00'),
('2', '8', 'pacman', '8500','2018-02-24 17:30:00'),
('1', '10', 'pacman', '9100','2018-02-15 18:30:00'),
('1', '10', 'pacman', '8800','2018-02-15 18:11:00'),
('1', '11', 'snake', '9600','2018-02-14 15:00:00'),
('1', '6', 'snake', '7020','2018-02-11 11:30:00'),
('2', '8', 'snake', '8500','2018-02-24 14:00:00'),
('2', '12', 'snake', '9200','2018-02-25 19:00:00'),
('2', '12', 'snake', '9800','2018-02-25 19:20:00'),
('1', '4', 'pacman', '2452','2018-03-11 15:00:00'),
('1', '6', 'pacman', '4999','2018-03-07 16:30:00'),
('2', '7', 'pacman', '5500','2018-03-02 17:30:00'),
('1', '7', 'pacman', '5100','2018-03-01 18:30:00'),
('1', '3', 'snake', '3600','2018-03-03 15:00:00'),
('1', '5', 'snake', '4220','2018-03-01 11:30:00'),
('2', '5', 'snake', '3900','2018-03-04 14:00:00'),
('2', '5', 'snake', '5200','2018-03-05 19:00:00');
i want retrieve the hall of fame for selected month and game,
for example if i choose pacman on march the result should be:
user level score
2 7 5500
1 7 5100
i tryed this how suggest in other similar topic
select d1.*
from docs d1
left outer join docs d2
on (d1.user_id = d2.user_id and d1.level < d2.level)
where d2.user_id is null
order by level desc;
but i've duplicate levels for same user, then i cant choose the game or the month.
here there is the SQL Fiddle
SELECT x.* FROM docs x
JOIN
(select user_id
, game
, MONTH(data) month
, MAX(score) score
from docs
where game = 'pacman'
and MONTH(data) = 3
group
by user_id
, game
, MONTH(data)
) y
ON y.user_id = x.user_id
AND y.game = x.game
AND y.month = MONTH(x.data)
AND y.score = x.score;
or something like that
after a long work, study and research this is the best solution for me:
SELECT user_id, level, score, game
FROM (
SELECT *,
#rn := IF(user_id = #g, #rn + 1, 1) rn,
#g := user_id
FROM (select #g := null, #rn := 0) x,
docs where game='pacman'
ORDER BY user_id, level desc, score desc, game
) X
WHERE rn = 1 order by level desc, score desc;
the explanation is in this topic Select one value from a group based on order from other columns

How to SUM result both SUMs SQL?

Using this query I try to sum result of both SUM function:
select
DAY(created_at) AS day,
SUM(if(status = '1', 1, 0)) AS result,
SUM(if(status = '2', 1, 0)) AS noresult,
SUM(result + noresult)
from `clients` where `doctor_id` = 2 and MONTH(created_at) = MONTH(CURRENT_TIMESTAMP) group by `day`
I try to do that in this line:
SUM(result + noresult)
Try this:
select
DAY(created_at) AS day,
SUM(if(status = '1', 1, 0)) AS result,
SUM(if(status = '2', 1, 0)) AS noresult,
SUM(if(status in ('1', '2'), 1, 0))
from `clients`
where `doctor_id` = 2 and MONTH(created_at) = MONTH(CURRENT_TIMESTAMP)
group by `day`
You can't use alias in select columns name you must repeat the code
select
DAY(created_at) AS day,
SUM(if(status = '1', 1, 0)) AS result,
SUM(if(status = '2', 1, 0)) AS noresult,
SUM(if(status = '1', 1, 0)) + SUM(if(status = '2', 1, 0)) AS all_result
from `clients` where `doctor_id` = 2 and MONTH(created_at) = MONTH(CURRENT_TIMESTAMP) group by `day`
you must repeat the code because the different SQL clause are processed in a specific order (first from then where then select and and group by .... etc.. ) so at the moment of the select parsing the alias are not available to the sql engine
As several other people have stated, you cannot use aliases in your select statement. However, to keep it cleaner, you could combine both conditions rather than summing both SUM fields.
select
DAY(created_at) AS day,
SUM(if(status = '1', 1, 0)) AS result,
SUM(if(status = '2', 1, 0)) AS noresult,
SUM(if(status = '1' OR status = '2', 1, 0)) AS newcolumn
from `clients` where `doctor_id` = 2 and MONTH(created_at) = MONTH(CURRENT_TIMESTAMP) group by `day`

"Complex" MySql request, getting some columns associated with MIN and MAX values in the same request

For example I have a dataset as below:
id, idPro, mesure2, mesure3, date
'6067', '1', '9.0', '29.3', '2013-11-17 12:48:39'
'6061', '1', '7.3', '30.3', '2013-11-17 15:20:12'
'6068', '2', '7.0', '29.3', '2013-11-17 12:48:39'
'6062', '2', '9.0', '25.3', '2013-11-17 15:20:12'
'6069', '3', '7.6', '29.0', '2013-11-17 12:48:39'
'6063', '3', '7.5', '27.0', '2013-11-17 15:20:12'
'6070', '4', '8.9', '29.1', '2013-11-17 12:48:39'
'6064', '4', '9.1', '23.1', '2013-11-17 15:20:12'
'6071', '5', '9.4', '28.9', '2013-11-17 12:48:39'
'6065', '5', '5.4', '30.9', '2013-11-17 15:20:12'
'6072', '6', '9.3', '28.9', '2013-11-17 12:48:39'
'6066', '6', '9.1', '24.9', '2013-11-17 15:20:12'
I want to output a table that have the minimal and maximal value of "mesure2" and "mesure3" grouped by idPro+Date and the time where those minimal and maximal value appeared.
So it should output something like:
idPro, mesureMin, dateMinMesure, mesureMax, timeMaxMesure, mesureType, date
'1', '7.3', '2013-11-17 12:48:39', '9.0', '2013-11-17 12:48:39', 'mesure2', '2013-11-17'
'1', '29.3', '2013-11-17 12:48:39', '30.3', '2013-11-17 15:20:12', 'mesure3', '2013-11-17'
'2', '7.0', '2013-11-17 12:48:39', '9.0', '2013-11-17 15:20:12', 'mesure2', '2013-11-17'
'2', '29.3', '2013-11-17 12:48:39', '25.3', '2013-11-17 15:20:12', 'mesure3', '2013-11-17'
...
Actually, I make a request that output the minimal and maximal value of each day for each mesure type (mesure2 or mesure3) and then I do other requests to get the date of each mesure. (I use php, and I build the request with
WHERE mesure2 = $valueReturnedByTheFirstRequest AND date LIKE '%2013-11-17%'
)
But this is a lot of sql requests, I'm looking for a way to output something similar in 1 query.
Thanks in advance
Try this query:
SELECT date,
idpro,
mesureType,
mesureMin,
( SELECT min(t.date) FROM table1 t
WHERE mesure2 = mesureMin
AND t.idpro = x1.idpro
AND x1.date = date( t.date )
) dateMinMesure ,
mesureMax,
( SELECT min(t.date) FROM table1 t
WHERE mesure2 = mesureMax
AND t.idpro = x1.idpro
AND x1.date = date( t.date )
) dateMaxMesure
FROM(
SELECT date(date) `date`,
idpro,
'measure2' mesureType,
min( mesure2 ) mesureMin,
max( mesure2 ) mesureMax
FROM table1
GROUP BY date(date), idpro, mesureType
) x1
UNION ALL
SELECT date,
idpro,
mesureType,
mesureMin,
( SELECT min(t.date) FROM table1 t
WHERE mesure3 = mesureMin
AND t.idpro = x1.idpro
AND x1.date = date( t.date )
) dateMinMesure ,
mesureMax,
( SELECT min(t.date) FROM table1 t
WHERE mesure3 = mesureMax
AND t.idpro = x1.idpro
AND x1.date = date( t.date )
) dateMaxMesure
FROM(
SELECT date(date) date,
idpro,
'measure3' mesureType,
min( mesure3 ) mesureMin,
max( mesure3 ) mesureMax
FROM table1
GROUP BY date(date), idpro, mesureType
) x1
ORDER BY date, idpro, mesuretype
demo --> http://www.sqlfiddle.com/#!2/daa51/14

MySQL SUM and CASE possible on DISTINCTROW?

I have this query :
SELECT
p.name,
COUNT(DISTINCT t.keyTask) AS totalTasksCount,
SUM(DISTINCT CASE WHEN t.keyPriority = 24 AND (t.keyState = 16 OR t.keyState = 17) THEN 1 ELSE 0 END) AS highPriorityTasksCount,
SUM(DISTINCT CASE WHEN t.keyState = 16 OR t.keyState = 17 THEN 1 ELSE 0 END) AS activesTasksCount,
SUM(DISTINCT t.estimatedDuration) AS estimatedDuration,
SUM(TIMESTAMPDIFF(SECOND,wp.start,wp.end)) * 1000 AS workedDuration
FROM projects_projects p
LEFT JOIN projects_tasks t
ON p.keyProject = t.keyProject
LEFT JOIN projects_workPeriods wp
ON wp.keyTask = t.keyTask
LEFT JOIN common_organizations o
ON o.keyOrganization = p.keyOrganization
LEFT JOIN common_users uc
ON uc.keyUser = p.keyUserCreator
LEFT JOIN common_users uu
ON uu.keyUser = p.keyUserUpdater
GROUP BY
p.keyProject
ORDER BY
highPriorityTasksCount DESC,
activesTasksCount DESC,
p.updated DESC,
p.name;
But the result fields highPriorityTasksCount and activesTasksCount returns 0 or 1 which is normal with this query. I was wondering if there is any way to make DISTINCTROW work on row and not on case result value for these fields without subquery ?
Current result :
p.name,
totalTasksCount,
highPriorityTasksCount,
activesTasksCount,
estimatedDuration,
workedDuration
'Project 1', '4', '1', '1', '14400000', '15300000'
'Project 2', '48', '1', '1', '84600000', '503100000'
'Project 3', '6', '1', '1', '108000000', NULL
'Project 4', '4', '1', '1', '25200000', '30600000'
'Project 5', '5', '1', '1', '226800000', '39600000'
'Project 6', '2', '0', '1', NULL, '10800000'
'Project 7', '9', '0', '1', NULL, '36900000'
Expected result :
'Project 1', '4', '1', '1', '14400000', '15300000'
'Project 2', '48','20', '2', '84600000', '503100000'
'Project 3', '6', '1', '1', '108000000', NULL
'Project 4', '4', '4', '2', '25200000', '30600000'
'Project 5', '5', '5', '1', '226800000', '39600000'
'Project 6', '2', '0', '1', NULL, '10800000'
'Project 7', '9', '0', '1', NULL, '36900000'
EDIT :
Modified query with subqueries since there is no way, help about optimization would be appreciated, this one is working :
SELECT
p.name,
COUNT(DISTINCT t.keyTask) AS totalTasksCount,
/* OLD SUM(DISTINCT CASE WHEN t.keyPriority = 24 AND (t.keyState = 16 OR t.keyState = 17) THEN 1 ELSE 0 END) AS highPriorityTasksCount, */
(
SELECT
SUM(CASE WHEN st.keyPriority = 24 AND (st.keyState = 16 OR st.keyState = 17) THEN 1 ELSE 0 END)
FROM projects_tasks st
WHERE
st.keyProject = p.keyProject
) AS highPriorityTasksCount,
/* OLD SUM(DISTINCT CASE WHEN t.keyState = 16 OR t.keyState = 17 THEN 1 ELSE 0 END) AS activesTasksCount, */
(
SELECT
SUM(CASE WHEN st.keyState = 16 OR st.keyState = 17 THEN 1 ELSE 0 END)
FROM projects_tasks st
WHERE
st.keyProject = p.keyProject
) AS activesTasksCount,
SUM(t.estimatedDuration) AS estimatedDuration,
SUM(TIMESTAMPDIFF(SECOND,wp.start,wp.end)) * 1000 AS workedDuration
FROM projects_projects p
LEFT JOIN projects_tasks t
ON p.keyProject = t.keyProject
LEFT JOIN projects_workPeriods wp
ON wp.keyTask = t.keyTask
LEFT JOIN common_organizations o
ON o.keyOrganization = p.keyOrganization
LEFT JOIN common_users uc
ON uc.keyUser = p.keyUserCreator
LEFT JOIN common_users uu
ON uu.keyUser = p.keyUserUpdater
GROUP BY
p.keyProject
ORDER BY
highPriorityTasksCount DESC,
activesTasksCount DESC,
p.updated DESC,
p.name;
without subquery.
No, you will have to use a subquery, or there's something I don't know about MySQL.