s-l-o-w query looking up records in same table (also difference between rows)

zerocred

New member
Local time
Today, 19:53
Joined
Dec 2, 2008
Messages
1
I have a table and query that looks up previous records in. It works, but its really slow. Any suggestions to speed it up?
Table t_detail fields:
Code:
id date stock qty brand 
1 1/1/2008 1 1 A 
2 1/2/2008 2 2 B 
3 1/3/2008 3 3 A
the records of interest are not adjacent rows, so a select is needed to get each one out.
How do I avoid needing the nested selects for each 'old' field I want to pull out?
The query to outputs something like
Code:
 date brand stock qty old_stock old_qty 
1/2/2008 B 2 2 - - 
1/3/2008 A 3 3 1 1
I use the next previous occurance of brand. (I can work out differences as required from this) Attached is the SQL.
 

Attachments

Last edited:
Well, with several nested sub queries, the whole thing is bound to run slow. Maybe u could create a sub that first creates a temp table, adds the relevant rows to it, then executes a simple select query to retrieve the data needed.

HTH
 

Users who are viewing this thread

Back
Top Bottom