【问题标题】:Numeric Data Types in Postgre SQLPostgresql 中的数值数据类型
【发布时间】:2019-01-04 18:13:06
【问题描述】:

当我为 postgre sql 编写代码时。我经常得到不需要的数字类型,尤其是 '0.12E3' 十进制形式。

例如,

with data as (
select created_at::timestamp::date as date,count(*)
from posts
group by created_at::timestamp::date
)
select date, count, sum(count) over
(order by date asc rows between unbounded preceding and current row) as total
from data

这段代码以科学计数法的形式给了我累积和的结果。

为什么即使代码中没有浮点数也会发生这种情况?我怎样才能使它正确或避免这种情况发生?

【问题讨论】:

    标签: sql postgresql sqldatatypes


    【解决方案1】:

    在使用 agragate 时需要使用别名——像这样:

    with data as (
      select created_at::timestamp::date as date, count(*) as day_count
      from posts
      group by created_at::timestamp::date
    )
    select date, day_count, 
           sum(day_count) over (order by date asc rows between unbounded preceding and current row) as total
    from data
    

    【讨论】:

    • 感谢您的回答霍根,但您是否介意告诉我发生这种情况的原因?
    猜你喜欢
    • 1970-01-01
    • 2012-03-29
    • 1970-01-01
    • 2017-01-01
    • 2015-11-16
    • 2017-09-29
    • 1970-01-01
    • 2010-10-25
    • 2014-03-25
    相关资源
    最近更新 更多