I have table like this
id
name
1
Apple
1
Banana
1
Guava
2
Cassava
2
Carrot
2
Potato
3
Almond
3
Soybeans
3
Peanuts
I want to select only the first one from each id
id
name
1
Apple
2
Cassava
3
Almond
What's the query like?
you can try this way
SELECT id, name FROM table_name GROUP BY id ORDER BY id ASC;
You can achieve your goal by using row_number(). You can check my query in db-fiddle: https://www.db-fiddle.com/f/g4MrUTTTGDFfqjAKYnkFxn/1
WITH CTE as
(
SELECT id, name, ROW_NUMBER() OVER (ORDER BY null) as rowNumber FROM Fruits
)
SELECT id, name FROM CTE WHERE rowNumber IN(SELECT min(rowNumber) FROM CTE Group by id)
Related
so i have a database table like:
NAME name_ID
---------------
Joao 1
Maria 3
Joao 1
carlos 2
carlos 2
i want to do a select query that displays all duplicates only like this :
NAME name_ID
---------------
Joao 1
Joao 1
carlos 2
carlos 2
and other select query that displays singles like so :
NAME name_ID
---------------
Maria 3
This would be simpler if you had a unique id column in each table. I encourage you to design tables with primary keys.
In any case, you can do this with a query of the form for duplicates:
select t.*
from databasetable t join
(select name, count(*) as cnt
from databasetable
group by name
) tt
on t.name = tt.name
where cnt > 1;
For singletons, the comparison would be cnt = 1.
EDIT:
With a unique id and an index on (name, id), the following is probably faster for duplicates:
select t.*
from databasetabe t
where exists (select 1
from databasetable t2
where t2.name = t.name and t2.id <> t.id
);
Singletons would use not exists instead.
select name from table group by name haveing count(*)=1 ###for Maria
select * from table where not in (previous select) order by name
I have a table with this data in it:
ID Name Color
1 Kyle Blue
1 Susan Orange
1 Steven Orange
2 Susan Blue
I want to use a query like this:
Select * from table group by ID, Top1(Name), Top1(Color)
So I get these results:
ID Name Color
1 Kyle Blue
2 Susan Blue
I don't care if it's Kyle Blue, or Steven Orange as long as the color matches the name.
Eg
SELECT x.*
FROM my_table x
JOIN (SELECT id, MIN(name) min_name FROM my_table GROUP BY id) y
ON y.id = x.id
AND y.min_name = x.name;
select *
from (
select *,
row_number() over (partition by Name order by Name) as row_number
from table
) as rows
where row_number = 1
Okay I have two tables:
Table 1 looks like this:
id age gender
1 10 M
2 11 F
3 11 F
And Table 2 looks like this (same with different values):
id age gender
1 11 F
2 12 M
3 10 M
Now I want my final output to look like the following:
age count
10 2
11 3
12 1
What is the most efficient way to achieve this?
You want to aggregate the union:
select age, count(*)
from (select id, age, gender from table1 union all
select id, age, gender from table2
) t
group by age
try this
select age ,count(age) count from table1 group by age
union
select age, count(age) count from table2 group by age
I have a query that returns data in the following format:
id | name | number
1 John 12545
1 John 50496
2 Mary 23443
3 Mark 54
3 Mark 5600
3 Mark 50206
I would like to find out the number of distinct ids that appear in the result set. For example, for the result above. I would like to obtain the value 3.
Is there any way to add a column so the result looks like this instead?
count | id | name | number
3 1 John 12545
3 1 John 50496
3 2 Mary 23443
3 3 Mark 54
3 3 Mark 5600
3 3 Mark 50206
My query is:
SELECT * FROM (
SELECT id FROM tableA
WHERE xyz
) as t1
JOIN tableB using (id)
SELECT (SELECT COUNT(DISTINCT id) FROM tableName) totalCount,
id,name,number
FROM tableName
or by using CROSS JOIN
SELECT x.totalCount,
a.id, a.name, a.number
FROM tableName a, (SELECT COUNT(DISTINCT id) totalCount
FROM tableName) x
You should try :
SELECT id,name,number, (SELECT COUNT(DISTINCT name) FROM YourTableName) FROM YourTableName
Good luck
SELECT COUNT(DISTINCT id) would be faster than using column name.
SELECT (SELECT COUNT(DISTINCT id) FROM tableName) as 'count',
id,name,number
FROM tableName
SELECT COUNT(id) AS count , id, name, number
FROM
(
SELECT id
FROM tableA
WHERE xyz
) as t1
JOIN tableB using (id)
GROUP BY id, name, number
Table
ID Name
1 abc
2 cde
3 xyz
4 abc
5 cde
6 cde
My expected result is
abc 2
cde 3
how will be the query ?
You can do this with a GROUP BY statement:
SELECT name, count(*) cnt
FROM your_table
GROUP BY name;
As suggested by #jadarnel27, you can limit the results to only showing duplicates as follows:
SELECT name, count(*) cnt
FROM your_table
GROUP BY name
HAVING cnt > 1;
Try this:
SELECT Name, count(*)
FROM Table
GROUP BY Name
ORDER BY Name