Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
Hi All,i have table having two column A and B and having data as belowColumn A Column B 1 w 1 x 2 y 2 zI want to fetch record as below Column A Column B 1 w 2 yI want just one record against every value in column A from column B RegardsAmit
raky
Aged Yak Warrior
767 Posts
Posted - 2010-03-11 : 00:57:24
make use of row_number functionfor example like this
declare @temp table ( id int, val varchar(10))insert into @tempselect 1,'a' union allselect 1,'b' union allselect 2,'c' union allselect 2,'d' union allselect 2, 'e' --select * from @tempselect id,valfrom ( select id,val,row_number () over( partition by id order by val ) as rownum from @temp)t where t.rownum = 1
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 belowColumn A Column B 1 w 1 x 2 y 2 zI want to fetch record as below Column A Column B 1 w 2 yI want just one record against every value in column A from column B RegardsAmit
SELECT COLA,COLBFROM( SELECT COLA,COLB,ROW_NUMBER()OVER(PARTITION BY COLA ORDER BY COLB)AS SEQ )T WHERE SEQ=1
madhivanan
Premature Yak Congratulator
22864 Posts
Posted - 2010-03-11 : 02:20:39
select colA,min(colB) as colB from your_tablegroup by colAMadhivananFailing to plan is Planning to fail