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

时间:2012-12-13 06:52:53

标签: sql postgresql

我有一个列为stamp_type的列,其数量如下

                          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但dint work。

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

试试这段代码::

 select stamp_type, sum(amount) amount
    from tbl
    group by stamp_type
 union
 select 'Total:' stamp_type,sum(amount) amount
 from tbl;