I know that this question is asked also before but i tried all of them but no one works for me because my query is a little bit different because it has sum function in query that needs to get sum and in base of sum to get the rank of the user.
So my question is how to find the rank for single user my table is this:
currently i am trying with this query but this gives me all users.
SELECT u.user_name as userName
, sum(taken_quiz_points) as totalPoints
FROM taken_quiz as q
, users_app as u
WHERE q.taken_quiz_user_id = u.user_id
GROUP
BY taken_quiz_user_id
ORDER
BY totalPoints DESC
First of all, you should use INNER JOIN when you query data from multiple tables.
Also, I don't think it's possible to ORDER BY an alias, you have to repeat the operation of the alias.
If you want to have the rank of a single user you have to add a WHERE condition, like WHERE u.name = "toto" or something.
You should end up with this :
SELECT
u.user_name as userName ,
sum(q.taken_quiz_points) as totalPoints
FROM taken_quiz q
INNER JOIN users_app u ON q.taken_quiz_user_id = u.user_id
WHERE u.name = "toto"
GROUP BY q.taken_quiz_user_id
ORDER BY sum(q.taken_quiz_points) DESC
So you need the whole dataset in order to know the ranking of the person. It's a bit ugly, but you can nest to get the ranking, and then select from the ranked dataset the person you are interested in:-
Select userName, user_rank from
(Select userName,totalpoints ,dense_rank() OVER (Order by totalpoints desc) as user_rank
from (
SELECT u.user_name as userName ,sum(taken_quiz_points) as totalPoints
FROM taken_quiz as q, users_app as u
WHERE q.taken_quiz_user_id=u.user_id GROUP BY user_name,
taken_quiz_user_id) aa
) bb
where userName ='bob'
Related
I allow myself to write a thread regarding a query I'm trying to make for hours now. I'm trying to get the name of the friend (friend.first_name) who refuses the most proposed dates for events.
To do this I'm counting the number of proposed date and ORDER BY ASC.
SELECT COUNT(*) 'NbrProposedDate', f.FIRST_NAME,
f.LAST_NAME, f.FRIEND_ID
FROM PROPOSES
NATURAL JOIN FRIEND f
GROUP BY f.FRIEND_ID
ORDER BY NbrProposedDate ASC
LIMIT 1;
However, this does not take into account TIES.
What I'm looking for is the following result :
Furthermore, I've seen something with FETCH FIRST 1 ROW WITH TIES, however it does not seem to work with MySQL (getting SQL syntax issue).
Finally, I've had found an alternative using a function :
-- Find Minimum Count
SELECT MIN(cnt) INTO #min FROM (SELECT COUNT(*) cnt FROM PROPOSES NATURAL JOIN FRIEND f GROUP BY f.FRIEND_ID) t;
-- Show Friends with minimum count
DROP VIEW IF EXISTS troublemaker;
CREATE VIEW troublemaker AS
SELECT FIRST_NAME, LAST_NAME
FROM PROPOSES p
JOIN (SELECT FRIEND.FRIEND_ID
FROM PROPOSES
NATURAL JOIN FRIEND
GROUP BY FRIEND.FRIEND_ID
HAVING COUNT(*) = #min) t
ON p.FRIEND_ID = t.FRIEND_ID
JOIN FRIEND ON t.FRIEND_ID = FRIEND.FRIEND_ID
ORDER BY p.FRIEND_ID ASC;
However, the issue is that, I need to put this into a view, but "View's SELECT contains a variable or parameter".
Therefore, I'm looking for another alternative or a solution to fix this issue.
P.S. : here is an MLD :
View is not required, a query result could be used as subquery
SELECT
COUNT(*) NbrProposedDate,
MAX(f.FIRST_NAME) FIRST_NAME,
MAX(f.LAST_NAME) LAST_NAME,
f.FRIEND_ID
FROM PROPOSES
NATURAL JOIN FRIEND f
GROUP BY f.FRIEND_ID
HAVING COUNT(*) = (
SELECT COUNT(*)
FROM PROPOSES
NATURAL JOIN FRIEND f
GROUP BY f.FRIEND_ID
ORDER BY COUNT(*)
LIMIT 1
)
On MySQL 8+, it can also use RANK()
WITH ranks AS (
SELECT
COUNT(*) NbrProposedDate,
MAX(f.FIRST_NAME) FIRST_NAME,
MAX(f.LAST_NAME) LAST_NAME,
f.FRIEND_ID,
RANK() OVER (ORDER BY COUNT(*) ASC) rk
FROM PROPOSES
NATURAL JOIN FRIEND f
GROUP BY f.FRIEND_ID
)
SELECT * FROM ranks WHERE rk = 1
I'm currently outputting all of my members by adding the MySQL clause ORDER BY id DESC, but I feel that doesn't reward people that are active on my service.
I thought about judging the order by the amount of entries in another table they have under their ID.
Essentially, I'm asking if it's possible to order columns in a MAIN table counting the amount of rows where the users ID is in the column of the row.
Something pseudo to this
SELECT user_id,name,etc FROM users ORDER BY (
COUNT(SELECT FROM users_interactions WHERE user_id = user_id) *******
) ASC
In the end of the COUNT statement, the user_id = user_id was just a guess.
You are almost there - what you need to do is to put COUNT inside SELECT:
SELECT user_id,name,etc FROM users u ORDER BY (
SELECT COUNT(*)
FROM users_interactions i
WHERE i.user_id = u.user_id
) ASC
You could also do it using a JOIN, like this:
SELECT u.user_id, u.name, u.etc
FROM users u
LEFT OUTER JOIN users_interactions i ON i.user_id = u.user_id
GROUP BY u.user_id, u.name, u.etc
ORDER BY COUNT(*) ASC
I have 2 tables: user and review, a one-to-many relationship.
When I execute the following query:
SELECT
user_id,
count(*) totalReviews,
USER . NAME
FROM
review,
USER
WHERE
USER .id = review.user_id
GROUP BY
user_id
I get:
1 2 marius
2 2 daniela
3 1 alin
What I want to do now is to display first 2 users because they have given the most reviews(2).
I tried adding having, if I hardcode having totalReviews=2 it works, but if I write having total = max(total) I get 0 results, while if I'm trying with,
SELECT
*
FROM
(
SELECT
user_id,
count(*) total,
USER . NAME
FROM
review,
USER
WHERE
USER .id = review.user_id
GROUP BY
user_id
) A
WHERE
total = (SELECT max(total) FROM A) `
I get an error (table A doesn't exist)
You would do this with ORDER BY and LIMIT:
SELECT u.id, count(*) as totalReviews, u.name
FROM review r JOIN
user u
ON u.id = r.user_id
GROUP BY u.id, u.name
ORDER BY totalReviews DESC
LIMIT 2;
Notes:
Never use commas in the FROM clause. Always use proper, explicit JOIN syntax.
Table aliases make the query easier to write and read.
EDIT:
If occurs to me that you want all users with the maximum number of reviews, not exactly 2. Here is one method:
SELECT u.id, COUNT(*) as totalReviews, u.name
FROM review r JOIN
user u
ON u.id = r.user_id
GROUP BY u.id, u.name
HAVING totalReviews = (SELECT COUNT(*)
FROM review r2
GROUP BY r2.user_id
ORDER BY COUNT(*) DESC
LIMIT 1
);
Note that the subquery in the HAVING clause is simpler than the outer query. There is no need to bring in the user name.
I am saving the history of Facebook likes for a page, identified by user_id.
Now from this table, I need to get a set representing the user_id's and their latest number of likes, based on the most recent timestamp.
I started off with this:
SELECT *
FROM facebook_log
GROUP BY user_id
ORDER BY timestamp DESC;
But that does not do what I want because it returns the first records with the lowest timestamps.
I read something online about GROUP returning the very first records from the table.
I also understood something about JOIN the table with itself, but that doesn't work either, or I did something wrong.
If you just need the user_id and the timestamp, you can just do
select f.user_id, max(f.timestamp)
from facebook_log
group by user_id;
if you need all the data from the table, you can do
select f.*
from facebook_log f
inner join (select max(timestamp) mt, user_id
from facebook_log
group by user_id) m
on m.user_id = f.user_id and m.mt = f.timestamp
You can also get the latest number of likes by using this MySQL trick:
select f.user_id, max(f.timestamp),
substring_index(group_concat(f.numlikes order by f.timestamp desc), ',', 1) as LatestLikes
from facebook_log f
group by f.user_id;
I have this query:
SELECT id_user, COUNT(*) as count
FROM posts
GROUP BY id_user
ORDER BY COUNT(*) DESC
which gives me the id_user ordered by occurrences, and the number of each occurrence.
Can I get, in the same request, the LAST post from each 'id_user'? i.e. I want to select the last 'post' too, but when I do
SELECT id_user, post, COUNT(*) as count
Tthe value in 'post' isn't the last one (nor the first one; actually I don't know how groups are ordered). Should I run another query?
I believe u can accomplish this by adding max(post_id) last_post to your select.
This ought to do it in one query:
SELECT
p.id_user,
ap.post AS last_post,
COUNT(*) as count
FROM
posts p
JOIN posts ap on (
p.id_user = ap.id_user
AND ap.post_id = (
SELECT MAX(post_id) FROM posts ip WHERE p.id_user = ip.id_user
)
GROUP BY
p.id_user,
ap.post
ORDER BY
COUNT(*) DESC