-- select minimum of minimum
-- select minimum of id3 , for that id3 select minimum of id2
create table #t ( ID1 int, ID2 int, ID3 int )
insert into #t select 1005, 1, 37
union
select 1005, 2, 0
union
select 1005, 3, 0
union
select 1005, 4, 54
union
select 1005, 5, 38
union
select 1007, 1, 2
union
select 1009, 1, 1
union
select 1009, 2, 3
union
select 1010, 1, 1
union
select 1010, 2, 1
union
select 1010, 3, 2
union select 1010, 4, 3
select * from #t
-- SQL 2005
with rs as
(select id1,id2,id3
,row_number() over (partition by id1 order by id3,id2) as r from #t)
select id1,id2,id3
from rs where r = 1 ;
-- SQL 2005 and SQL 2000
select id1,min(id2) rr,id3
from #t
where id3 =
(select min(id3) from #t t where t.id1=#t.id1 )
group by id1,id3
drop table #t
-- select minimum of id3 , for that id3 select minimum of id2
create table #t ( ID1 int, ID2 int, ID3 int )
insert into #t select 1005, 1, 37
union
select 1005, 2, 0
union
select 1005, 3, 0
union
select 1005, 4, 54
union
select 1005, 5, 38
union
select 1007, 1, 2
union
select 1009, 1, 1
union
select 1009, 2, 3
union
select 1010, 1, 1
union
select 1010, 2, 1
union
select 1010, 3, 2
union select 1010, 4, 3
select * from #t
-- SQL 2005
with rs as
(select id1,id2,id3
,row_number() over (partition by id1 order by id3,id2) as r from #t)
select id1,id2,id3
from rs where r = 1 ;
-- SQL 2005 and SQL 2000
select id1,min(id2) rr,id3
from #t
where id3 =
(select min(id3) from #t t where t.id1=#t.id1 )
group by id1,id3
drop table #t
No comments:
Post a Comment