SQL query to get all unique rows with uid - sql

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.

you can try like below
select ip, count(distinct uid) from table t
group by ip

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.

Related

Finding entry count for each unique id in SQL

I have an SQL database that shows the amount of times a person submits an entry. I want to count how many time each person who owns a unique id makes a claim. Each unique i.d can make mulpile entries into the table and I want to find out how many everyone has made.
I also want to filter the people based on the amount of entries they have made. For example 10.
select id, entry, COUNT(ID) from Table where COUNT(entry) <=10 GROUP BY ID
This is my thinking so far but I havent had much success. If anyone could help I would greatly appreciate it.
This is all you need
select id, COUNT(*) cnt
from Table
GROUP BY ID
having COUNT(*) <= 10
order by 2 desc -- optional descending count
Optionally, add Order by 2. desc or asc

SQL query for selecting all entries from the user with the most entries

I am making a suggestions application in relation to school.
I have a table with users and a table with suggestions that share the key userId.
Every suggestions has the unique key of sugId and a duplicateable userId.
How can I select all the entries from the user with the most entries?
I resolved my issue with a small hack.
select * from suggestions
where userId = (
select userId from suggestions
group by userId
order by count(sugId) desc limit 1
)
order by TimeStamp asc;
Just posting it here in case it is needed.
**edit
sugId had to be userId

Looking for identical sequences over all users

I want to get the identical pathes (with counts) of all users
Hey everybody,
Want to keep the question short and hopefully it‘s clear what I want.
I have a table in BigQuery. There I have the following columns
- UserID
- Timestamp
- Domain
- some other columns (but I guess they are unimportant)
I have totally no idea how to fix this!
So I want to look for the same paths over all users and count how many users have the same sequence of domains.
Problem: We are talking about 129 000 users and around 5TB of data. I guess I have to limit the amount of path length or something else.
I‘m familiar with SQL but I need some help/input to keep the costs low. Every query costs money and my thought was to ask the community before I spend thousands of Dollars.
Thanks for any input!
EDIT:
I tried the following to rank the visits of domains:
SELECT
guid,
domain AS channel,
timestamp,
RANK() OVER (PARTITION BY guid ORDER BY timestamp ASC ) AS rank
FROM
data.all
My problem is now: How can I match identical pathes afters merge each "step" in this customer journey?
This may help or at least help you get started:
select domains, count(*)
from (select userid, string_agg(domain order by timestamp, ',') as domains
from t
group by userid
) u
group by domains;
I would prefer to use arrays to store the path itself, but BigQuery does not (yet) support arrays as GROUP BY keys.

Count the amount of unique IP adresses per category using SQL

I'm teaching myself SQL and Access 2016 and now I am trying to do the following:
I have two tables, Table1 contains a list of URI's and categories and Table2 contain a list of URI's and IP's. Now I want to find out how many different IP's there are for each category in the list. Table 1's URI column is a subset of table 2's URI column.
Table 1:
URI | Categories
Table 2:
URI | IP
So obviously I should do an inner join on both tables with the URI which gets me:
URI | Categories | IP
Now I need to find out per category how many different IP's there are. So for example if the category is "Boats" I want to know how many different IP's looked for that category. I believe this can be done with COUNT, but that only works for 1 single category right? I want to figure out the amount of different IP's each category has within a huge dataset (millions of rows), so not just for 1 category.
So basically what I'm looking to get is an output like this:
Category | # of unique IP's
___________________________
Boats | 5
Insects | 22
This question might be a bit simple and honestly I've been thinking about it / googling it for a full day now because the question seemed too simple to ask here, but I'm just stuck now.
Thanks in advance for the help!
P.S. If you need any more examples or explanation on what I'm trying to do just tell me and I'll get it! :)
EDIT:
I forgot to use DISTINCT.. So I just added that but now I get a syntax error in Access 2016? Here is my SQL query:
SELECT Count(DISTINCT Final_parsed_userlogs_access.Field1) AS CountOfField1, Final_category_list_all_uri_access_edited.Field2
FROM Final_category_list_all_uri_access_edited INNER JOIN Final_parsed_userlogs_access ON Final_category_list_all_uri_access_edited.Field1 = Final_parsed_userlogs_access.Field4
GROUP BY Final_category_list_all_uri_access_edited.Field2;
Small final edit:
Apparently Access 2016 doesn't really work with Count distinct (Select Distinct in Access? Syntax error), but I don't understand how I should edit my own query to work with this fix?
Group it based on category, that should help!
Access-Engine does not support
SELECT count(DISTINCT....) FROM ...
You have to do it like this:
SELECT count(*)
FROM
(SELECT DISTINCT Name FROM table1)
Edit your query to:
SELECT Count(T.Field1) AS CountOfField1, T.Field2
FROM
(SELECT Final_parsed_userlogs_access.Field1, Final_category_list_all_uri_access_edited.Field2
FROM Final_category_list_all_uri_access_edited INNER JOIN Final_parsed_userlogs_access ON Final_category_list_all_uri_access_edited.Field1 = Final_parsed_userlogs_access.Field4
GROUP BY Final_category_list_all_uri_access_edited.Field2, Final_parsed_userlogs_access.Field1) as T
GROUP BY T.Field2;
Hope it helps.

How do I identify like sets using SQL?

Using SQL Server, I have a table that looks like this:
What I need to do is write a query to identify scenarios where the Name and Permissions field are equal so that I can give give them a unique Set ID.
For instance, rows 2 and 4 would be a set I can give a SetID as well as rows 6 and 7 are a set that I can give another SetID. But rows 2 and 3 are NOT a set.
So far I have tried using DENSE_RANK () Over(Order by Name) which helps to add an id based on like Names but doesn't take into account matching permissions. And have tried joining the table on itself but with millions of rows of data I end up with unwanted duplicates.
The logic I am following is this:
If (Name and Permissions) of one row = (Name and Permissions) of another row give them a SetID to share.
Please help I have been banging my head against the wall with this one. Ideally a SQL query would accomplish this but am open to anything.
Thank you!
You could do it for example like this:
select
Name,
Permission,
row_number() over (order by Name, Permission) as RN
from (
select distinct
Name,
Permission
from
permissions
) TMP
order by Name, Permission
The inner select gets the distinct combinations, and the outer one assigns the numbers.
SQL Fiddle: http://sqlfiddle.com/#!6/c8319/3
This will probably do something similar to what you want.
SELECT
name,
permissions,
accountname,
ROW_NUMBER() OVER (PARTITION BY name,permissions ORDER By name,permissions) as SetID
FROM table;