Mysql SUM 0 when not in another table - mysql

I have 2 tables:
users:
id | name | club_id |
1 Bob 4
2 Jane 5
3 Alex 4
4 Paul 4
5 Tom 4
points:
user_id | club_id | amount(can vary)
1 4 10
1 2 10
2 5 10
3 4 10
3 4 10
4 4 10
3 2 10
3 4 10
I need (where users.club_id = 4 AND points.club_id = 4):
user_id | name | sum(amount)
3 Alex 30
1 Bob 10
4 Paul 10
5 Tom 0
Notice Tom is present in users but doesn't have any entries in points, so his sum should be 0. This is what throws me off in conjuction with grabbing a list from users.
Also would like this to be as efficient as possible (hence I added club_id = 4 both in users and points)

Try this:
SELECT
u.id,
u.name,
COALESCE(SUM(p.amount), 0) AS Totalpoints
FROM
(
SELECT *
FROM users
WHERE club_id = 4
) AS u
LEFT JOIN
(
SELECT *
FROM points
WHERE club_id = 4
) AS p ON p.user_id = u.id
GROUP BY u.id,
u.name;
See it in action here:
SQL Fiddle Demo

try this query
select u.id, u.name, sum(if (p.amount is null, 0, p.amount)) as totalPoint
from
user u
left join
(select * from point p where p.club_id = 4)p
on u.id = p.user_id
where u.club_id=4
group by u.id
SQL FIDDLE:

Related

Get user who had no activity in from last 12 months

I want to make SQL that will return users who not performed any points/redeem activity in last 12 months
user_data
userId email create_date
1 steve#gmail.com 2017-01-05 12:55:00
2 mark_nel#gmail.com 2019-05-15 12:13:00
3 les.born#gmail.com 2018-04-05 03:15:00
points_data
id user_id activity_id activity_points create_date
1 1 1 10 2017-01-05 11:09:00
2 2 1 15 2019-06-12 09:17:00
3 2 2 10 2019-08-05 02:55:00
4 3 1 10 2019-01-05 07:15:00
redeem_data
id user_id redeem_points create_date
1 2 20 2019-09-11 02:55:00
Result
email
steve#gmail.com
Use LEFT JOIN concept to include non matching row from table user_data and only select non matching row by removing matching rows using where id of both tables(points_data,redeem_data) is null.
SELECT u.email
FROM user_data u
LEFT JOIN points_data p ON u.userId = p.user_id AND p.create_date >= CURRENT_DATE - INTERVAL 1 YEAR
LEFT JOIN redeem_data r ON u.userId = r.user_id AND r.create_date >= CURRENT_DATE - INTERVAL 1 YEAR
WHERE p.id IS NULL AND r.id IS NULL;

JOIN MySQL tables based on condition

I have two MySQL tables users and task_reg
users
id name
1 user1
2 user2
3 user3
4 user4
5 user5
6 user6
7 user7
8 user8
9 user9
task_reg
top_id use_id payment
1 1 1
1 2 1
1 4 1
2 3 1
2 5 1
I have to display all the users and also show whether they have paid or not
here is my SQL command but it only shows the paid users
SELECT users.id, users.name, task_reg.payment
FROM task_reg
JOIN users ON users.id = task_reg.use_id
JOIN topics ON topics.id = task_reg.top_id
WHERE topics.id = 1
Result
id name payment
1 user1 1
2 user2 1
4 user4 1
Expected result
id name payment
1 user1 1
2 user2 1
3 user3 0
4 user4 1
5 user5 0
6 user6 0
7 user7 0
8 user8 0
9 user9 1
DB-Fiddle
Thanks.
Since you want all the users, it should be your first table in the join order.
Change your Inner join to Left Join.
Where condition on the topics.id should be a Join condition.
Use Coalesce() function to get value of payment done as 0 (because of no matching rows found).
Try:
SELECT users.id, users.name, COALESCE(task_reg.payment, 0) AS payment
FROM users
LEFT JOIN task_reg ON users.id = task_reg.use_id
LEFT JOIN topics ON topics.id = task_reg.top_id AND topics.id = 1

Retrieving the TOP 10 data with arithmetic operation

