Find duplicate sql record - sql

I have a database table in vbulletin, where a table has just 2 columns:
Col 1 : userid
Col 2 : relationid
Col1 may have multiple entries, like:
userid relationid
1 A
1 B
1 C
2 B
2 T
I would like to extract a csv or just manage to order them, so i could end up with some thing like this:
userid entries
1 100
2 12
3 44
4 33
5 33
Where userid is the repeating number in col1, and entries is how many times the userid is repeating itself.

Try like this;
select userid, count(*) as entries from table group by userid

SELECT userid, COUNT(userid) FROM TableName GROUP BY userid;
This query will return userid and a count of all userids from TableName. Because we are using COUNT, we also must GROUP BY any additional columns we are selecting.

Related

Get duplicate on single column after distinct across multiple columns in SQL

I have a table that looks like this:
name | id
-----------
A 1
A 1
B 2
C 1
D 3
D 3
F 2
I want to return id's 1 and 2 because they are duplicate on names. I don't want to return 3, because it is distinct for D 3.
Basically, I'm thinking of doing a query to first get a distinct pairing, so the above reduces to
name | id
-----------
A 1
B 2
C 1
D 3
F 2
And then doing a duplicate find on the id column. However, I'm struggling to find the correct syntax to construct that query.
You should be able to get the result you want by using a GROUP BY along with a HAVING clause that counts the distinct names. The HAVING clause will filter for those ids that have more than one distinct name:
select id
from Table1
group by id
having count(distinct name) > 1
Here is a demo

SQL - How to Count from multiple tables and add them together

I am trying to do a count from multiple tables but there could be multiple entries in each table. Here is simple sample data simplified. There are actually more then 3 tables but just so I get an understanding of how to do it
table2 table2 table3
person_ID person_id person_id
1 1 2
2 1 2
3 2 1
4 2 4
5 4 5
I'm trying to get a count of each person ID in each table so the output would be the following. Note that personID is a key I don't need the addition of the number of the ID not 2+2+2+2. But the count of the number of appearances it makes in the all tables then the count in each table added together for total number of appearances it makes. Basically I'm trying to find a total number of items attached to each personID
person_id total
1 4
2 4
3 1
4 3
5 2
Select the ids from all the tables together withunion. That result can be grouped by the id and counted for each
select person_id, count(*) as count
from
(
select person_id from table1
union all
select person_id from table2
union all
select person_id from table3
) tmp
group by person_id

Find average amount of times a value appears in table

How do I count the average amount of times a given number appears in a database?
id | ...
----------
1 | ...
5 | ...
2 | ...
3 | ...
3 | ...
1 | ...
6 | ...
4 | ...
3 | ...
...| ...
id corresponds to the id of the user. Perhaps the table is for customer orders or donations made by a user. For the above table:
id 1 = 2 entries
id 2 = 1 entry
id 3 = 3 entries
id 4 = 1 entry
id 5 = 1 entry
id 6 = 1 entry
Average = (2+1+3+1+1+1)/6 = 1.5 entries per user
The average number of orders/donations made per user is 1.5 to give an example.
I could do something like the below:
$getTotalEntries = $db->prepare("
SELECT *
FROM table
");
$getTotalEntries->execute();
$totalEntries = $getTotalEntries->rowCount();
$getGroupedEntries = $db->prepare("
SELECT *
FROM table
GROUP BY id
");
$getGroupedEntries->execute();
$groupedEntries = $getTotalEntries->rowCount();
$average = $totalEntries/$groupedEntries;
I'm hoping for a single SQL request, however. Incidentally, the below gives me the number of occurances of a given id, but I cannot AVG() them.
$getAverageEntries = $db->prepare("
SELECT id, COUNT(*)
FROM table
GROUP BY id
"); // works, returns the 2,1,3,1,... from before
$getAverageEntries = $db->prepare("
SELECT AVG(COUNT(*))
FROM table
GROUP BY id
"); // won't find aggregate count
How about this?
select count(id) / count(distinct id) as avgEntriesPerUser
from table t;
The only issue with this would be a NULL value for id. If this occurred (and I find it highly unlikely for a column named id), then the above ignores those rows entirely. It can be modified to take this situation into account.
select avg(a.entryCount)
from (
select id, count(id) as entryCount
from <tablename>
group by id
) a;
in SQL you need to do a count to get the number of entries
select avg(entries) from(
Select Distinct Id.tableName, count(Id) As Entries
from tableName
group by ID)
You mean?
select avg(countPerID) from (
select id, count(*) as countPerID from table group by id) x

SQL - Order by amount of occurrences

It's my first question here so I hope I can explain it well enough,
I want to order my data by amount of occurrences in the table.
My table is like this:
id Daynr
1 2
1 4
2 4
2 5
2 6
3 1
4 2
4 5
And I want it to sort it like this:
id Daynr
3 1
1 2
1 4
4 2
4 5
2 4
2 5
2 6
Player #3 has one day in the table, and Player #1 has 2.
My table is named "dayid"
Both id and Daynr are foreign keys, together making it a primary key
I hope this explains my problem enough, Please ask for more information it's my first time here.
Thanks in advance
You can do this by counting the number of times that things occur for each id. Most databases support window functions, so you can do this as:
select id, daynr
from (select t.*, count(*) over (partition by id) as cnt
from table t
) t
order by cnt, id;
You can also express this as a join:
select t.id, t.daynr
from table as t inner join
(select id, count(*) as cnt
from table
group by id
) as tg
on t.id = tg.id
order by tg.cnt, id;
Note that both of these include the id in the order by. That way, if two ids have the same count, all rows for the id will appear together.

How do I use a select query to get the least of one value for each unique second value?

There are groups like this;
USER_ID SEQ_ID NAME
1 2 Armut
1 3 Elma
1 4 Kiraz
2 1 Nar
2 2 Uzum
4 3 Sheftali
4 4 Karpuz
4 5 Kavun
After select query I want to see only;
USER_ID SEQ_ID NAME
1 2 Armut
2 1 Nar
4 3 Karpuz
That is, I want the row with the least SEQ_ID for each USER_ID. What SQL query will give me this result?
Best regards
SELECT USER_ID, SEQ_ID, NAME
FROM table
WHERE NAME IN ('Armut', 'Nar', 'Karpuz')
ORDER BY USER_ID
If you have something else in mind, please clarify your question.
Looks to me like it should be:
SELECT USER_ID, MIN(SEQ_ID) AS SEQ_ID, NAME
FROM table
GROUP BY USER_ID, NAME
ORDER BY USER_ID;