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

时间:2018-01-24 19:16:19

标签: sql ms-access ms-access-2010

所有

我在MS Access 2010中运行下面的SQL查询。一切正常,除了" a.trans_amt"列被舍入为整数(即​​查询返回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);

1 个答案:

答案 0 :(得分:1)

实质上,您正在尝试将十进制精确值附加到整数列。虽然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在表设计界面中将这些转换为Field Sizes,其中默认格式为 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...

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

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

如果表已存在,请考虑使用其他DDL命令更改设计。

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

请注意:如果您在“查询设计”窗口中运行CREATEALTER命令,则不会出现提示或确认,但会进行更改。