I want to retrieve the name of the user with the top 10 most voted post.
the formula in calculating the votes should be upvotes-downvotes. I also need to count the votes of that post.
Here's my
table user_info
id lname fname
1 abc1 abc1
2 abc2 abc2
3 abc3 abc3
4 abc4 abc4
5 abc5 abc5
6 abc6 abc6
7 abc7 abc7
table post
post_id post_message user_id
1 sup 1
2 good morning 2
3 hello 5
4 test 3
5 sample 4
table vote
vote_id user_id post_id vote_type
1 1 1 upvote
2 2 1 upvote
3 1 2 downvote
4 1 2 upvote
5 2 2 downvote
6 3 1 upvote
7 3 4 upvote
8 4 4 downvote
here's the query that I tried.
SELECT post_id, fname,lname,COUNT(CASE WHEN vote_type = 'upvote' then 1 ELSE NULL END) as "upvotes", COUNT(CASE WHEN vote_type = 'downvote' then 1 ELSE NULL END) as "downvotes"
FROM user_info
LEFT JOIN post ON
post.user_id = user_info.id
LEFT JOIN vote ON
post.user_id = user_info.id
ORDER BY 'upvotes'- 'downvotes' // is this possible?
LIMIT 10
My desired output is something like this.
post_id lname fname vote
1 abc1 abc1 3 // 3 upvotes
4 abc3 abc3 0 // 1upvote - 1 downvote
3 abc5 abc5 0 // no vote
5 abc4 abc4 0 // no vote
2 abc2 abc2 -1 // 1upvote - 2 downvotes
I don't know where I should put that column "vote" but it should be there in my query.
I created an sql fiddle for this
Fiddle
select p.post_id,u.fname,u.lname,
case when sum(upvotes)+sum(downvotes) is null then 0
else sum(upvotes)+sum(downvotes) end as vote
from post p left join
(
SELECT post_id,
CASE WHEN vote_type = 'upvote' then 1 ELSE 0 END as "upvotes",
CASE WHEN vote_type = 'downvote' then -1 ELSE 0 END as "downvotes"
FROM vote
) t
on p.post_id = t.post_id
left join user_info u on p.user_id = u.id
group by p.post_id
order by vote desc, sum(upvotes) desc, sum(downvotes) desc
limit 10;

MySQL returns incorrect data with use of join

I have join with a multiple tables. You can find the tables and MySQL query in this demo.
These are the tables:
users
id name
1 abc
2 xyz
3 pqr
friend
user_id friend_id
1 2
1 3
2 3
collection
id user_id friend_id amount
1 1 2 100
2 2 1 -100
3 2 3 200
4 3 2 -200
5 1 3 300
6 3 1 -300
bill
id use_id(bill_creator)
1 1
2 2
3 2
bill_person
id bill_id user_id
1 1 1
2 1 2
3 1 3
4 2 2
5 2 3
6 3 2
6 3 1
So far I have made this query:
SELECT mf.id
, mf.name
, c.amount AS amount,count(bp.user_id) AS no_common_bills
FROM (
SELECT fr.user_id AS user_id
, fr.friend_id AS friend_id
FROM friend fr
JOIN users fru
ON fru.id = fr.user_id
WHERE fru.id IN (1)
UNION
SELECT fl.friend_id AS user_id
, fl.user_id AS friend_id
FROM friend fl
JOIN users flf
ON flf.id = fl.friend_id
WHERE flf.id IN (1)
) f
JOIN users mf
ON mf.id = f.friend_id
LEFT
JOIN collection c
ON c.friend_id = mf.id
AND c.user_id = f.user_id
LEFT JOIN bill_person bp
ON bp.user_id=f.user_id AND c.friend_id = mf.id
GROUP BY mf.id
ORDER BY mf.id
and my output of this query is
id NAME AMOUNT NO_COMMON_BILLS
2 XYZ 100 2
3 PQR 300 2
but I want this result:
id NAME AMOUNT NO_COMMON_BILLS
2 XYZ 100 2
3 PQR 300 1
I am getting wrong output at NO_COMMON_BILLS. Other than that, all values are correct.
Third time's a charm...
http://sqlfiddle.com/#!2/338dd/12
SELECT u.name friend
, COUNT(bp2.user_id) no_common_bills
FROM users u
LEFT
JOIN
( SELECT user_id me, friend_id them FROM friend WHERE user_id = 1
UNION
SELECT friend_id,user_id FROM friend WHERE friend_id = 1
) x
ON x.them = u.id
LEFT
JOIN bill_person bp1
ON bp1.user_id = x.them
LEFT
JOIN bill_person bp2
ON bp2.bill_id = bp1.bill_id
AND bp2.user_id = x.me
WHERE u.id <> 1
GROUP
BY friend;
FRIEND NO_COMMON_BILLS
jkl 0
mno 0
pqr 1
xyz 2

