Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4619054
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:22:19+00:00 2026-05-22T02:22:19+00:00

I have a bunch of records in a table variable like so: Id ProductId

  • 0

I have a bunch of records in a table variable like so:

Id     ProductId   Rank    RankCreated
1      123213      2       2011-05-02
2      123213      4       2011-05-03
3      123213      1       2011-05-03
4      155432      10      2011-05-01
5      155432      10      2011-05-02

Id is an identity column i added to my table variable (will explain why i need it in a moment). ProductId is a Product. Rank is a value which represents a product’s rank at a given time. RankCreated is the time that Product was ranked.

What im trying to do:

Calculate the “movement” between each product rank, for each product. Where “movement” is defined as current – previous.

So the “computed column” would look like this:

Id     ProductId   Rank    RankCreated   Movement
1      123213      2       2011-05-02    NULL
2      123213      4       2011-05-03    2
3      123213      1       2011-05-03    -3
4      155432      10      2011-05-01    NULL
5      155432      10      2011-05-02    0

I added the Id column so i could use that to fetch the previous record.

Here’s how i got the data into the temp table:

insert into @rankhistories (productid, [rank], [rankcreated])
select a.ProductId, b.[rank]
from dbo.ProductRankHistories b
inner join dbo.Products a on a.ProductId = b.ProductId
order by a.ProductId, b.RankCreated

I really can’t see how i can avoid a cursor here. There are 6000+ records in that table variable, and with my cursor solution it took 5 seconds, which isn’t acceptable.

Can anyone help?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T02:22:20+00:00Added an answer on May 22, 2026 at 2:22 am
    DECLARE @TV TABLE
    (
    Id INT IDENTITY(1,1) PRIMARY KEY,
    ProductId INT,
    Rank INT,   
    RankCreated DATE
    )
    
    
     /*Populate *6000 rows of random data*/
    INSERT INTO @TV
    SELECT TOP 6000 
                  ROW_NUMBER() OVER (ORDER BY (SELECT 0)) / 9 AS ProductId,
                  CRYPT_GEN_RANDOM(1) % 10 AS Rank,
                  GETDATE() AS RankCreated
    FROM master..spt_values v1,master..spt_values v2
    
    
    SELECT t1.Id, 
           t1.ProductId, 
           t1.Rank, 
           t1.RankCreated, 
           t2.Rank - t1.Rank AS Movement 
    FROM @TV t1
    LEFT MERGE JOIN @TV t2 ON t1.Id = t2.Id+1 AND t1.ProductId=t2.ProductId
    ORDER BY t1.Id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say that I have a table with a bunch of records, which I want
I have a bunch of records in a MySQL table, each with a date
I have got a bunch of records in locations table: ... *************************** 8. row
I have a table with a whole bunch of fields and records in it
I have a bunch of records with dates formatted as a string such as
I have a bunch of records I want to move to another database and
I have bunch of strings, some of which are fairly long, like so: movie.titles
I have bunch of lists like this: out[3] [[3]] [1] forum=28&mid=11883 [2] forum=29&mid=11884 out[4]
I have a database which has a table with an XML column. The XML
I have a variable called c_kilometers. I have a cursor that grabs a bunch

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.