Solved

Selecting data from previous record in current record with custom order by

Posted on 2007-03-23
3
429 Views
Last Modified: 2008-01-09
Experts,

I have a bit of a challenge for you. Mind you, I did already solve it but my solution is quite ugly (using multiple subqueries etc). I am looking for the most elegant and efficient way to select data from a "previous" record in the "current" record in a query. To explain what I mean, here's the example of the data and the example of what I want the results to be:

MyTable:
MyKey:     MySortValue:
1              50
2              20
3              70

MyResultView:
MyKey:     MySortValue:   MyPreviousKey:     MyPreviousSortValue:
2              20                    NULL                      NULL
1              50                    2                            20
3              70                    1                            50

(I hope it formats okay)
0
Comment
Question by:NeoTeq
3 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 18778608
you should do this in the front-end. much simpler and hence more elegant :)
in sql, this will be a terrible performance results...

anyhow, I would give this a whirl
select t.*, p.*
from MyTable t
left join MyTable p
  on p.MyKey = ( select top 1 i.MyKey from MyTable i where i.MySortValue < t.MySortValue order by i.MySortValue desc )
order by t.MySortValue

0
 
LVL 2

Expert Comment

by:sunil_mails
ID: 18779986
heres the answer buddy

select x.MyKey, x.MySortValue, Y.MyKey as MyPreviousKey, x.MySortValue as MyPreviousSortValue
from MyTable X
left join MyTable Y
      on a.MyKey=b.MyKey-1
0
 
LVL 10

Author Comment

by:NeoTeq
ID: 18780311
AngelIII, your solution was definately cleaner than mine and it worked, thanks. Don't worry about the speed implications as this happens on a small table only (30-100 rows).

Sunil, the sorting has to be on MySortValue, not on my key since there can be all sorts of gaps there.
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

830 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