0

I have the following tables

A User table with column names (UserId,Name,email)

A Videos table with column names (UserId,VideoLink)

An Activity table with column names (UserId,Status) if status=0 likes,if status=1 disliked

I want to get the following result

Name, Total_Videos_posted_by_that_user, count(Likes), count(dislikes)

How can i Query them in a single Query?

1

1 Answer 1

3
select u.name, 
       videocount, 
       dislikes,
       likes
from user u
left join 
(
  select u.name, count(v.videolink) as videocount
  from user u
  inner join videos v on v.userid = u.userid
) x on x.name = u.name
left join 
(
  select u.name, 
         sum(a.status=1) as dislikes,
         sum(a.status=0) as likes
  from user u
  inner join activity a on a.userid = u.userid
) y on y.name = u.name

SQLFiddle demo

Sign up to request clarification or add additional context in comments.

6 Comments

Are you sure that sum(status=1) or count(a.Status=1)?
I am getting correct data if i use select u.name,count(v.videolink) as videocount from user u left join videos on u.userid=v.userid group by u.Name If i include another table as you mentioned activity, It gives incorrect result
I am sure it is sum(status=1) You want to count the dislikes only, right?
Ya i want to count both dislikes and likes. Ok even though i try to get dislikes count as you mentioned it returns incorrect data. Example user name John have 19 videos if i query till video count. If i include Dislikes query then it shows 142 as video count. and unfortunately dislikes count is also showing 142. But thats not the correct data it should be 6
It says Column 'Status' in field list is ambiguous
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.