Login / Register  search  syndication  about

          Kris Halsrud's Blog

Kris Halsrud on development and Integration with CRM and Development

SQL to add an incrementing counter in results

One thing I alwys forget how to do is to add a sequential number to a query output in SQL.  Using the rank function you can get this.  A  query such as

 

select rank() OVER (ORDER BY accountid) as xrank, account, accountid from account

Will return the accountid and a sequential number based on the AccountID sort order.

SQL Rank Output

Similarly you can do something like this to sequence the results based on a sort on account names:

select rank() OVER (ORDER BY account) as xrank, account, accountid from account

SQL Rank Output 2

 

What's This?
  
Bookmark and Share

About Kris Halsrud

   Kris Halsrud is a Senior Analyst / Developer for Customer FX Corporation.


Related Content
   Creating a Unique Saleslogix table ID in a SQL Function
A couple of months ago Ryan Farley wrote an article about how to use a SQL stored procedure to create a S
Posted on Mar 25, 2014 by Kris Halsrud to Kris Halsrud's Blog
 
   How to get sizes for tables in a SQL 2008 database
Under the heading of "often needed and never remembered", I decided to post a quick T-SQl examp
Posted on Apr 18, 2013 by Kris Halsrud to Kris Halsrud's Blog
 
   Errors caused by invalild dates in the SalesLogix entity model
Working with non-nullable types in C# can be a bit of a pain.  In the world of SalesLogix you may oc
Posted on Nov 10, 2011 by Kris Halsrud to Kris Halsrud's Blog
 
   How to fix the inability to open/view a SSIS package in BIDS
 I recently ran into an issue with a SQL server installation at a client where I tried to open a SSI
Posted on Aug 30, 2011 by Kris Halsrud to Kris Halsrud's Blog
 
   Scripting out all existing indexes in a SalesLogix database as "Create Index" statements
 I was recently asked to provide the ability to extract out of a SalesLogix SQL database, all of the
Posted on Aug 16, 2011 by Kris Halsrud to Kris Halsrud's Blog
 
Comments

No Comments

Leave a Comment

(required)  
(optional)
(required)  
Add
All contents Copyright © 2014 Customer FX Corporation
Customer FX Corporation
2324 University Avenue West, Suite 115
Saint Paul, Minnesota 55114
Tel: 800.728.5783

  Follow @CustomerFX on twitter
Follow the best news, tips, and articles
  Subscribe to Customer FX on youtube
Watch SalesLogix tutorial videos from Customer FX
Login / Register