inner join + count + group by - mysql

I'm have trouble counting/grouping the results of an inner join
I have two tables
results_dump: Which has two columns: email and result (the result value can be either open or bounce)
all_data: Which has three columns: email, full_name and address
The first goal is to query the result_dump table and count and group the number of times the result is "open" for a specific email.
This query works great:
SELECT `email`, COUNT(*) AS count
FROM `result_dump`
WHERE `date` = "open"
GROUP BY `email`
HAVING COUNT(*) > 3
ORDER BY count DESC
The second goal it to take those results (anyone who "open" more then 3 time) and pull in the 'full_name' and 'address' so I will have details on who opened an email 3+ times.
I have this query and it works as far as getting the data together - But I can't figure out how to get the COUNT, HAVING and ORDER to work with the INNER JOIN?
SELECT *
FROM all_data
INNER JOIN result_dump ON
all_data.email = result_dump.email
where `result` = "open"

SELECT email,name,count(*)
FROM all_data
INNER JOIN result_dump ON
all_data.email = result_dump.email
where `result` = "open"
group by result_dump.email
having count(*)>3
ORDER by count DESC
Nothing wrong with this one I think.

Try with following query:
SELECT * FROM all_data AS a
INNER JOIN
(SELECT * FROM result_dump where email IN
(SELECT `email`
FROM `result_dump`
WHERE `date` = "open"
GROUP BY `email`
HAVING count(email) >3
ORDER BY count(email) DESC)) AS b
ON a.email = b.email
WHERE b.`result` = "open"

This is Works Fine...! Try to this..
SELECT title.title
,count(*)
,title.production_year
,title.id as movie_id
,title.flag as language
,movie_info.info
FROM title INNER JOIN movie_info ON title.id=movie_info.movie_id;

Related

MySQL, joining a table where you require the max value from the second table

I have the below query:
SELECT users_service.id, name
FROM users_service
LEFT JOIN
(SELECT * FROM activity)
activity ON (users_service.id = activity.user_service_id)
WHERE admin_id = 1
However, this returns as many results from the activity table as exist, ie multiple activity results for each admin_id entry.
I desire to return only the latest row from the activity table for each admin_id.
This could be entry_date or id.
I tried using distinct & max and limit 1, but these all produced strange behavior.
Use ROW_NUMBER():
SELECT us.id, a.name
FROM users_service us LEFT JOIN
(SELECT a.*,
ROW_NUMBER() OVER (PARTITION BY a.user_service id ORDER BY ? DESC) as seqnum
FROM activity
) a
ON u.id = a.user_service_id AND seqnum = 1
WHERE u.admin_id = 1;
The ? is for the column that specifies the "most recent", which your question doesn't clarify.
You did not specify the column by which you determine the most recent activity. I call it datetime_col in the solution below:
SELECT users_service.id
, name
FROM users_service usv
LEFT
JOIN activity act
on act.users_service.id = usv.user_service_id
and act.datetime_col = (select max(datetime_col)
from activity act_
WHERE act_.user_service_id= act.user_service_id)

SQL query that limits the results to one when using count inside count

