【问题标题】:Where to place numeric in this SQL SELECT query在此 SQL SELECT 查询中放置数字的位置
【发布时间】:2022-01-04 23:59:55
【问题描述】:

我想知道 NUMERIC(5,2) 相对于我的 avg 的正确位置在下面的 SQL 查询中:

select distinct 
    b.name, avg(c.price) over (partition by b.name) as average_price
from     
    Catalog a 
join 
    books b on (a.book_id = b.id)
join 
    movies c on (a.movie_id = c.id)
where    
    c.price is not null 
    and a.record >= 2
group by 
    b.name, c.price, average_price

我已经尝试过任何地方的感觉!

提前致谢。

【问题讨论】:

    标签: sql group-by average numeric partition


    【解决方案1】:

    我只会投平均数:

    select distinct b.name,
           cast(avg(c.price) over (partition by b.name) as numeric(5,2)) as average_price
    from ...
    

    这假设您希望使用 price 列中的任何原始精度进行平均计算,并且您只想将输出查看为 numeric(5,2)

    【讨论】:

    • 嗯,当我这样做时,我得到了一个语法错误
    • 试用更新版本。
    猜你喜欢
    • 2013-09-14
    • 2011-03-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-25
    • 2013-09-08
    • 1970-01-01
    • 2015-04-27
    • 1970-01-01
    相关资源
    最近更新 更多