法语十进制格式数字的 sqlite sum()

Posted

技术标签:

【中文标题】法语十进制格式数字的 sqlite sum()【英文标题】:sqlite sum() of French decimal format numbers 【发布时间】:2015-01-26 14:39:03 【问题描述】:

当我意识到这种类型的 SQLite SQL 查询时:

SELECT SUM(Column) as TOTAL_DEBIT FROM table_name;

我希望加实数,但它给了我整数除法的总和。

例如:

SELECT SUM(DEBIT) as TOTAL_DEBIT FROM DEBIT

下表中的 SQL 查询给出:

TOTAL_DEBIT
11475

我希望我能得到这样的结果:

TOTAL_DEBIT
11482,79

示例表:

   DROP TABLE IF EXISTS "DEBIT";
   CREATE TABLE "DEBIT" ("DATE" DATETIME NOT NULL , "DEBIT" REAL NOT NULL );
   INSERT INTO "DEBIT" VALUES('23/12/13','29,90');
   INSERT INTO "DEBIT" VALUES('23/12/13','304,00');
   INSERT INTO "DEBIT" VALUES('15/01/14','1245,48');
   INSERT INTO "DEBIT" VALUES('21/01/14','304,00');
   INSERT INTO "DEBIT" VALUES('23/01/14','30,00');
   INSERT INTO "DEBIT" VALUES('28/01/14','129,10');
   INSERT INTO "DEBIT" VALUES('31/01/14','32,10');
   INSERT INTO "DEBIT" VALUES('21/02/14','30,00');
   INSERT INTO "DEBIT" VALUES('21/02/14','304,00');
   INSERT INTO "DEBIT" VALUES('17/03/14','117,66');
   INSERT INTO "DEBIT" VALUES('21/03/14','30,00');
   INSERT INTO "DEBIT" VALUES('21/03/14','304,00');
   INSERT INTO "DEBIT" VALUES('02/04/14','175,62');
   INSERT INTO "DEBIT" VALUES('22/04/14','304,00');
   INSERT INTO "DEBIT" VALUES('23/04/14','30,00');
   INSERT INTO "DEBIT" VALUES('30/04/14','32,10');
   INSERT INTO "DEBIT" VALUES('21/05/14','304,00');
   INSERT INTO "DEBIT" VALUES('22/05/14','2838,00');
   INSERT INTO "DEBIT" VALUES('22/05/14','30,00');
   INSERT INTO "DEBIT" VALUES('30/05/14','145,41');
   INSERT INTO "DEBIT" VALUES('23/06/14','30,00');
   INSERT INTO "DEBIT" VALUES('23/06/14','304,00');
   INSERT INTO "DEBIT" VALUES('25/06/14','112,48');
   INSERT INTO "DEBIT" VALUES('21/07/14','304,00');
   INSERT INTO "DEBIT" VALUES('22/07/14','30,00');
   INSERT INTO "DEBIT" VALUES('24/07/14','1245,48');
   INSERT INTO "DEBIT" VALUES('25/07/14','127,68');
   INSERT INTO "DEBIT" VALUES('31/07/14','32,10');
   INSERT INTO "DEBIT" VALUES('12/08/14','23,89');
   INSERT INTO "DEBIT" VALUES('21/08/14','30,00');
   INSERT INTO "DEBIT" VALUES('21/08/14','304,00');
   INSERT INTO "DEBIT" VALUES('10/09/14','480,00');
   INSERT INTO "DEBIT" VALUES('22/09/14','30,00');
   INSERT INTO "DEBIT" VALUES('22/09/14','304,00');
   INSERT INTO "DEBIT" VALUES('29/09/14','131,37');
   INSERT INTO "DEBIT" VALUES('21/10/14','30,00');
   INSERT INTO "DEBIT" VALUES('21/10/14','304,00');
   INSERT INTO "DEBIT" VALUES('24/10/14','96,00');
   INSERT INTO "DEBIT" VALUES('31/10/14','32,50');
   INSERT INTO "DEBIT" VALUES('21/11/14','304,00');
   INSERT INTO "DEBIT" VALUES('24/11/14','30,00');
   INSERT INTO "DEBIT" VALUES('28/11/14','143,92');
   INSERT INTO "DEBIT" VALUES('22/12/14','30,00');
   INSERT INTO "DEBIT" VALUES('22/12/14','304,00');

提前感谢您的帮助和问候, 阿尔班

【问题讨论】:

啊!问题来自法语十进制格式:在法国,逗号用于分隔实数。国际格式使用句点作为分隔符。 数字是否以字符格式存储? 另一个困难是如果格式中已经有句点.(标记数千、数百万等)。在这种情况下,如下所述的简单REPLACE() 将不起作用。 您显示的表格使用 REAL。那么为什么要插入字符串呢?插入数值,例如 29.90 而不是 '29,90' 应该没有问题。不幸的是,SQLite 至少可以说对数据类型不是很严格,所以一旦你输入字符串,它很可能会存储字符串而不是数字,而不是引发异常。而且看起来它在对字符串求和时甚至不会出错,而只是取字符串开头的数字。 【参考方案1】:

