Using Oracle, I'm looking to do the following query, but I'd like to know if there is a more "intelligent" way to do it.
Select * from Sales Sales1
left join Sales Sales2 on Sales2.val = Sales1.val
left join Sales Sales3 on Sales3.val = Sales2.val
left join Sales Sales4 on Sales4.val = Sales3.val
left join Sales Sales5 on Sales5.val = Sales4.val
...
Here's what my sample data might look like
customer number | acct | start balance | open date | prev account
a 1 100 01-01-15 b-1
b 1 80 03-04-14
c 2 200 04-11-14 c-1
c 1 150 06-12-15
d 1 600 08-16-15
e 3 400 12-19-15 e-2
e 2 150 10-21-14 e-1
e 1 100 01-18-13
And a result set would look like this:
Customer | start | open | prevStart_01 | prevOpen_01 | prevStart_02 | prevOpen_02
a-1 | 100| 01-01-15| 80 | 03-04-14 | |
c-2 | 200| 04-11-14| 150 | 06-11-14 | |
e-3 | 400| 12-19-15| 150 | 10-21-14 | 100| 01-18-13
As you can see, I need to keep joining another record of sales based upon the result, and I need to keep doing so until I return an empty result set. My current scenario is running the query and seeing whether there are values in sales5, sales6, sales7, and so on.