dimanche 16 août 2015

skip records based on columns condition

Hi I have one querstion in sql server

table name : Emp

Id  |Pid  |Firstname| LastName  | Level
1   |101  | Ram     |Kumar      | 3
1   |100  | Ravi    |Kumar      | 2
2   |101  | Jaid    |Balu       | 10
1   |100  | Hari    | Babu      | 5
1   |103  | nani    | Jai       |44
1   |103  | Nani    | Balu      |10
3   |103  |bani     |lalu       |20

Here need to retrive unique records based on id and Pid columns and duplicate records need to skip. Finally I want output like below

Id  |Pid  |Firstname| LastName  | Level
1   |101  | Ram     |Kumar      | 3
2   |101  | Jaid    |Balu       | 10
3   |103  |bani     |lalu       |20

I found duplicate records based on below query

select id,pid,count(*) from emp group by id,pid having count(*)>=2 

this query get duplicated records 2 that records need to skip to retrive output

please tell me how to wirte query to achive this task in sql server .

Aucun commentaire:

Enregistrer un commentaire