SQL count and sum some row - mysql

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

Related

(MySQL) Get last data in every group (SubQuery)

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

MySQL subquery fetch only latest value from JOIN table

I have the following MySQL query. The user_extra table can return multiple values for a given user_id. I would like only the latest a.session_date value from user_extra.
SELECT DISTINCT (user_id) user_id, a.value
FROM user_reg ap
JOIN user_extra a USING (user_id)
WHERE ap.session_date BETWEEN '2017-01-01' AND '2017-06-10' AND a.session_date<='2017-01-01'
Any ideas how I can do this?
select ap.user_id, ap1.value
from user_reg ap
inner join (
select user_id, max(session_date) as MaxDate, value
from user_extra
group by user_id
) ap1 on ap.user_id = ap1.user_id and ap.date = ap1.MaxDate
It will give maximum session date for single user id.
One approach is to join to a subquery which finds the latest records. This is probably the best way to go here as MySQL does not have analytic functions available. In this case, we can add one more join to your query to restrict the user_extra table to having the record corresponding to the most recent session for each user.
SELECT
ur.user_id,
ue1.value
FROM user_reg ur
INNER JOIN user_extra ue1
ON ur.user_id = ue1.user_id AND
ue1.session_date <= '2017-01-01'
INNER JOIN
(
SELECT user_id, MAX(session_date) AS max_session_date
FROM user_extra
GROUP BY user_id
) ue2
ON ue1.user_id = ue2.user_id AND
ue1.session_date = ue2.max_session_date
WHERE
ur.session_date BETWEEN '2017-01-01' AND '2017-06-10';
If you would want to get a list of all the values of a user use:
SELECT A.user_id, GROUP_CONCAT(B.value) LIST_OF_VALUES FROM
(SELECT user_id, value FROM user_reg
WHERE session_date BETWEEN '2017-01-01' AND '2017-06-10'
GROUP BY user_id, value) A INNER JOIN
(SELECT user_id, MAX(session_date) FROM user_extra
WHERE session_date<='2017-01-01' GROUP BY user_id) B
ON A.user_id=B.user_id
GROUP BY A.user_id;
But if you would want to get the total values of a user use:
SELECT A.user_id, SUM(B.value) SUM_OF_VALUES FROM
(SELECT user_id, value FROM user_reg
WHERE session_date BETWEEN '2017-01-01' AND '2017-06-10'
GROUP BY user_id, value) A INNER JOIN
(SELECT user_id, MAX(session_date) FROM user_extra
WHERE session_date<='2017-01-01' GROUP BY user_id) B
ON A.user_id=B.user_id
GROUP BY A.user_id;

Sql: Using alias in nested queries

select
user.name,
(select department.name
from profile, degree, department
where user.profile_id=profile.id
and profile.degree_id=degree.id
and degree.department_id=department.id) DEPT_NAME
from user, query
where user.id=query.user_id as USER_NAME
order by user.name;
This is the query I have written.In this for department.name the column alias changed to DEPT_NAME but for user.name it doesn't changes, it throws error.. Please help...
Move your alias to fields clause:
select
`user`.name as USER_NAME,
(select department.name
from profile, degree, department
where `user`.profile_id=profile.id
and profile.degree_id=degree.id
and degree.department_id=department.id LIMIT 1) DEPT_NAME
from `user`, query
where `user`.id=query.user_id
order by `user`.name;
Also your query is too heavy, try to change it and check difference:
select u.name USER_NAME, dp.name DEPT_NAME
from `user` u
inner join query q on u.id = q.user_id
left join profile p on p.id = u.profile_id
left join degree d on d.id = p.degree_id
left join department dp on dp.id = d.department_id
order by u.name;
try it-
SELECT usr.name User_Name,(SELECT dt.name FROM profile pf,degree dg,department dt WHERE usr.profile_id=pf.id AND pf.degree_id=dg.id AND dg.department_id=dt.id limit 1)DEPT_NAME FROM USER usr, QUERY qry WHERE usr.id=qry.user_id ORDER BY usr.name;
Even you can use below query which can be fast-
SELECT user.name user_name,department.name AS DEPT_NAME
FROM profile,degree,department,`user`,`query`
WHERE user.profile_id=profile.id AND profile.degree_id=degree.id AND degree.department_id=department.id AND user.id=query.user_id ORDER BY user.name;

Report Query for multiple subqueries

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

Query for multiple count values

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