Solved

SQL to update table with "item numbers"

Posted on 2011-09-13
3
256 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

839 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