Selecting Distinct IDs From a Table - sql

I have a table that looks like such:
firstName ID
Mike 1
James 2
Mike 3
Sally 4
Emma 5
Sally 6
and am trying to get an output that returns each person who has more than 1 different ID, and what those IDs are. In my example it would be like such:
firstName ID
Mike 1
Mike 3
Sally 4
Sally 6
I am working on it and have something like what is below but it is erroring. There is something in the logic I am clearly missing but I am struggling to see what it is. Can someone point me in the direction of what is wrong here?
SELECT firstName, ID
FROM table
GROUP BY ID
HAVING COUNT(ID) > 1

You select names with more than one id using exists:
select t.*
from t
where exists (select 1
from t t2
where t2.firstName = t.firstName and t2.id <> t.id
);

Related

SQL COUNT(DISTINCT(field1)) GROUP BY MAX(filed2)

I have a table like
name num_try
John 2
John 1
Mike 3
Mike 2
Linda 2
And I want to know count distinct names group by MAX(num_try).
Desired result should look like
MAX(num_try) COUNT(DISTINCT(names))
2 2
3 1
Can you help me with this query?
select max_num_try, count(*) from
(
select name, max(num_try) as max_num_try
from table1
group by name
) a
group by max_num_try
order by max_num_try desc

SQL Server get distinct counts of name by each ID

I have a dataset like :
ID NAME
1 Aaron
2 Theon
3 Jon Snow
4 Jon Snow
4 Dany
5 Arya
5 Robert
5 Tyrion
I need to add a new column to this that shows the output based on the number of distinct names per ID. So expected output would be:
ID NAME Mapping
1 Aaron 1
2 Theon 1
3 Jon Snow 1
4 Jon Snow 2
4 Dany 2
5 Arya 3
5 Robert 3
5 Tyrion 3
I am confused about how to achieve this since I have tried a case statement where count(distinct(name)) does not return the right values.
You may try using COUNT as an analytic function:
SELECT
ID,
Name,
COUNT(*) OVER (PARTITION BY ID) Mapping
FROM yourTable
ORDER BY
ID;
Another approach to get COUNT of DISTINCT Name for each ID
SELECT *,
(SELECT Count(DISTINCT NAME)
FROM #table T
WHERE T1.id = T.id) Mapping
FROM #table T1
Online Demo
You can simply use below query
SELECT COUNT(DISTINCT NAME)
FROM YOUR_TABLE
GROUP BY ID
Thanks
Other method (specif SQL Server, otherwise use INNER JOIN LATERAL):
SELECT *
FROM #table f1
CROSS APPLY
(
select Count(*) Nb from #table f2
where f2.ID=f1.ID
) f3

Sql query with selected and splited column value from another table

I have 2 tables
First table
Id Type Value
1 2 1,2,3,5
2 1 1,3,6
3 1 2,3,1,6
Second table
Id Name
1 Leon
2 Anna
3 Biorn
4 Alex
5 Peter
6 Luis
Values in First table are Ids in Second table.
I need query that returns all names by type from the first table
For example:
Type = 1
return: Leon,Anna,Biorn,Luis
type = 2
return: Leon,Anna,Biorn,Peter
I'm trying to create a View that will look like this:
Type Name
1 Leon
1 Anna
1 Biorn
1 Luis
2 Leon
2 Anna
2 Biorn
2 Peter
So I can easily select all the names by type, but I can't figure out how to do it. Please help!
You seem to recognize that this is a poor data structure. You should have a junction table -- storing lists of integers as a delimited string is not a SQLish data structure.
Sometimes, we are stuck with other people's bad design decisions. Here is one thing you can do:
select t1.type, t2.name
from table1 t1 join
table2 t2
on ',' + t1.value + ',' like ',%' + cast(t2.id as varchar(255)) + '%,';

How to select columns that have repeating values from another table in SQLite

I am following Zed Shaw's learnSQLthehardway and I wanted to figure out how to select the names from a table person, who owned multiple pets (pets information in table pet.)
person_pet Table:
person_id pet_id
0 0
0 1
1 1
1 2
2 3
person Table:
id name
0 Zed
1 Orange
2 Limen
pet Table:
id name
0 Jag
1 Black
2 Fluffy
3 Mister
I have been trying to use the COUNT function but I can't seem to get the right results. I must return Zed and Orange based off this data.
SELECT name FROM person, person_pet WHERE id =
(SELECT person_id FROM person_pet GROUP BY person_id HAVING COUNT(person_id) > 1);
This is only returning Zed and not my name. How? What could solve the problem then?
Any help would be great, thank you!
Use SELECT from person table and IN condition from person_pet table:
SELECT name
FROM person
WHERE id IN
(
SELECT person_id from person_pet
GROUP BY person_id
HAVING COUNT(pet_id) > 1
)

Write nested SQL query

I have 2 tables
Table1
ID Name
--------------------
1 John Carter
2 Jack Hammer
3 John Adams
4 John Doe
5 Brian Adams
Table2
ID ID_FromTable1
-----------------------------
1 2
2 3
3 1
4 1
5 1
6 2
7 3
8 1
9 1
10 5
11 4
12 5
13 4
ID in both tables is the primary key
ID_FromTable1 is the foreign key pointing to ID of Table1.
Now I do something like this:
SELECT ID
FROM Table1
WHERE Name like '%John%'
This will give me the IDs 1, 3, 4.
Now using these IDs, I want to write a query on Table2 to delete all entries where ID_FromTable1 are 1, 3, 4.
Please help me write one single SQL query to get all the IDs from Table1 where Name is 'John' and then using those IDs to delete entries from Table2.
I hope I have made the question clear. Do let me know if you need any clarification.
You can use IN with subquery:
DELETE FROM Table2
WHERE ID_FromTable1 IN ( SELECT ID
FROM Table1
WHERE Name LIKE '%John%' )
In MySQL you can do it with this join
delete table2
from table2
join table1 on table2.id_fromtable1 = table1.id
WHERE t1.Name like '%John%'