Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have two tables:

  • SamplesTbl contains samples every 5 seconds of a bike ride
  • RxTbl contains the prescribed stages and other values of a bike ride


SamplesID int
SessionNum int
StageNum int
RunTime int


RxID int
RxNum int
RxStageNum int
RxRunTime int

In certain cases (not all) a prescribed bike ride is broken up into more stages than prescribed (5 stages are turned into 20 stages). Currently I use cursors and temporary tables to take these 20 stages to map them back to the 5 prescribed stages based on the number of stages it takes from the samples table to equal the prescribed run time.

For example, stage 1 is prescribed at 5 minutes but the actual exercise data would contain stages 1 through 5 that equal the 5 minutes (1-4: 65 seconds, 5: 40 seconds), stage 2 is prescribed at 3 minutes but the actual exercise data would contain stages 6 through 8 that equal the 3 minutes (1-2: 65 seconds, 3: 50 seconds), etc.

Is there a way to retrieve the result set without using cursors and/or temporary tables?

share|improve this question
Scripts to create the schema and test data go along way toward getting good answers for questions like this. Your existing query would be useful also. – Mark Storey-Smith Dec 12 '12 at 23:37
Expected output would be useful too – Paul White Dec 13 '12 at 1:04

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.