fikir
Programmer
- Jun 25, 2007
- 86
One thing is troubling me
I have a table
delcare @tbl table (depid int, empid int)
insert into @tbl
select 1, 11
union all select 1, 12
union all select 1, 13
union all select 1, 14
union all select 2, 20
union all select 2, 23
union all select 3, 30
I want to select any top max 2 empid for for each depid
i.e two record for each depid if it has two or more records otherwise one
in this case the recordset looks like
depid empid
1 11
1 12
2 20
2 23
3 30
can we use top keyword with group by
Thanks,
I have a table
delcare @tbl table (depid int, empid int)
insert into @tbl
select 1, 11
union all select 1, 12
union all select 1, 13
union all select 1, 14
union all select 2, 20
union all select 2, 23
union all select 3, 30
I want to select any top max 2 empid for for each depid
i.e two record for each depid if it has two or more records otherwise one
in this case the recordset looks like
depid empid
1 11
1 12
2 20
2 23
3 30
can we use top keyword with group by
Thanks,