skip to Main Content

I want to see which user has received the most highfives using a SQL query. My table looks like following, id | uid | ip. Now, I want to count the amount of rows a uid has, but it has to be unique with the ip. So nobody can give multiple highfives to a person.

I searched around online, and I couldn’t find anything about this. If anyone could help me with this, I would be grateful.

2

Answers


  1. you can try like below

    select ip, count(distinct uid) from table t
    group by ip
    
    Login or Signup to reply.
  2. SELECT uid,COUNT(ip)NoOfVotes FROM
    (SELECT uid,ip,Serial=ROW_NUMBER() OVER(PARTITION BY ip,uid ORDER BY uid)  FROM 
    dbo.tbl_user)A
    WHERE Serial=1
    GROUP BY uid
    

    I think this will give you perfect vote counting. Using Row Number actively remove duplicates from same ip,same uid. Voting to multiple uid from same ip is allowed in this query.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search