3

How to get a value from previous result row of a SELECT statement

If we have a table called cardevent and has rows [ID(int) , Value(Money) ] and we have some rows in it, for example

ID --Value

1------70 
1------90
2------100
2------150
2------300 
3------150 
3------200 
3-----250 
3-----280 

so on...

How to make one Query that get each row ID,Value and the previous Row Value in which data appear as follow

ID --- Value ---Prev_Value

1 ----- 70 ----------  0 
1 ----- 90 ---------- 70
2 ----- 100 --------  90 
2 ------150 -------- 100
2 ------300 -------- 150
3 ----- 150 -------- 300 
3 ----- 200 -------- 150 
3 ---- 250 -------- 200 
3 ---- 280 -------- 250 

so on.

i make the following query but it's so bad in performance in huge amount of data

SELECT cardevent.ID, cardevent.Value, 
      (SELECT F1.Value
       FROM cardevent as F1 
       where F1.ID = (SELECT  Max(F2.ID)
                      FROM cardevent as F2 
                      WHERE F2.ID < cardevent.ID)
      ) AS Prev_Value
FROM cardevent

So can anyone help me to get the best solution for such a problem ?

4

4 Answers 4

6

This one should work fine for both databases:

SELECT cardevent.ID, cardevent.Value, 
  (SELECT TOP 1 F1.Value
   FROM cardevent as F1
   WHERE F1.ID < cardevent.ID
   ORDER BY F1.ID DESC
   ) AS Prev_Value
FROM cardevent

Update: Assuming that ID is not unique, but that the combination of ID and Value is unique (you must specify what imposes your ordering on the table, so one knows what is the previous row), you must use this query:

select cardevent.ID, cardevent.Value, 
    (select TOP 1 F1.Value from cardevent as F1
     where (F1.ID < cardevent.ID) or (F1.ID = cardevent.ID and F1.Value < cardevent.Value)
     order by F1.ID DESC, F1.Value DESC
    ) AS Prev_Value
from cardevent 
16
  • @MicSim - I used your query, but it Showing Error "At Most one record can be returned by the subquery" nothing display in prev_value field
    – Gopal
    Jul 1, 2009 at 7:51
  • Output Showing like this.... PERSONID CARDEVENTDATE Expr1002 Prev_Value #Name? #Name? #Name? #Name? #Name? #Name? #Name? #Name? #Name? #Name? #Name? #Name? #Name? #Name? #Name? #Name?
    – Gopal
    Jul 1, 2009 at 8:25
  • Just tested above query with Access 2000 (don't have 2003 here) and it's working. Maybe you have extended/modified the query?
    – MicSim
    Jul 1, 2009 at 8:53
  • Your query is working Fine, but Id is not different all the time for the value. Example. id, value - 1 100, 2 200, 2 400, 2 500, 3 100 3 200 so on.. So Result is.. 1 100 0, 2 200 100, 2 400 200, 2 500 400, 3 100 500, 3 200 100 so on.
    – Gopal
    Jul 1, 2009 at 9:55
  • @MicSim - I used your updated query, but it Showing Error "At Most one record can be returned by the subquery"
    – Gopal
    Jul 1, 2009 at 11:45
3

Try something like this:

SELECT C.ID, C.Value, COALESCE(C1.Value, 0)
FROM cardevent C
    LEFT JOIN cardevent C1
    ON C1.Id = (SELECT MAX(id) FROM cardevent C2 where C2.Id < C.Id)
5
  • +1. This seems to be the fastes of all given solutions. Jul 1, 2009 at 7:34
  • It seems to compile to the same execution plan as MicSim's solution - and it makes sense if you look closely. I'd say both are ok. Jul 1, 2009 at 7:39
  • I didn't look close enough at the actual execution plans, only at the percentages given by SQL Server. All solutions are working afaik, main difference is execution time and readability. Yours is fastest (or as fast as MicSim's solution) and most readable. Mine is... well, not so good <g> Jul 1, 2009 at 7:45
  • +1 for the use of COALESCE. Its not often I see this used but its a very useful command in SQL to know Jul 1, 2009 at 8:09
  • Though COALESCE doesn't work in MS Access, just SQL Server.
    – MicSim
    Jul 1, 2009 at 8:56
-1

How does following perform?

SELECT F1.ID, F1.Value, F3.Value
FROM cardevent F1
     INNER JOIN (
        SELECT F1.ID, mID = MAX(F2.ID)
        FROM cardevent F1
             INNER JOIN cardevent F2 ON F2.ID < F1.ID
        GROUP BY F1.ID
      ) F2 ON F2.id = F1.ID
     INNER JOIN cardevent F3 ON F3.ID = F2.mID
UNION ALL
SELECT F1.ID, F1.Value, 0
FROM cardevent F1
     INNER JOIN (
        SELECT ID = MIN(ID) 
        FROM cardevent
      ) F2 ON F2.ID = F1.ID
ORDER BY 1
-1

I prefer TOP 1 instead of MAX(...), which IMHO should be also faster:

SELECT      C.ID, C.Value, COALESCE(C1.Value, 0)
FROM        cardevent C
LEFT JOIN   cardevent C1
        ON  C1.ID = (SELECT TOP 1 C2.ID FROM cardevent C2 WHERE C2.ID < C.ID ORDER BY C2.ID)
2
  • 1
    Using access 2003 database, is not accepting coalesce function
    – Gopal
    Jul 1, 2009 at 7:52
  • @Gopal: I am not about the efficiency of the code in MSAccess, but IIF and ISNULL should help: "IIF(ISNULL(C1.Value), 0, C1.Value))"
    – van
    Jul 1, 2009 at 8:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.