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 2000 Forums
 SQL Server Development (2000)
 First in SQL Server

Author  Topic 

Lazer
Starting Member

8 Posts

Posted - 2002-01-23 : 15:12:12
I'm in middle of migrating my Access database to SQL Server and I got a Query using the First function from access dose anyone know what it the equivalent function for First() from access in SQL Server?

exp.
SELECT First(PK_ID) as FirstPKID, [Name]
FROM tblTesting
GROUP BY [Name]



Edited by - Lazer on 01/23/2002 15:12:41

Tigger
Yak Posting Veteran

85 Posts

Posted - 2002-01-23 : 15:22:54
Assuming PK_ID is numeric and First(PK_ID) is the lowest number try using Min(PK_ID)


Go to Top of Page

Lazer
Starting Member

8 Posts

Posted - 2002-01-23 : 15:49:21
Sorry PK_ID is a varchar(20)

and the query will look like this:
SELECT First(PK_ID) as FirstPKID, [Name]
FROM tblTesting
GROUP BY [Name]
ORDER BY [Name]


Go to Top of Page

Tigger
Yak Posting Veteran

85 Posts

Posted - 2002-01-23 : 16:07:48
Even though PK_ID is varchar, Min(PK_ID) should still work if the first PK_ID is always the one closest to 'A' eg if PK_ID was 'BCD' and 'BGF' and First(PK_ID) was always 'BCD'.

Otherwise, I think you will have to use a cursor and loop through.

Does anyone else have any ideas on this?

Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2002-01-23 : 16:09:33
Min() will still work for you (so will Max(), the other aggregate functions won't though). There's really no concept of "first" in a relational database because it depends on how you order the data. The physical storage order is not a factor, nor should it be. Don't rely on physical ordering for your query results, you'll very rarely get consistent results.

Go to Top of Page

Lazer
Starting Member

8 Posts

Posted - 2002-01-23 : 17:01:09
Thanks it's working now.
I didn't know that the Min function will work on a Varchar type field

Thanks Again.

Go to Top of Page
   

- Advertisement -