Previous record -MS Access

后端 未结 1 1541
有刺的猬
有刺的猬 2021-01-28 08:44

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 tha

相关标签:
1条回答
  • 2021-01-28 09:11

    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; 
    
    0 讨论(0)
提交回复
热议问题