2015-09-05 104 views
1

有什麼辦法可以在MS Access中創建查詢,它返回我選擇的連續號碼?例如Microsoft Access查詢返回連續號碼

10.1 
10.2 
10.3 
10.4 
10.5 

100 
90 
80 
70 
60 
50 
40 
30 
20 

2015-04-10 12:00 
2015-04-10 13:00 
2015-04-10 14:00 
2015-04-10 15:00 
2015-04-10 16:00 
2015-04-10 17:00 
2015-04-10 18:00 

我可以創建使用VBA這些值臨時表。我也可以創建自己的VBA函數,並將其放入查詢中。問題是:這是純粹的MS Access查詢可能嗎?如果不是,最簡單和最快的方法是什麼?

編輯:我不必避免VBA,我只是想找到簡短,乾淨,高效的代碼。

回答

3

您可以使用一個簡單的笛卡爾查詢有一個用數字表從0到9:

SELECT 
    t1.Number + 
    t2.Number * 10 + 
    t3.Number * 100 + 
    t4.Number * 1000 As Factor 
FROM 
    tblNumber AS t1, 
    tblNumber AS t2, 
    tblNumber AS t3, 
    tblNumber AS t4 

或 - 如果你只需要小系列 - 只是一個數字表,從0到99

然後爲您的樣品:

SELECT 
    10 + 0.1 * [Number] AS [Value] 
FROM 
    tblNumber 
WHERE 
    [Number] Between 1 And 5 

和:

SELECT 
    [Number] * 10 AS [Value] 
FROM 
    tblNumber 
WHERE 
    [Number] Between 2 And 10 
ORDER BY 
    [Number] * 10 Desc 

和:

SELECT 
    DateAdd("h", [Number], #2015-04-10 12:00 PM#) AS [Date] 
FROM 
    tblNumber 
WHERE 
    [Number] Between 0 And 6 

附錄

多個系列也可以不用專門的數字表通過使用系統表作爲源建:

SELECT DISTINCT 
    [Tens]+[Ones] AS Factor, 
    10*Abs([Deca].[id] Mod 10) AS Tens, 
    Abs([Uno].[id] Mod 10) AS Ones 
FROM 
    msysobjects AS Uno, 
    msysobjects AS Deca; 
+0

好像做小桌子與號碼是唯一的解決辦法。這個答案與shA.t的答案基本相同,但代碼看起來更乾淨。你們都是對的,但這個答案對我來說更好。 – Combinatix

+0

不,它不是唯一的解決方案,請參閱附錄。 – Gustav

2

我認爲你可以使用這樣一個解決方案:像這樣

  1. 做一個表(命名爲t):

    ID | i 
    ---+--- 
    1 | 0 
    2 | 1 
    3 | 2 
    4 | 3 
    5 | 4 
    6 | 5 
    7 | 6 
    8 | 7 
    9 | 8 
    10 | 9 
    
  2. 現在使用此表爲您提供所需,爲例如,使用此查詢來查詢09999

    SELECT 
        i1 + i2 * 10 + i3 * 100 + i4 * 1000 AS rowNo 
    FROM 
        (SELECT 
         t1.i AS i1, t2.i AS i2 
        FROM 
         t AS t1, t AS t2) AS tt1, 
        (SELECT 
         t1.i AS i3, t2.i AS i4 
        FROM 
         t AS t1, t AS t2) AS tt2; 
    
  3. 例如,您可以更改rowNo公式這些:

    CDbl('10.'+CStr([i1]+[i2]*10+[i3]*100+[i4]*1000)) => 10.1, 10.2, ... 
    

    (10000 - ([i1]+[i2]*10+[i3]*100+[i4]*1000)) * 10 => 100000, 99990, ... , 20, 10 
    

    DateAdd("h",[i1]+[i2]*10+[i3]*100+[i4]*1000,#4/10/2015 12:00:00 PM#)