MS Access 2010 SQL 查询自动四舍五入为整数

MS Access 2010 SQL query is rounding automatically to whole numbers

全部,

我是 运行 下面在 MS Access 2010 中的 SQL 查询。除了 "a.trans_amt" 列四舍五入为整数(即​​查询 returns 12.00 而不是 12.15 或 96.00 而不是 96.30)。有任何想法吗?我希望它显示 2 个小数点。我尝试使用 ROUND 函数但没有成功。

谢谢!

INSERT INTO [2-Matched Activity] ( dbs_eff_date, batch_id_r1, jrnl_name, 
ledger, entity_id_s1, account_s2, intercompany_s6, trans_amt, 
dbs_description, icb_name, fdt_key, combo )
SELECT a.dbs_eff_date, 
a.batch_id_r1, 
a.jrnl_name, 
a.ledger, 
a.entity_id_s1, 
a.account_s2, 
a.intercompany_s6, 
a.trans_amt, 
a.dbs_description, 
a.icb_name, 
a.fdt_key, 
a.combo
FROM [1-ICB Daily Activity] AS a 
INNER JOIN 
(
SELECT 
b.dbs_eff_date, 
b.batch_id_r1, 
b.jrnl_name, 
sum(b.trans_amt) AS ["trans_amt"], 
b.icb_name 
FROM [1-ICB Daily Activity] AS b 
GROUP BY dbs_eff_date, batch_id_r1, jrnl_name, icb_name 
HAVING sum(trans_amt) = 0
)  AS b 
ON (a.dbs_eff_date = b.dbs_eff_date) AND (a.batch_id_r1 = b.batch_id_r1) AND 
(a.jrnl_name = b.jrnl_name) AND (a.icb_name = b.icb_name);

本质上,您是在尝试将十进制精确值附加到整数列。虽然 MS Access 不会引发类型异常,但它会隐式降低精度以适应目标存储。为避免这些不良结果,请提前设置精度类型。

根据MSDN docs,MS Access 数据库引擎维护以下数字类型:

REAL        4 bytes     A single-precision floating-point value with a range of ...
FLOAT       8 bytes     A double-precision floating-point value with a range of ...
SMALLINT    2 bytes     A short integer between – 32,768 and 32,767.
INTEGER     4 bytes     A long integer between – 2,147,483,648 and 2,147,483,647.
DECIMAL    17 bytes     An exact numeric data type that holds values ...

并且 MS Access GUI 在 table 设计界面中将这些翻译为 Field Sizes,其中 Number 的默认格式为 Long Integer 类型。

Byte           — For integers that range from 0 to 255. Storage requirement is a single byte.    
Integer        — For integers that range from -32,768 to +32,767. Storage requirement is two bytes.    
Long Integer   — For integers that range from -2,147,483,648 to +2,147,483,647 ...    
Single         — For numeric floating point values that range from -3.4 x 1038 to ...    
Double         — For numeric floating point values that range from -1.797 x 10308 to ...
Replication ID — For storing a GUID that is required for replication...
Decimal        — For numeric values that range from -9.999... x 1027 to +9.999...

因此,在设计数据库、模式和 table 时,select 需要适当的值来满足您所需的精度。如果不使用 MS Access GUI 程序,您可以在 DDL 命令中定义类型:

CREATE TABLE [2-Matched Activity] (
    ...
    trans_amt DOUBLE,
    ...
)

如果 table 已经存在,请考虑使用另一个 DDL 命令更改设计。

ALTER TABLE [2-Matched Activity] ALTER COLUMN trans_amt DOUBLE

请注意:如果您在查询设计 window 中 运行 CREATEALTER 命令,将不会出现任何提示或确认,但会呈现更改。