将 varchar 转换为数字和总和会出现错误:

发布于 01-16 00:23 字数 316 浏览 2 评论 0原文

我试图通过将其转换为数字来求和此列,但这些值也有逗号,这是我的查询中的错误来源:

SELECT date_trunc('day', to_date("date" , 'mm-dd-yyyy')) as day, 
"from merch", 
sum(CAST("amount " AS numeric)) as amount 
FROM tb 
GROUP BY 1,2 
ORDER BY 1,2;

我试图获取每天每个“来自商品”的金额总和。

我得到的错误是:无效数字,值“,”,位置 1,类型:十进制

I am trying to sum this column by casting this as numeric, but the values have commas as well which are the source of error on my query:

SELECT date_trunc('day', to_date("date" , 'mm-dd-yyyy')) as day, 
"from merch", 
sum(CAST("amount " AS numeric)) as amount 
FROM tb 
GROUP BY 1,2 
ORDER BY 1,2;

I am trying to get the sum of amounts for each "from merch" per day.

Error i get is: Invalid Digit, Value ',', Pos 1, Type: Decimal

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奢欲2025-01-23 00:23:32

演示

使用replace(string,FromValue,ToValue)

注意:这假设您的金额采用 ###,###.00 格式。如果没有,您可能会返回错误的数据。

with tb AS 
(SELECT '1,000.01' as "amount", '01-01-2022' as "date",'z' as "from merch" UNION ALL
SELECT '10,000.02' as "amount", '01-01-2022' as "date",'z' as "from merch" UNION ALL
SELECT '100,000.02' as "amount", '02-01-2022' as "date",'y' as "from merch")

SELECT date_trunc('day', to_date("date" , 'mm-dd-yyyy')) as day, 
"from merch", 
sum(CAST(replace("amount",',','') AS numeric)) as amount 
FROM tb 
GROUP BY 1,2 
ORDER BY 1,2;

带小数的演示 2

sum(CAST(replace("amount",',','') AS decimal(10,2))) as amount 

假设您已正确识别问题。

我(不是最近)在评论中发现了一个幽默的交流:

  • 如果你需要对它进行数学运算,请将其存储为数字
  • 如果你永远不会对其进行数学运算,请将其存储为字符串
  • 如果它需要同时执行这两项操作,则需要两列
  • 除了日期...将日期存储为日期期间并对其使用日期函数...而不是字符串 {shudder} 函数!
  • AutoIncrements 可以是数字(尽管我们不应该对它们进行数学运算)

@xQbert——此后将被称为“xQbert 剃刀”

demo

using replace(string,FromValue,ToValue)

NOTE: this assumes you have amount in a ###,###.00 format. if not you could return bad data.

with tb AS 
(SELECT '1,000.01' as "amount", '01-01-2022' as "date",'z' as "from merch" UNION ALL
SELECT '10,000.02' as "amount", '01-01-2022' as "date",'z' as "from merch" UNION ALL
SELECT '100,000.02' as "amount", '02-01-2022' as "date",'y' as "from merch")

SELECT date_trunc('day', to_date("date" , 'mm-dd-yyyy')) as day, 
"from merch", 
sum(CAST(replace("amount",',','') AS numeric)) as amount 
FROM tb 
GROUP BY 1,2 
ORDER BY 1,2;

Demo 2 with decimal

sum(CAST(replace("amount",',','') AS decimal(10,2))) as amount 

This is assuming you've identified the problem correctly.

I (not so recently) found an exchange in comments humorous:

  • If you EVER need to do math on it, store it as a number
  • If you'll NEVER do math on it store it as string
  • If it needs to do both, you need two columns
  • Except for dates... store dates as dates period and use date functions on them... not string {shudder} functions!
  • and AutoIncrements can be numbers (though we should never do math on them)

@xQbert -- this shall henceforth be referred to as "xQbert's razor"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文