Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to fetch single record against one record

Author  Topic 

amit.2601
Starting Member

8 Posts

Posted - 2010-03-11 : 00:49:42
Hi All,
i have table having two column A and B and having data as below

Column A Column B

1 w
1 x
2 y
2 z

I want to fetch record as below

Column A Column B
1 w
2 y

I want just one record against every value in column A from column B

Regards
Amit

raky
Aged Yak Warrior

767 Posts

Posted - 2010-03-11 : 00:57:24
make use of row_number function

for example like this

declare @temp table ( id int, val varchar(10))
insert into @temp
select 1,'a' union all
select 1,'b' union all
select 2,'c' union all
select 2,'d' union all
select 2, 'e'

--select * from @temp

select id,val
from
( select id,val,row_number () over( partition by id order by val ) as rownum
from @temp
)t
where t.rownum = 1
Go to Top of Page

haroon2k9
Constraint Violating Yak Guru

328 Posts

Posted - 2010-03-11 : 01:03:49
quote:
Originally posted by amit.2601

Hi All,
i have table having two column A and B and having data as below

Column A Column B

1 w
1 x
2 y
2 z

I want to fetch record as below

Column A Column B
1 w
2 y

I want just one record against every value in column A from column B

Regards
Amit




SELECT COLA,COLB
FROM(
SELECT COLA,COLB,ROW_NUMBER()OVER(PARTITION BY COLA ORDER BY COLB)AS SEQ
)T WHERE SEQ=1
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-03-11 : 02:20:39
select colA,min(colB) as colB from your_table
group by colA

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -