0

Appreciate help if anybody let me know how to acheive "PreviousValue" cloumn as shown below in MSAccess

I have records stored in table for various categories other than "Overall" and based on ID/Name/Office/product/Category combination I want to acheive previous value column thorugh MS Access function/efficient query

ID       Name        Office Product Overall    Month         Value  Previous
228      Woodlands  london  Europe  Sales      201106          6    0
228      Woodlands  london  Europe  Sales      201202          3    6
228      Woodlands  london  Europe  Sales      201207          4    3
228      Woodlands  london  Europe  Overall    201106          6    0
228      Woodlands  london  Europe  Overall    201202          3    6
228      Woodlands  london  Europe  Overall    201207          4    3
228      Woodlands  london  Europe  Exetion    201202          6    0
228      Woodlands  london  UK      Exetion    201202          6    1
228      Woodlands  london  UK      Exetion    201106          1    0
228      Woodlands  london  Europe  Exetion    201207          6    6
228      Woodlands  london  Europe  Content    201106          6    0
228      Woodlands  london  UK      Content    201106          1    0
228      Woodlands  london  Europe  Content    201202          3    6
228      Woodlands  london  UK      Content    201202          1    1
228      Woodlands  london  UK      Content    201207          1    1
228      Woodlands  london  Europe  Content    201207          4    3

Thanks in Advance !!


Result of query--Which is not as expected because of Top1 function i guess, the correct output is as shown in image below

 ID  Name      Office   Product Category    Date       Month    ValuePrevious
228  Investors  london  Europe  Content   01/06/2011    201106  6   0
228  Investors  london  Europe  Content   23/02/2012    201202  3   6
228 Investors   london  Europe  Content   01/07/2012    201207  4   6

enter image description here

5
  • Please do not post pictures. Even very badly formatted data is better than a picture. Post the data and I can certainly answer. Commented Aug 17, 2012 at 7:59
  • i could not see option of uploading excel sheet Commented Aug 17, 2012 at 8:17
  • Your tags say MS Access. Open the table, cut and paste a few lines into your post. If the worst comes to the worst, type them. Commented Aug 17, 2012 at 8:18
  • Your sample data does not match your new post. Do you have the correct sample data? Commented Aug 17, 2012 at 10:01
  • Can only these three rows be taken as base to frame query Commented Aug 17, 2012 at 10:08

1 Answer 1

0

You can use a subquery to get the previous value.

SELECT tx.id,
       tx.name,
       tx.office,
       tx.product,
       tx.overall,
       tx.month,
       tx.VALUE,
       tx.previous,
       Nz((SELECT TOP 1 [value]
           FROM   tablename t
           WHERE  t.id = tx.id
                  AND t.name = tx.name
                  AND t.office = tx.office
                  AND t.product = tx.product
                  AND t.overall = tx.overall
                  AND t.month < tx.month
           ORDER  BY t.month, rnd(t.id) DESC), 0) AS Prev
FROM   tablename tx
ORDER  BY tx.month; 

New query based on changed, very sparse, sample data

SELECT tz.id, 
       tz.name, 
       tz.office, 
       tz.product, 
       tz.date, 
       tz.month, 
       tz.VALUE, 
       Nz((SELECT TOP 1 [value] 
           FROM   tablename t 
           WHERE  t.id = tz.id 
                  AND t.name = tz.name 
                  AND t.office = tz.office 
                  AND t.product = tz.product 
                  AND t.date < tz.date
           ORDER  BY t.date DESC), 0) AS Prev 
FROM   tablename tz 
ORDER  BY tz.date; 
Sign up to request clarification or add additional context in comments.

7 Comments

Hi Remou thanks for your quick help..But i am getting error as "At most one record can be returned by this subquery"..i have pasted query in my original post..actually query is giving 400-500 records but after that giving this error..
In that case you have more duplicate records that I would have thought possible. Have you got a unique ID on the table?
no unique ID..it's sharepoint maintained table..can i debug somehow which are duplicates? will have corrected them
Okay, add rnd(t.id) to the ORDER BY as illustrated above.
The error indicates that you have more than one record with exactly the same name, office,product, overall and month. The addition to ORDER BY will pick one of these records randomly. This may not be what you need.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.