Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我的表中有一个 qty 列,我正在尝试对这些值求和。字段类型是 varchar(20)。下面的数量之和应该正好加起来为 0。它是负小数和正小数的混合。
展品A(截图)
当我执行下面的求和时,我得到一吨小数而不是 0。我假设这是一个数据类型问题。解决此问题的最佳方法是什么?
展品 B(截图)
您不应该将数字数据存储为字符串,但如果这样做,则需要cast()对其应用SUM()聚合:
cast()
SUM()
SELECT SUM(CAST(yourcolumn AS DECIMAL(10, 2))) FROM yourtable
所以您的查询将是:
select sum(cast(qty as DECIMAL(10, 2))) from inventory i where i.refDocNum = 485 and i.refApp = 'WO' and i.type in (20, 21)