Re: Running counter
From: Hugo Kornelis (hugo_at_pe_NO_rFact.in_SPAM_fo)
Date: 12/27/04
- Next message: MichaelK: "Re: Running counter"
- Previous message: MichaelK: "Running counter"
- In reply to: MichaelK: "Running counter"
- Next in thread: MichaelK: "Re: Running counter"
- Reply: MichaelK: "Re: Running counter"
- Messages sorted by: [ date ] [ thread ]
Date: Tue, 28 Dec 2004 00:27:35 +0100
On Mon, 27 Dec 2004 13:37:05 -0800, MichaelK wrote:
>Is it possible to make a query to have a counter which would have
>a sequential number of each record in the query starting with 1?
Hi Michael,
That depends. Relational databases handle sets, which are UNordered by
definition, so there is no such thing as an intrinsic "row number". But if
you can define something to base the numbering on, you can have SQL Server
add the numbers.
As an example, the following query will list all authors with a ranking
based on alphabetic order of last name (note how the tie for Albert Ringer
and Ann Ringer is handled!):
USE pubs
GO
SELECT au_id, au_lname, au_fname,
(SELECT 1 + COUNT(*)
FROM authors AS b
WHERE b.au_lname < a.au_lname) AS rank
FROM authors AS a
ORDER BY rank
GO
Best, Hugo
-- (Remove _NO_ and _SPAM_ to get my e-mail address)
- Next message: MichaelK: "Re: Running counter"
- Previous message: MichaelK: "Running counter"
- In reply to: MichaelK: "Running counter"
- Next in thread: MichaelK: "Re: Running counter"
- Reply: MichaelK: "Re: Running counter"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|
|