Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
Hi all,

I have data in a table as:

FileId	ParentId	BatchId
1	1	1
2	1	1
3	1	1
4	2	1
5	2	1
6	2	1
7	3	1
8	3	1
9	3	1
10	4	1


I want top 1 fileid on the basis of parentid and batcid.

The output should be as below

FileId	ParentId	BatchId
1	1	1
4	2	1
7	3	1
10	4	1


[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 20-Dec-13 22:24pm
v2
Comments
♥…ЯҠ…♥ 21-Dec-13 3:45am    
Nice question 5+

Here it is :
SQL
select t.fileid, t.parentid, batchid from
(select *, row_number() over (partition by parentid, batchid order by fileid) rno from tablename) t
where rno =1


Replace tablename with your table name.

Good Luck
 
Share this answer
 
Try:
SQL
SELECT a.FileId, a.ParentId, a.BatchId FROM MyTable a
JOIN (SELECT MIN(FileId) AS FileId FROM MyTable GROUP BY ParentId) b
ON a.FileId=b.FileId
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900