【问题标题】:Get Decimal Places for 10/7获取 10/7 的小数位数
【发布时间】:2022-01-18 02:17:36
【问题描述】:

我正在尝试进行计算,我想根据餐厅是每周 5 天还是 7 天,根据助理和主教练的数量更新理想时间。当我使用 10/7 或 20/7 进行计算时,它分别作为 1 和 2。我目前正在使用 trunc 但我尝试过使用 cast :: decimal(10,6) 等但它不起作用。

select
       a.entity,
       a.store_name,
       a.order_date,
       a.daily_ideal_hours,
       a.daily_ideal_hours - (case when b.days_open like '%Weekdays%' then ((c.total_acs*(20/5) + c.total_hcs*(10/5)))
                                   when b.days_open like '%All%' then ((c.total_acs * trunc(20/7,10)) + (c.total_hcs * trunc(10/7,10))) end) as updated_value
from scorecards_ideal_labor_hours as a
left join days_store_open as b
    on a.entity = b.entity
left join hc_ac_data as c
    on a.entity = c.entity_id
    and a.order_date = c.report_date
where a.order_date between '2021-12-06' and '2021-12-12'
and a.entity = 66
order by a.order_date desc;

我该如何解决这个问题?

【问题讨论】:

  • 使用 7.0 以便它使用浮点计算。

标签: sql amazon-redshift decimal integer-division


【解决方案1】:

这里不需要使用 trunc 函数。 只需将分子和分母都输入浮点数。 这将给出带有小数位的最终答案,没有任何四舍五入:
select (10::float)/(7::float);
这给出了答案: 1.4285714285714286

如果您只需要将最终答案四舍五入:
select trunc((10::float)/(7::float),2);

【讨论】:

    猜你喜欢
    • 2011-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-12
    • 2010-11-12
    • 2020-04-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多