MySQL : Get max values of groups

I have a table:
user_id | fav_song_genre | votes_as_fav_member
--------+----------------+--------------------
1 | hip hop | 3
2 | hip hop | 5
3 | rock | 8
4 | rock | 6
How do I get only results of user_id's with the highest votes_as_fav_member per group fav_song_genre:
Something like
SELECT *, MAX(votes_as_fav_member) as most_votes
FROM table
GROUP BY
fav_song_genre
I'm using that but it's not giving me the ID's of the members with most votes per genre.
This is not a problem of MySQL, rather a bit of logic problem with your approach.
Let's say we have the following:
user_id | fav_song_genre | votes_as_fav_member
--------+----------------+--------------------
1 | hip hop | 3
2 | hip hop | 5
3 | rock | 8
4 | rock | 6
5 | hip hop | 5
6 | rock | 8
Which ID should the query return? Should it return only one? or all that have the same amount of votes?
So, if you require only a single ID, what is the differentiation of a draw?
Lieven beat me to the SQL resolution by a few seconds, though.
Reasoning goes like
SELECT max vote for each genre
JOIN back with the original table to retrieve the additional columns for the records found.
SQL Statement
SELECT us.*
FROM UserSongs us
INNER JOIN (
SELECT fav_song_genre
, MAX(votes_as_fav_member) AS votes_as_fav_member
FROM UserSongs
GROUP BY
fav_song_genre
) usm ON usm.fav_song_genre = us.fav_song_genre
AND usm.votes_as_fav_member = us.votes_as_fav_member
Edit
How can I make sure the person with the lower ID is returned
SELECT MIN(us.user_id) as user_id
, us.fav_song_genre
, us.votes_as_fav_member
FROM UserSongs us
INNER JOIN (
SELECT fav_song_genre
, MAX(votes_as_fav_member) AS votes_as_fav_member
FROM UserSongs
GROUP BY
fav_song_genre
) usm ON usm.fav_song_genre = us.fav_song_genre
AND usm.votes_as_fav_member = us.votes_as_fav_member
GROUP BY
us.fav_song_genre
, votes_as_fav_member
Not sure if that's what you are asking:
SELECT g.fav_song_genre
, t.user_id
, g.most_votes
FROM yourTable t
JOIN
( SELECT fav_song_genre
, MAX(votes_as_fav_member) as most_votes
FROM yourTable
GROUP BY fav_song_genre
) AS g
ON t.fav_song_genre = g.fav_song_genre
AND t.votes_as_fav_member= g.most_votes
;
I created the table and tested, and I think this will also work
Table Data:
user_id fav_song_genre votes_as_fav_member
1 hip_hop 3
2 hip_hop 5
3 rock 8
4 rock 6
5 blues 20
6 indie 18
7 rock 35
8 country 33
9 hip_hop 35
10 indie 5
11 blues 7
12 hip_hop 59
13 indie 187
14 classic 45
15 country 61
16 hip_hop 243
Query:
select t.user_id, t.fav_song_genre, t.votes_as_fav_member
from (
select user_id, max(votes_as_fav_member) as max_votes, fav_song_genre
from table1 group by fav_song_genre
)
as x inner join table1 as t on t.votes_as_fav_member = x.max_votes and t.fav_song_genre = x.fav_song_genre;
Results:
user_id fav_song_genre votes_as_fav_member
5 blues 20
7 rock 35
13 indie 187
14 classic 45
15 country 61
16 hip_hop 243