2012-08-17 64 views
0

欣賞幫助,如果有人讓我知道如何acheive「PREVIOUSVALUE」 cloumn在MSACCESS歷年戰績-MS訪問

我存儲在表比「整體」等各種類別的記錄上顯示的下方,基於ID /名稱/辦公/產品/分類組合,我想acheive前值列thorugh的MS Access功能/高效的查詢

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 

先謝謝了!


查詢的結果 - 未如預期這是因爲TOP1功能我猜的,爲如下的圖像正確的輸出低於

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

+0

請不要將圖片張貼新的查詢。即使格式非常糟糕的數據也比圖片更好。發佈數據,我當然可以回答。 – Fionnuala 2012-08-17 07:59:08

+0

我看不到上傳excel表格的選項 – user1268559 2012-08-17 08:17:13

+0

你的標籤說MS Access。打開桌子,在你的文章中剪切並粘貼幾行文字。如果最糟糕的情況出現,請輸入它們。 – Fionnuala 2012-08-17 08:18:48

回答

0

您可以使用子查詢獲得以前的價值。

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; 

基於改變,很稀疏,樣本數據

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

嗨Remou感謝您的快速幫助..但我得到的錯誤爲「至多有一個記錄可以由此子查詢返回」..我已粘貼查詢在我的原post..actually query給了400-500條記錄,但在此之後給出這個錯誤.. – user1268559 2012-08-17 09:23:19

+0

在這種情況下,你有更多的重複記錄,我認爲可能。桌上有沒有唯一的ID? – Fionnuala 2012-08-17 09:25:54

+0

沒有唯一的ID ..它的共享點維護表..我可以調試某些重複?將會糾正它們 – user1268559 2012-08-17 09:28:23