SQL query, count and group by - sql

If I have data like this:
Key
Name
1
Dan
2
Tom
3
Jon
4
Tom
5
Sam
6
Dan
What is the SQL query to bring back the records where Name is repeated 2 or more times?
So the result I would want is
Tom
Dan

Couldn't be simpler...
Select Name, Count(Name) As Count
From Table
Group By Name
Having Count(Name) > 1
Order By Count(Name) Desc
This could also be extended to delete duplicates:
Delete From Table
Where Key In (
Select Max(Key)
From Table
Group By Name
Having Count(Name) > 1
)

select name from table group by name having count(name) > 1

This could also be accomplished by joining the table with itself,
SELECT DISTINCT t1.name
FROM tbl t1
INNER JOIN tbl t2
ON t1.name = t2.name
WHERE t1.key != t2.key;

Related

join two sql table as column

i have two tables as below:
firsttable
id
cat_id
name
1
2
name_01
2
2
name_02
3
1
name_03
4
3
name_04
5
3
name_04
secondtable
id
name
1
cat_01
2
cat_02
3
cat_03
my question is how can i create below table result?
id(secondtable)
name(secondtable)
count(firsttable)
1
cat_01
1
2
cat_02
2
3
cat_03
2
select t2.id,t2.name,
(select count(*) from firsttable t1 where t1.cat_id=t2.id )as count
from secendtable t2;
Just using standard aggregation
select
s.id,
s.name,
count(*)
from firsttable f
join secondtable s
on f.cat_id = s.id
group by s.id, s.name order by s.id
It's very Simple like that
SELECT s.id,s.name, (SELECT count(*)
FROM `firsttable` AS f
WHERE f.cat_id = s.id ) as count
FROM `secondtable` AS s

How to get max count of referral to a root node in a tree

I've a table with users by structure like this:
id name parent_id
1 Mike
2 Jack 1
3 Sam 1
4 Kurt 1
5 Somebody 3
6 Tommy 4
6 etc.. 2
How to get a max count of referral on first level nesting per user, by this example I expect result:
3 because Jack, Sam, Kurt is a referral of Mike on first level
Assuming "first level" is defined by parent_id IS NULL and the current version Postgres 9.4:
SELECT parent_id, count(*) AS referral_ct
FROM (
SELECT id AS parent_id
FROM tbl
WHERE t1.parent_id IS NULL
) t1
JOIN tbl t2 USING (parent_id)
GROUP BY 1
ORDER BY 2 DESC
LIMIT 1; -- to only get 1 row with max. referral_ct
With only few root nodes, JOIN LATERAL may be faster:
SELECT t1.id, t2.referral_ct
FROM (
SELECT id
FROM tbl
WHERE parent_id IS NULL
) t1
LEFT JOIN LATERAL (
SELECT parent_id, count(*) AS referral_ct
FROM tbl
WHERE parent_id = t1.id
GROUP BY 1
) t2 ON true
ORDER BY 2 DESC
LIMIT 1; -- to only get 1 row with max. referral_ct
Related, with more explanation:
Optimize GROUP BY query to retrieve latest record per user

SQL Duplicates Query

The table is as follows:
id name
1 tom
2 bob
3 tom
4 tom
5 harry
I'm looking for a query that would result in the following:
id name duplicateposition
1 tom 1
2 bob 1
3 tom 2
4 tom 3
5 harry 1
So duplicateposition for the first tom is 1, for the second tom is 2, and for the third tom is 3. The first bob is 1 and the first harry is 1. Is there an easy query to do this?
Here is my attempt:
SELECT id, name, count(id) from table
I'm a little new to sql so that is my best shot.
You can do the following. It partitions the data by name and assigns a number within the partition. Forgot to mention I did this in SQL Server 2012.
select *,
row_number() over (partition by name order by id) as DuplicatePosition
from test
order by id
SQL Fiddle Demo
Here's a generic self join that works on most databases (although if you have access to window functions you should use them instead as they're generally faster)
select t1.id, t1.name, count(t2.id) duplicateposition
from mytable t1
join mytable t2
on t2.name = t1.name
and t2.id <= t1.id
group by t1.id, t1.name
order by t1.id

Get first row of each group in access2010

There are multiple way to get first row from each group, but none of my idea is working with access2010.
Do you have a solution to get first row in access2010 ?
Or
ID Name Age
1 Name1 3
2 Name2 4
3 Name1 2
4 Name2 5
It should get the top row in each group (name column) so the output would be
1 Name1 3
2 Name2 4
Here's a solution that still uses a sub query, but only once, instead of on each record.
SELECT T1.*
FROM mytable AS T1
WHERE T1.id IN (SELECT First(T2.id)
FROM mytable T2
GROUP BY T2.name)
select * from table t1 where ID in
(sel min(ID) from table where t1.name=name);

I want a query to select more than 4 AccountId repetitions from table2 having distinct name in table1

I want a query to select more than 4 AccountId repetitions from table2 having distinct name in table1
Here are the table structures:
table1 | Table2
ID AccountNumber Name | ID AccountNumber AccountID
1 12345 John Jeff | 1 12345 A467T
1 12345 Patrick Jones | 1 12345 A467T
ID and AccountNumber are the same on both tables
Table1 has Name
Table2 has AccountID
Here's a query to find AccountID's with more than 4 different names:
select t2.AccountID
from Table1 as t1
join Table2 as t2
on t1.AccountNumber = t2.AccountNumber
and t1.ID = t2.ID
group by
t2.AccountID
having count(distinct t1.Name) > 4
If that's not what you were looking for, please clarify the question! For examole, you could add desired output.
EDIT: In reply to your comment, you could query the ID and Numbers with a subquery:
select distinct
ID
, AccountNumber
, AccoutnID
from Table2
where AccountNumber in
(
... place query from above here ...
)