I am trying to select the count of likes on a specific project. The idea i came up with is
CAST(count(uploads.ID in (SELECT uploadID from votes)) as decimal) as numberoflikes
this works but the query then only returns one thing.
Entire query
SELECT DISTINCT users.NAME AS username
,users.ID AS userID
,subjects.NAME AS subjectname
,uploads.TIME
,uploads.description
,uploads.NAME
,uploads.ID
,CASE
WHEN uploads.ID IN (
SELECT uploadID
FROM votes
WHERE userID = 2
)
THEN CAST(1 AS DECIMAL)
ELSE CAST(0 AS DECIMAL)
END AS liked
,CASE
WHEN uploads.ID IN (
SELECT uploadID
FROM bookmarks
WHERE userID = 2
)
THEN CAST(1 AS DECIMAL)
ELSE CAST(0 AS DECIMAL)
END AS bookmarked
,CAST(count(uploads.ID IN (
SELECT uploadID
FROM votes
)) AS DECIMAL) AS numberoflikes
FROM uploads
INNER JOIN subjects ON (subjects.ID = uploads.subjectID)
INNER JOIN users ON (users.ID = uploads.userID)
INNER JOIN uploadGrades ON (uploads.ID = uploadGrades.uploadID)
INNER JOIN grades ON (grades.ID = uploadGrades.gradeID)
WHERE uploads.active = 1
AND subjects.ID IN (
SELECT subjectID
FROM userSubjects
INNER JOIN users ON (users.ID = userSubjects.userID)
WHERE userSubjects.userID = 2
)
AND grades.ID IN (
SELECT userGrades.gradeID
FROM uploadGrades
INNER JOIN userGrades ON (uploadGrades.gradeID = userGrades.gradeID)
WHERE userGrades.userID = 2
)
ORDER BY uploads.trueRating DESC;
Lets try a reduce version of your query, That is the base to get better answers
I reduce the initial query to user and upload to start. Also remove the fields you already know how to calculate.
.
SELECT DISTINCT users.NAME AS username
,users.ID AS userID
,uploads.NAME
,uploads.ID
,CAST(count(uploads.ID IN (
SELECT uploadID
FROM votes
)) AS DECIMAL) AS numberoflikes
FROM uploads
INNER JOIN users ON (users.ID = uploads.userID)
WHERE uploads.active = 1
ORDER BY uploads.trueRating DESC;
Then add votes with LEFT JOIN to replace the SELECT in the COUNT that way if not match you will get NULL and as I say in my comment COUNT doesnt count NULL's
.
SELECT DISTINCT users.NAME AS username
,users.ID AS userID
,uploads.NAME
,uploads.ID
,CAST(count(votes.uploadID)) AS DECIMAL) AS numberoflikes
FROM uploads
INNER JOIN users ON (users.ID = uploads.userID)
LEFT JOIN votes ON (uploads.ID = votes.uploadID)
WHERE uploads.active = 1
ORDER BY uploads.trueRating DESC;
Try something like this...
SELECT users.name as username, users.ID as userID, subjects.name as subjectname,
uploads.time, uploads.description, uploads.name, uploads.ID,
count(userVotes.userId), count(bookmarksMade.userId),
FROM uploads
join subjects on(subjects.ID = uploads.subjectID)
join users on(users.ID = uploads.userID)
join uploadGrades on(uploads.ID = uploadGrades.uploadID)
join grades on(grades.ID = uploadGrades.gradeID)
left join (select userId, uploadId from votes where userId = 2) as userVotes on uploads.id = userVotes.uploadId
left join (select userId, uploadId from bookmarks where userId = 2) as bookmarksMade on uploads.id = bookmarksMade.uploadId
join userSubjects on subjects.id = userSubjects.subjectID
WHERE uploads.active = 1 AND
userSubjects.userID = 2
ORDER BY uploads.trueRating DESC;
But, I am leaving out the userGrades thing, because you are doing a funky join there that I don't really understand (joining two tables on something that looks like it is not the whole primary key on either table).
Anyway, you really need to go to something more like this or what Oropeza suggests in his answer. Get more direct about what you want. This query looks like a monster that has been growing and getting things added in with "IN" clauses, as you needed them. Time to go back to the drawing board and think about what you want and how to get at it directly.
count(uploads.ID in (SELECT uploadID from votes)) as numberoflikes
group by uploads.Id ORDER BY uploads.trueRating DESC
I managed to do it like this. If i added the group by then it split the numberoflikes into rows and returned more then one row. Thanks for the help!

SQL subquery logic confusion

