有没有办法在MS 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,我只是想找到简短,干净,高效的代码。
答案 0 :(得分:4)
您可以使用一个简单的笛卡尔查询,其中包含一个数字从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;
答案 1 :(得分:2)
我认为你可以使用这样的解决方案:
创建一个表(将其命名为t
),如下所示:
ID | i
---+---
1 | 0
2 | 1
3 | 2
4 | 3
5 | 4
6 | 5
7 | 6
8 | 7
9 | 8
10 | 9
现在可以根据需要使用此表,例如,从0
到9999
进行查询,使用此查询:
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;
例如,您可以将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#)