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.
Hello,say suppose sample data is userid text date1 m 1/1/20092 n 2/2/20091 p 3/2/20093 r 4/2/20082 l 5/2/2007i want that userid should be distinct so the result should be eitheruserid text date1 m 1/1/20092 n 2/2/20093 r 4/2/2008oruserid text date1 p 3/2/20093 r 4/2/20082 l 5/2/2007means the userid should be distinct in resultset. How can i do this?Thanks in advance!
bklr
Master Smack Fu Yak Hacker
1693 Posts
Posted - 2009-11-30 : 00:31:47
select userid,text,date from (Select row_number()over(partition by userid order by userid)as rid,* from tablename)s where rid = 1
rajdaksha
Aged Yak Warrior
595 Posts
Posted - 2009-11-30 : 00:31:47
HiYou want this...
CREATE TABLE #TEMP(ID INT, TEXT VARCHAR(1), DATE DATETIME)INSERT INTO #TEMPSELECT 1,'M', '1/1/2009' UNION ALLSELECT 2,'N', '2/2/2009' UNION ALLSELECT 1,'P', '3/2/2009' UNION ALLSELECT 3,'R', '4/2/2008' UNION ALLSELECT 2,'L', '5/2/2007'SELECT ID, MIN(TEXT)AS TEXT,MIN(DATE) AS DATE FROM #TEMPGROUP BY IDSELECT ID, MAX(TEXT)AS TEXT,MAX(DATE) AS DATE FROM #TEMPGROUP BY ID