I'm trying to get the rank of a particular lap time of a specific track owned by a particular user.
There are multiple rows (laps) in this table for a specific user. So I'm trying to GROUP BY as seen in the subquery of FIND_IN_SET.
Right now MySQL (latest version) is complaining that my session_id,user_id,track_id,duration are not aggregated for the GROUP BY.
Which I don't understand why its complaining about this since the GROUP BY is in a subquery.
session_lap_times schema:
session_id, int
user_id, int
track_id, int
duration, decimal
This is what I've got so far.
SELECT
session_id
user_id,
track_id,
duration,
FIND_IN_SET( duration,
(SELECT GROUP_CONCAT( duration ORDER BY duration ASC ) FROM
(SELECT user_id,track_id,min(duration)
FROM session_lap_times GROUP BY user_id,track_id) AS aa WHERE track_id=s1.track_id)
) as ranking
FROM session_lap_times s1
WHERE user_id=1
It seems like its trying to enforce the group by rules on the parent queries as well.
For reference, this is the error I'm getting: http://imgur.com/a/ILufE
Any help is greatly appreciated.
If I'm not mistaken, the problem is here (broken out for clarity):
SELECT user_id,track_id,any_value(duration)
FROM session_lap_times
GROUP BY user_id
The query is probably barfing because track_id is in the select and not in the group by. That means the subselect doesn't stand on its own and makes the whole thing fail.
Try adding track_id to your group by and adjust from there.
You are grouping by user_id but you do not do any aggregation in select or having in the following sub-query
SELECT
user_id,any_value(track_id),any_value(duration)
FROM session_lap_times GROUP BY user_id
You are using GROUP_CONCAT in a wrong context in the following sub-query because you do not group any column in ranking temporary table.
(SELECT GROUP_CONCAT( duration ORDER BY duration ASC ) FROM
(SELECT user_id,track_id,any_value(duration)
FROM session_lap_times GROUP BY user_id,track_id) AS aa WHERE track_id=s1.track_id)
) as ranking
Related
I can do the following fine:
SELECT provider_id, count(*) cnt
FROM title
GROUP BY provider_id WITH ROLLUP
However, it doesn't seem to support ordering after a rollup:
SELECT provider_id, count(*) cnt
FROM title
GROUP BY provider_id WITH ROLLUP
ORDER BY count(*) DESC
Incorrect usage of CUBE/ROLLUP and ORDER BY
Two questions related to this:
Are you not allowed to use an order by in the same select statement as a with rollup? Or, is this something specific to mysql5.7 -- in that it doesn't support this feature -- but other DBs do dupport this?
Given this constraint, is the only way to do the sort by using a subselect?
SELECT * FROM (<rollup query>) _ ORDER BY cnt DESC
Related, but doesn't really answer the above question (as I already have the query above): https://stackoverflow.com/a/1768565/651174.
I have a query with a count with some group by, and I want to get the greater count. I can do with an order by and limit 1, but I have multiple results with the same count and then does not work for me.
How do I solve this problem?
With MySql 8+, you can use CTE to get the maximum count, and then retrieve all records with a count equal to the maximum count.
However, since CTE is not available in MySql 5.6, you'd need to use a sub-query to get the maximum count, and then write the main query which compares the count of each record to the maximum count retrieved in the subquery.
Here is a query I wrote. Maybe it's not the most efficient solution, but it gets the desired result.
SELECT
group_id, COUNT(record_id) c
FROM
table_name
GROUP BY group_id
HAVING c IN (
SELECT
MAX(sub_query.c)
FROM
(SELECT
group_id, COUNT(record_id) c
FROM
table_name
GROUP BY group_id) AS sub_query
)
If you are not going to do this using window functions, you can do:
SELECT group_id, COUNT(*) as cnt
FROM table_name
GROUP BY group_id
HAVING cnt = (SELECT COUNT(*)
FROM table_name
GROUP BY group_id
ORDER BY COUNT(*) DESC
LIMIT 1
) ;
I have two tables: ticket and ticketRules and i have a query like this:
SELECT * FROM (
SELECT * FROM ticketRules
ORDER BY
date DESC,
time DESC
) AS myTicketRules
GROUP BY ticketId
ORDER BY ticketId ASC
The first Order By (the one in the subquery) sorts data by date and time so the last ticketrule is always the first.
I group the results by ticketId so I only got the last ticketRule for each ticket.
Now I want to sort the results by ticket but if I do that the first result is also affected and tickets don't have the last ticketrule anymore but the one with the lowest id cause it's ordered by ticketId.
How can I sort only the visible records after I grouped them?
There are several issues with your query :
there should be only one ORDER BY clause (that should be placed in the outer query)
GROUP BY does not do what you think (ie it does not give access to the last ticket)
If you are trying to pull out the full latest record for each ticketId, ordered by ticketId, you can use a correlated subquery as follows :
SELECT
t.*
FROM
ticketrule t
WHERE
t.date = (
SELECT MAX(date)
FROM ticketrule
WHERE ticketId = t.ticketId
)
ORDER BY t.ticketId
you should not use a group by without aggregation function and for obtain the last value could use use an aggregation function as max( )
select ticketId, max(date)
from ticketRules
grouo by ticketId
Similar to this issue: MySQL 5.7 group by latest record
I'm not sure how to do this properly in 5.7. Also with possibility of 2nd sort column. Working query in 5.6 that I'm trying to replicate in 5.7:
SELECT id FROM test
GROUP BY category
ORDER BY sort1 DESC, sort2 DESC
id is not always the highest, so MAX(id) does not work.
Looking into the link above, the solution for single sort should be:
SELECT t1.*
FROM test t1
INNER JOIN (
SELECT category, max(sort) AS sort FROM test GROUP BY category
) t2 ON t2.category = t1.category AND t2.sort = t1.sort
But how will it work with 2 sorting?
You are using GROUP BY the wrong way.
Think of group by as a way to separate data row into different groups. Each group has multiple rows, based on the value of group by column.
Once you get those groups, selecting table columns (as in: select *) is like picking any row from that group randomly. This is not helpful nor useful.
Usually once we group records (or rows), we need to find meta information about those records. For example: get us the count of records in that group (as in: select count(*)), or the sum of values of a specific column in that group (as in: select sum(price)), or get the min, max or avg values.
So in a nutshell, when you use group by you should use on of the aggregation functions with it, otherwise it's not going to do you any good.
Why don't you have the ORDER BY at your outer query, instead?
SELECT *
FROM (
SELECT 100 AS id, 1 AS category, NULL AS sort
UNION
SELECT 200 AS id, 1 AS category, 2 AS sort
) dt
GROUP BY category
ORDER BY sort DESC;
It seems that what happened to the data when it was grouped, it took the first data while neglecting the ORDER BY DESC. On your first query, it ordered descending first then group by took the first record which is 200. And yes, this shouldn't be the way you should use GROUP BY. It is used in conjunction with aggregate functions.
when you select a column in a group by query that is not one of the columns you are grouping by, (ie, your id) you have no control over the value unless you use another aggregate function. If you want to sort, use MIN or MAX:
SELECT MAX(id), category, FROM `test2`
GROUP BY category; -- always returns 200
SELECT MIN(id), category, FROM `test2`
GROUP BY category; -- always returns 100
I need help returning a relevant result for this query. I have one table that I am hitting with three columns. trans_date, trans_amount and user_id
what I am trying to determine is this. For a given user_id when was the last trans_date and what was the trans_amount.
I'm having trouble returning the correct transaction_amount. Here is my code so far. It's returning the correct date but the amount is not right
select user_id, trans_date, trans_credit
from table
WHERE trans_credit =
(select max(trans_date) from inclick_account_act as f
where f.user_id = table.user_id);
Thanks in advance
If I understand you correctly you just want to get the most recent transaction for all users.
SELECT user_id, trans_date, trans_credit
FROM `table`
GROUP BY user_id
ORDER BY trans_date DESC;
How about something like
SELECT t.*
FROM table t INNER JOIN
(
SELECT user_id,
MAX(trans_date) max_trans_date
FROM table
GROUP BY user_id
) MaxDates ON t.user_id = MaxDates.max_trans_date