id name marks
1;"mohit";10
2;"mohit";20
3;"raj";30
4;"rupesh";40
5;"rupesh";50
Expected output is :
1;"mohit";10
4;"rupesh";40
Try this
SELECT *
FROM Your_Table
WHERE ID IN (
SELECT Min(ID)
FROM Your_Table
GROUP BY Name
HAVING Count(*) > 1
)
I think DISTINCT ON
should work here:
SELECT DISTINCT ON (name) name, id, marks
FROM your_table
ORDER BY name, id;
This assumes that you want the first row of each person with regard to the id
.
Late Edit:
It looks like you want the first record from each name group where a name appears at least two or more times. We can try the following query:
SELECT id, name, marks
FROM
(
SELECT *,
ROW_NUMBER() OVER (PARTITION BY name ORDER BY id) rn,
COUNT(*) OVER (PARTITION BY name) cnt
FROM your_table
) t
WHERE rn = 1 AND cnt > 1;
USE GROUP BY TO COUNT MULTIPLE OCCURENCE..
SELECT * FROM table WHERE name IN( SELECT name FROM table GROUP BY name HAVING COUNT(name) > 1)
I think this should work
select * from
(select ROW_NUMBER() OVER(
PARTITION BY name
ORDER BY id ) as rnum , name ,id ,marks
from your_table) A
where rnum = 1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.