Take a look at this:
Ordered set domesticus vulgaris, nothing unusual:
Now, for any single specified primary key (CustomerID) calculate it's ordinal position within ordered set. Examples:
ROMEY - should return 1
SPLIR - 8
BLAH - NULL (does not exist)
No cursors, no identity tables and no SQL2005 ranking functions please.
And also - don't assume sorted column (ContactName) is unique.
------
Theory: everybody knows everything, nothing works
Practice: everything works, nobody knows why
![[banghead] [banghead] [banghead]](/data/assets/smilies/banghead.gif)
Code:
use northwind
select top 10 CustomerID, CompanyName, ContactName, Address
from Customers
where CustomerID >= 'B'
order by ContactName
Code:
CustomerID CompanyName ContactName Address
----------.------------------------.-------------------.-------------------------
ROMEY Romero y tomillo Alejandra Camino Gran Vía, 1
MORGK Morgenstern Gesundkost Alexander Feuer Heerstr. 22
TRADH Tradiçao Hipermercados Anabela Domingues Av. Ines de Castro, 414
GOURL Gourmet Lanchonetes André Fonseca Av. Brasil, 442
EASTC Eastern Connection Ann Devon 35 King George
LAMAI La maison d'Asie Annette Roulet 1 rue Alsace-Lorraine
FAMIA Familia Arquibaldo Aria Cruz Rua Orós, 92
SPLIR Split Rail Beer & Ale Art Braunschweiger P.O. Box 555
QUEDE Que Delícia Bernardo Batista Rua da Panificadora, 12
FRANR France restauration Carine Schmitt 54, rue Royale
Now, for any single specified primary key (CustomerID) calculate it's ordinal position within ordered set. Examples:
ROMEY - should return 1
SPLIR - 8
BLAH - NULL (does not exist)
No cursors, no identity tables and no SQL2005 ranking functions please.
And also - don't assume sorted column (ContactName) is unique.
------
Theory: everybody knows everything, nothing works
Practice: everything works, nobody knows why
![[banghead] [banghead] [banghead]](/data/assets/smilies/banghead.gif)