I'm writing an SQL query but I'm stuck at a point and can't figure out how to solve this issue. First have a look at the query below:
SELECT user_id, COUNT(*) as count
FROM hwc_attend
WHERE at_id IN
(SELECT evdet_id
FROM eve_detail
WHERE evdet_id IN (SELECT at_id FROM hwc_attend WHERE attendstate=1 )
AND location <> ''
AND evdet_id > 999
AND location NOT IN (SELECT ASIN FROM pReviews )
)
GROUP BY user_id
This query is working fine but giving lesser results than required because the part SELECT ASIN FROM pReviews should be like SELECT ASIN FROM pReviews where cID={place current value of "location" field from table eve_detail here.
For a better understanding, here's the errornous query:
SELECT user_id, COUNT(*) as count
FROM hwc_attend
WHERE at_id IN
(SELECT evdet_id **, location**
FROM eve_detail
WHERE evdet_id IN (SELECT at_id FROM hwc_attend WHERE attendstate=1)
AND location <> ''
AND evdet_id > 999
AND location NOT IN (SELECT ASIN FROM pReviews where cID=**location**)
)
GROUP BY user_id
It's hard to explain.. In short, I have to remove "location" values from the result fetched from table "eve_detail" that also exist in table "pReviews" in column cID.
Additionally, it would be nice if someone can covert it into joins. I would need both queries for learning.
Translating it to a join would use something like this. Using a LEFT OUTER JOIN and checking for NULL instead of NOT IN. I am assuming that hwc_attend has a unique column called id which is used in the count to get distinct rows.
SELECT ha1.user_id, COUNT(DISTINCT ha1.id) as count
FROM hwc_attend ha1
INNER JOIN eve_detail ed ON ha1.at_id = ed.evdet_id
INNER JOIN hwc_attend ha2 ON ed.evdet_id = ha2.at_id
LEFT OUTER JOIN pReviews pr ON ed.location = pr.ASIN AND cID = **location**
WHERE ha2.attendstate = 1
AND ed.location <> ''
AND ed.evdet_id > 999
AND pr.ASIN IS NULL
GROUP BY ha1.user_id
Change
AND location NOT IN (SELECT ASIN FROM pReviews )
To
AND NOT EXISTS (SELECT ASIN FROM pReviews WHERE eve_detail.location = ASIN.cID )

LEFT JOIN SUM with WHERE clause

The following query always outputs SUM for all rows instead of per userid. Not sure where else to look. Please help.
SELECT * FROM assignments
LEFT JOIN (
SELECT SUM(timeworked) AS totaltimeworked
FROM time_entries
) assignments ON (userid = assignments.userid AND ticketid = ?)
WHERE ticketid = ?
ORDER BY assigned,scheduled
If you want to keep the SELECT *, you would have to add a group by clause in the subquery. Something like this
SELECT * FROM assignments
LEFT JOIN (
SELECT SUM(timeworked) AS totaltimeworked
FROM time_entries
GROUP BY userid
) time_entriesSummed ON time_entriesSummed.userid = assignments.userid
WHERE ticketid = ?
ORDER BY assigned,scheduled
But a better way would be to change the SELECT * to instead select the fields you want a add a group by clause directly. Something like this
SELECT
assignments.id,
assignments.assigned,
assignments.scheduled,
SUM(time_entries.timeworked) AS totalTimeworked
FROM assignments
LEFT JOIN time_entries
ON time_entries.userid = assignments.userid
GROUP BY assignments.id, assignments.assigned, assignments.scheduled
Edit 1
Included table names in query 2 as mentioned in chameera's comment below

using left join and where in another field of joined table

I have two tables users and work_orders.
Now I need to get work_orders results by filtering from users table field.
My following query did not return result.
SELECT `work_orders`.`id` as id, `work_orders`.`type` as type
, `work_orders`.`title` as title, `work_orders`.`status` as status
, `work_orders`.`publish_date` as publish_date
, `work_orders`.`priority` as priority, `work_orders`.`assigned_to` as assigned_to
, `work_orders`.`client_id` as client_id, `work_orders`.`due_date` as due_date
FROM (`work_orders`)
LEFT JOIN `users`
ON `users`.`id` = `work_orders`.`assigned_to`
WHERE `work_orders`.`status` != 'closed'
AND users.manager_id = '143'
ORDER BY `work_orders`.`id` DESC
LIMIT 30
Is your WHERE clause filtering out all results?
Also, if you want to display work_orders that only pertain to certain users, change your LEFT JOIN to an INNER JOIN, or use EXISTS.
Try this...
SELECT field1, field2, ...
FROM work_orders
WHERE EXISTS (
SELECT 1
FROM users
WHERE users.id = work_orders.assigned_to
AND manager_id='143'
)