Question : Set an incrementing Sequence number.

I need to create a new column to set sequence numbers for a group of stock market issues that have the same StockID. This field is an integer named DateSeqNo.

The StockHis table has columns for the sequence number, StockID, and QuoteDate

What is the SQL Syntax for the following Psuedocode ?
Select DateSeqNo FROM StockHist WHERE StockID = @StockID ORDER BY QuoteDate
intDateSeqNO = 0
Do While StockID = @StockID
           intDateSeqNo = intDateSeqNo + 1
          UpDate StockHist SET DateSeqNo = intDateSeqNo
Loop

Answer : Set an incrementing Sequence number.

try this.
1:
2:
3:
4:
5:
6:
7:
8:
9:
UPDATE t1
   SET t1.DateSeqNo = t2.DateSeqNo
  FROM StockHist t1
  JOIN (SELECT StockID, QuoteDate,
       	       ROW_NUMBER() OVER (PARTITION BY StockID ORDER BY QuoteDate) AS DateSeqNo
  	  FROM StockHist
 	 WHERE QuoteDate BETWEEN '2009-12-02' AND '2009-12-08') t2
    ON t1.StockID = t2.StockID AND t1.QuoteDate = t2.QuoteDate
 WHERE t1.StockID = 6842
Random Solutions  
 
programming4us programming4us