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)
 Select Only the First Value in a Recordset

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2003-10-03 : 07:46:21
Mike writes "Is there a keyword to select records based on the record being the first instance of a value in a given field or at least a clean way of setting that up if no keyword exists? I basically have a database of user logins and I am only interested in pulling records from the first instance of each user logging in.

I know that in Access there are the first() and last() commands that will return this type of data, but I do not know of similar functions in Microsoft SQL Server and have not been able to formulate logic to cleanly do it another way."

dsdeming

479 Posts

Posted - 2003-10-03 : 08:05:06
There is a TOP keyword in T-SQL:

SELECT TOP 1 *
FROM MyTable
WHERE ID > 7

Dennis
Go to Top of Page

mohdowais
Sheikh of Yak Knowledge

1456 Posts

Posted - 2003-10-03 : 08:09:34
You are probably after the MIN() aggregate function. The following query will return the first login time for each user:

SELECT UserID, MIN(LoginTime) FROM Users
GROUP BY UserID

Owais


Make it idiot proof and someone will make a better idiot
Go to Top of Page
   

- Advertisement -