获取与列sql的整个总和相关的列的百分比

时间:2016-10-13 15:16:52

标签: sql

我正在尝试获取与特定产品NDC的总体预测相关的年度预测列的百分比。我有下面的代码,但它每次都返回值0。有任何想法吗?

     select 
    f.ProductNDC ProductNDC,
    g.IMDSC1 ItemDesc,
    g.IMDSC2 ItemDesc2,
    cf.DirectUsage YearlyForecast,
    (cf.DirectUsage  / (SUM(cf.DirectUsage) over(partition by f.ProductNDC))),
    cd.CustomerNum CustomerNumber,
    cd.CustomerName CustomerName,
    cf.EffectiveDate EffectiveDate,
    cf.EndDate EndDate
From Forecast_Dev.dbo.Forecast f
    join Forecast_Dev.dbo.CustomerForecast cf 
        on cf.ForecastID = f.ForecastID
    join Forecast_Dev.dbo.CustomerDetail cd
        on cd.CustomerID = cf.CustomerID
    join JDE_DEVELOPMENT.TESTDTA.F4101 g
        on ltrim(rtrim(g.imlitm))= f.ProductNDC

1 个答案:

答案 0 :(得分:1)

某些数据库执行整数除法,这意味着两个整数的比率是整数。所以,1/2 = 0,而不是0.5。

因此,将其转换为某种浮点数:

(cf.DirectUsage * 1.0 / nullif(SUM(cf.DirectUsage) over(partition by f.ProductNDC))), 0),