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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 First Equivalent in SQL Server

Author  Topic 

pr4t3ekd
Starting Member

31 Posts

Posted - 2009-09-04 : 09:02:24
Hey,

Just started using SQL server today.

I have the following table e.g.


FName | LName | ID
Peter | Fang | 6000
James | Fang | 7000
Data | TEST | 6000
NULL | NULL | 8000
NULL | NULL | 9000
NULL | NULL | 10000
NULL | NULL | 7000


I would like all the records where ID is distinct or the first time SQL server see's an ID... so really the equivelent to First in Access or something.

I tried Distinct ofcourse but this is based on the whole row so therefore not sufficient

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-09-04 : 09:18:24
where Id is distinct:
select FName,LName,ID from table group by FName,LName,ID having count(*)=1

First:
Without a datetime you cannot see which ID was the first...


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-09-04 : 09:24:11
use row_number..........
Go to Top of Page

pr4t3ekd
Starting Member

31 Posts

Posted - 2009-09-04 : 09:26:58
so how would i just get a unique list regardless of it being the first id or a latter id. In the end, i just want unique ID's

Based on the above, the result should be:

FName | LName | ID
Peter | Fang | 6000
James | Fang | 7000
NULL | NULL | 8000
NULL | NULL | 9000
NULL | NULL | 10000
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-09-04 : 09:28:56
select * from
(select row_number()over(partition by id order by id)as rid, * from tablename ) s
where rid = 1
Go to Top of Page

pr4t3ekd
Starting Member

31 Posts

Posted - 2009-09-04 : 09:33:02
thank you bklr. that seems to work. will test that with the real data.
Go to Top of Page
   

- Advertisement -