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
Related
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
);
Lets say my table contains the following data
id
name
version
1
Rahul
1
1
Rahul
2
2
John
1
3
Mike
1
2
John
2
4
Rubel
1
5
David
1
1
Rahul
3
I need to filter the duplicate records with lower version. How can this be done?
The output essentially should be
id
name
version
1
Rahul
3
2
John
2
3
Mike
1
4
Rubel
1
5
David
1
For this dataset, aggregation seems sufficient:
select id, name, max(version) as max_version
from mytable
group by id, name
You can use not exists as follows:
select id, name, version
from your_table t
Where not exists
(Select 1 from your_table tt
Where tt.id = t.id and tt.version > t.version)
Or you can use analytical function row_number as follows:
Select id, name, version from
(select t.*,
Row_number() over (partition by id order by version desc) as rn
from your_table t) t
Where rn = 1
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
I have a following table
id group name
1 2 dodo
2 1 sdf
3 2 sd
4 3 dfs
5 3 fda
....
and i want to get intro record from each group like following
id group name
... 1 sdf
2 dodo
3 dfs
...
SELECT MIN(id) id, group, name
FROM TABLE1
GROUP BY group
ORDER BY group
select * from table_name where id in (select min(id) from table_name group by group)
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;