使用Postgresql中的计数和总和选择子查询

问题描述 投票:-1回答:1

大家好,请帮助我,

我有问题,我想从数据库postgreesql中获取数据

数据库架构:认证名称:au_accreditation

此数据库表

unit_code          unit_score
    1                3
    1                3
    1                4
    1                4
    2                1
    2                3
    2                3
    2                5

所以,我想基于单位代码对每个数据求和,这样我就可以得到表数据了

unit_code           unit_score
    1                   14               -> sum unit score from unit code : 1
    2                   12               -> sum unit score from unit code : 2
sql postgresql postgresql-9.5
1个回答
0
投票
select 
    unit_code,
    sum(CAST(unit_score AS int)) as unit_score
From <table>
group by unit_code
© www.soinside.com 2019 - 2024. All rights reserved.