gpt4 book ai didi

sql - 在postgresql中显示表的唯一属性和总值

转载 作者:行者123 更新时间:2023-11-29 14:23:15 26 4
gpt4 key购买 nike

我有一个表,其中包含 stamp_type 和 amount 列,如下所示

                          stamp_type                          | amount
--------------------------------------------------------------+--------
GENERAL STAMP | 11000
GENERAL STAMP | 25000
COURT FEE STAMP | 9800
SPECIAL ADHESIVE | 721000
GENERAL STAMP | 125000
COURT FEE STAMP | 21000

现在我要显示如下:

    stamp_type                                                    | amount
GENERAL STAMP 161000
COURT FEE STAMP 30800
SPECIAL ADHESIVE 721000

TOTAL:912800

我无法显示唯一值。任何人都可以给我建议查询。我尝试使用 Distinct 但效果不佳。

最佳答案

select * from 
(
select 0 as srt, stamp_type, sum(amount) as SumAmount from t group by stamp_type
union
select 1 as srt, 'Total' as stamp_type, sum(amount) as SumAmount from t
) b order by srt

关于sql - 在postgresql中显示表的唯一属性和总值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13854556/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com