Solved

SQL to update table with "item numbers"

Posted on 2011-09-13
3
255 Views
Last Modified: 2012-05-12
I am trying to duplicate some functionality I had in an Access app in a SQL Server environment via asp.net web forms.

My problem is with numbering items that are assigned to a Purchase Order.  After creating a new Purchase Order, users are directed to a screen where they can add items to their "cart".  The cart is based on a temporary table created via stored procedure with a parameter for the table name, which is the username plus date plus "TempItems".  

When they click a "submit" button, I want to sort the items they've added in the temp table and populate an "itemnumber" field with a sequential number - for each record, it's the count of items with itemdescriptions that are <= each record's itemdescription, if that makes any sense.

I used DCount in my Access app, and the Append Query I ran had a field with alias "Sequence" which was:  Sequence: Dcount("itemid", "tempitems", "itemdescription <=itemdescription).  This method worked very well.  

How do I do it in SQL Server??
0
Comment
Question by:Alastair_White
  • 2
3 Comments
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 36533165
Hi. In the tags you have SQL 2005, is that the correct version you are using? If so, introduced in SQL 2005 is windows function ROW_NUMBER() which combined with OVER() analytical clause will do what you need.

Some thing like:
ROW_NUMBER OVER(ORDER BY ItemDescription) AS "Sequence"

Open in new window


0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 36533173
For some more information on it, especially since you did this in Access, please see my article which compares the ranking options between SQL and Access; therefore, it should be a worthwhile read.
http://www.experts-exchange.com/Programming/Languages/SQL_Syntax/A_1555-Analytical-SQL-Where-do-you-rank.html
0
 

Author Closing Comment

by:Alastair_White
ID: 36537005
Yes, I am using SQL Server 2005, and I have now used the row_number() / over() combo to get my "item numbers" as desired.  

Thanks!
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
query execution hang 5 31
string fuctions 4 26
SQL - Use results of SELECT DISTINCT in a JOIN 4 20
Proper Case SQL Command 2 10
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how the fundamental information of how to create a table.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question