SELECT cm.commenter_id,
cm.comment,
m.id,
(
SELECT COUNT(*) AS r_count
FROM comments
GROUP BY comments.commenter_id
) AS count,
m.display_name
FROM comments cm
INNER JOIN members m
ON cm.commenter_id = m.id
From this query I want to get the display_name for the person with the highest count of comments. Any guidance is appreciated.
SELECT m.id, m.display_name, COUNT(*) totalComments
FROM comments cm
INNER JOIN members m
ON cm.commenter_id = m.id
GROUP BY m.id, m.display_name
HAVING COUNT(*) =
(
SELECT COUNT(*) totalCount
FROM Comments
GROUP BY commenter_id
ORDER BY totalCount DESC
LIMIT 1
)
SQLFiddle Demo
SQLFiddle Demo (with duplicates)
I think the simplest way is just to sort your query and take the first row:
SELECT cm.commenter_id,
cm.comment,
m.id,
(
SELECT COUNT(*) AS r_count
FROM comments
GROUP BY comments.commenter_id
) AS count,
m.display_name
FROM comments cm
INNER JOIN members m
ON cm.commenter_id = m.id
order by count desc
limit 1
Related
I am getting a calculation error in my code
SELECT dep_id,
dept_info.name AS dept_name,
count(dep_id) AS totalInovators,
count(user_id) AS totalIdea,
sum(POINT) AS totalpoint
FROM user_info
JOIN dept_info ON user_info.dep_id =dept_info.id
JOIN user_idea ON user_info.id=user_idea.user_id
GROUP BY dep_id
ORDER BY dep_id DESC
My output result:
Expected result:
With my table user_info :
My user_idea :
My dept_info :
Below the query that solve your problem:
select
user_info.dep_id,
dept_info.name,
count(*) as totalInovators,
sum(ideas_count) as totalIdea,
sum(point) as totalpoint
from
-- first we aggregate table user_idea
(select user_id, count(*) ideas_count from user_idea group by user_id) ideas
-- and after join rest of required tables
join user_info on ideas.user_id = user_info.id
join dept_info on dept_info.id = user_info.dep_id
group by user_info.dep_id, dept_info.name;
Working code here: SQLize.online
I suspect that you are joining along different dimensions. If so, a quick-and-easy solution uses count(distinct):
select d.id, d.name as dept_name, count(distinct u.id) as totalInovators,
count(*) as totalIdea, sum(i.point) as totalpoint
from user_info u join
dept_info d
on u.dep_id = d.id join
user_idea i
on u.id = i.user_id
group by d.id
order by d.id desc
I'm using the following query to sort, using Order by for 4 different ranks: id, avg_rating, total_sent & points
However, I'm having trouble with including all the members in the results. I'd like to include all members, including the ones that have 0: total_sent, total_received, points, avg_rating, votes
Please help me understand what I'm missing. Thank you.
SELECT m.id,
m.Name,
m.City,
m.Zip_Code,
m.url,
r.avg_rating,
r.votes,
froms.from_ct total_sent,
tos.to_ct total_received,
froms.from_ct - tos.to_ct `points`
FROM members m
JOIN (
SELECT id_rated,
avg(rating) avg_rating,
count(*) votes
FROM member_ratings
GROUP BY id_rated
) r ON r.id_rated = m.id
JOIN ( SELECT id_from, COUNT(*) AS from_ct FROM member_points GROUP BY 1
) AS froms ON froms.id_from = m.id
JOIN ( SELECT id_received, COUNT(*) AS to_ct FROM member_points GROUP BY 1
) AS tos ON tos.id_received = m.id
WHERE m.Account_Active = 'TRUE'
GROUP BY m.id,
m.Name
ORDER BY `avg_rating` DESC;
Use LEFT JOINs for all tables
SELECT m.id,
m.Name,
m.City,
m.Zip_Code,
m.url,
r.avg_rating,
r.votes,
froms.from_ct total_sent,
tos.to_ct total_received,
froms.from_ct - tos.to_ct `points`
FROM members m
LEFT JOIN (
SELECT id_rated,
avg(rating) avg_rating,
count(*) votes
FROM member_ratings
GROUP BY id_rated
) r ON r.id_rated = m.id
LEFT JOIN ( SELECT id_from, COUNT(*) AS from_ct FROM member_points GROUP BY 1
) AS froms ON froms.id_from = m.id
LEFT JOIN ( SELECT id_received, COUNT(*) AS to_ct FROM member_points GROUP BY 1
) AS tos ON tos.id_received = m.id
LEFT JOIN member_points mp ON mp.id_points = m.id
WHERE m.Account_Active = 'TRUE'
GROUP BY m.id,
m.Name
ORDER BY `avg_rating` DESC;
Table:
Table
Help me in. I'm suck in SubQuery. I wanna get the latest Name from every category group
Output:
Output
I already search some reference in google and still didn't understand. Hopefully this time I can understand
you can use join on max_date for each category
select * from
my_table m
inner join (
select category, max(date) max_date
from my_table
group by category) as t on m.date = t.max_date and m.category = t.category
Thx scaisEdge!!
I finally got it!
So this is my final query
select m.id, m.category, m.name, m.time
from my_table m
inner join (
select id, category, name, time
from my_table
order by id desc
) as t
on m.id = t.id
group by category
having:
SELECT *, COUNT(MakeModel) as `count`
FROM (`table_a`)
GROUP BY `MakeModel`
ORDER BY `count` DESC
Works fine for me (count = 2)
extend this query with a LEFT JOIN like this:
SELECT *, COUNT(distinct b.rating) as ratings
FROM (`table_a`)
LEFT JOIN `model_ratings` AS b ON `b`.`MakeModel` = `table_a`.`MakeModel`
GROUP BY `table_a`.`MakeModel`
Also works fine (ratings = 3),
but combine the 2 counts like this:
SELECT *, COUNT(table_a.MakeModel) as `count`, COUNT(distinct b.rating) as ratings
FROM (`table_a`)
LEFT JOIN `model_ratings` AS b ON `b`.`MakeModel` = `table_a`.`MakeModel`
GROUP BY `table_a`.`MakeModel`
ORDER BY `count` DESC
then (count= 6 ) and (ratings = 3).
So count becomes ('count' times 'ratings').
But I need (count= 2 ) and (ratings = 3)
What am I missing here?
Found out by myself
COUNT() and GROUP inside the LEFT JOIN first
SELECT *, COUNT(table_a.MakeModel) as `count`, b.rating as ratings
FROM (`table_a`)
LEFT JOIN (
SELECT MakeModel, COUNT(distinct rating)
FROM `model_ratings`
GROUP BY MakeModel
) AS b ON `b`.`MakeModel` = `table_a`.`MakeModel`
GROUP BY `table_a`.`MakeModel`
ORDER BY `count` DESC
Getting multiple records from table with subquery joins
SELECT
COUNT(*) AS total_count,
(SELECT
chat_box.user_id,
chat_box.message,
members.id,
members.display_name
FROM chat_box INNER JOIN members
ON chat_box.user_id = members.id
ORDER BY chat_id DESC LIMIT 1),
(SELECT COUNT(DISTINCT user_id) FROM chat_box) AS users_count
FROM chat_box
This is what I have so far, I want to get the members.display_name from the inner join where the chat_box.user_id = members.id as an output along aside the chat_box.message and save members.display_name and chat_box.message to a variable. Any help is appreciated.
It is not exactly clear what you are trying to do, but it seems like you could use something like this:
select u.user_id,
u.message,
u.id,
u.display_name,
cb1.total_count,
cb1.users_count
from
(
SELECT cb.user_id ,
cb.message,
m.id,
m.display_name
FROM chat_box cb
INNER JOIN members m
ON cb.user_id = m.id
) u
CROSS JOIN
(
select COUNT(*) AS total_count,
COUNT(DISTINCT user_id) AS users_count
FROM chat_box
) cb1