Solved

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

Posted on 2007-03-23
3
426 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 142

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
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, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now