你能不能先REPLACE,SUM然后再REPLACE呢?

SELECT REPLACE(SUM(REPLACE(DEBIT,',','.')),'.',',') AS TOTAL_DEBIT 
FROM DEBIT

【讨论】:

【参考方案2】:

如果数字存储为文本,那么您可以执行以下操作(我不会依赖隐式转换):

SELECT REPLACE(SUM(CAST(REPLACE(debit, ',', '.') AS REAL)), '.', ',') AS total_debit
  FROM debit

【讨论】:

【参考方案3】:

我忘记了法国小数分隔符与国际十进制系统不同:用点代替逗号时效果很好:

SQL 查询:

SELECT ROUND(SUM(DEBIT),2) as amount FROM DEBIT;

这个 SQL 脚本效果很好:

DROP TABLE IF EXISTS "DEBIT";
CREATE TABLE "DEBIT" ("DATE" DATETIME NOT NULL , "DEBIT" REAL NOT NULL );
INSERT INTO "DEBIT" VALUES('23/12/13','29.90');
INSERT INTO "DEBIT" VALUES('23/12/13','304.00');
INSERT INTO "DEBIT" VALUES('15/01/14','1245.48');
INSERT INTO "DEBIT" VALUES('21/01/14','304.00');
INSERT INTO "DEBIT" VALUES('23/01/14','30.00');
INSERT INTO "DEBIT" VALUES('28/01/14','129.10');
INSERT INTO "DEBIT" VALUES('31/01/14','32.10');
INSERT INTO "DEBIT" VALUES('21/02/14','30.00');
INSERT INTO "DEBIT" VALUES('21/02/14','304.00');
INSERT INTO "DEBIT" VALUES('17/03/14','117.66');
INSERT INTO "DEBIT" VALUES('21/03/14','30.00');
INSERT INTO "DEBIT" VALUES('21/03/14','304.00');
INSERT INTO "DEBIT" VALUES('02/04/14','175.62');
INSERT INTO "DEBIT" VALUES('22/04/14','304.00');
INSERT INTO "DEBIT" VALUES('23/04/14','30.00');
INSERT INTO "DEBIT" VALUES('30/04/14','32.10');
INSERT INTO "DEBIT" VALUES('21/05/14','304.00');
INSERT INTO "DEBIT" VALUES('22/05/14','2838.00');
INSERT INTO "DEBIT" VALUES('22/05/14','30.00');
INSERT INTO "DEBIT" VALUES('30/05/14','145.41');
INSERT INTO "DEBIT" VALUES('23/06/14','30.00');
INSERT INTO "DEBIT" VALUES('23/06/14','304.00');
INSERT INTO "DEBIT" VALUES('25/06/14','112.48');
INSERT INTO "DEBIT" VALUES('21/07/14','304.00');
INSERT INTO "DEBIT" VALUES('22/07/14','30.00');
INSERT INTO "DEBIT" VALUES('24/07/14','1245.48');
INSERT INTO "DEBIT" VALUES('25/07/14','127.68');
INSERT INTO "DEBIT" VALUES('31/07/14','32.10');
INSERT INTO "DEBIT" VALUES('12/08/14','23.89');
INSERT INTO "DEBIT" VALUES('21/08/14','30.00');
INSERT INTO "DEBIT" VALUES('21/08/14','304.00');
INSERT INTO "DEBIT" VALUES('10/09/14','480.00');
INSERT INTO "DEBIT" VALUES('22/09/14','30.00');
INSERT INTO "DEBIT" VALUES('22/09/14','304.00');
INSERT INTO "DEBIT" VALUES('29/09/14','131.37');
INSERT INTO "DEBIT" VALUES('21/10/14','30.00');
INSERT INTO "DEBIT" VALUES('21/10/14','304.00');
INSERT INTO "DEBIT" VALUES('24/10/14','96.00');
INSERT INTO "DEBIT" VALUES('31/10/14','32.50');
INSERT INTO "DEBIT" VALUES('21/11/14','304.00');
INSERT INTO "DEBIT" VALUES('24/11/14','30.00');
INSERT INTO "DEBIT" VALUES('28/11/14','143.92');
INSERT INTO "DEBIT" VALUES('22/12/14','30.00');
INSERT INTO "DEBIT" VALUES('22/12/14','304.00');

【讨论】:

您根本不应该依赖区域设置。日期是一个日期,应以 ISO 格式“yyyy-mm-dd”输入,无论任何区域设置如何都能被识别。数字也一样。数字不是字符串。因此,既不要输入“20.90”,也不要输入“29,90”,而是输入不带引号的 29.90。

以上是关于法语十进制格式数字的 sqlite sum()的主要内容,如果未能解决你的问题,请参考以下文章

在 SQLite 上转换表达式结果,从浮点数到十进制数

解析存储为文本的数字,逗号为十进制,点为千位

SQLite 中是不是可以进行十进制数约束?

如何将十进制格式转换为数字格式

使用标准数字格式字符串时的上标价格十进制数字

DecimalFormat格式化十进制数字