获得" 0"通过窗函数简单的数学函数

时间:2016-06-15 13:48:32

标签: sql-server mathematical-expressions

我需要找到产品在货架上的百分比。

所以我使用这个查询:

select 
    *, 
    cast((t1.CountProd / t1.SumProd)*100 as dec(12,10)) as asasa
from 
   (select 
        *,
        count(Nalichie) over (partition by ProductID) as CountProd,
        count(Nalichie) over() as SumProd
    from 
        [saleit_db_Danone].[OSA].[tbl_FullFCompetitive]) as t1

为什么我在最后一栏获得0

这是我的结果:

enter image description here

2 个答案:

答案 0 :(得分:3)

根据评论,您正在进行整数除法,然后转换为小数。试试这个:

select
    *,
    (CAST(CountProd AS DEC(16, 10)) / SumProd) * 100 AS asasa
from (
    select
        *,
        count(Nalichie) over (partition by ProductID) as CountProd,
        count(Nalichie) over() as SumProd
    FROM [saleit_db_Danone].[OSA].[tbl_FullFCompetitive]
) as t1

答案 1 :(得分:0)

此外,如果您转换为数字而不是小数,您将得到正确的答案。