PostgreSQL:选择特定列及其总行数

时间:2014-12-20 10:55:11

标签: sql postgresql select

我将用示例

解释我的问题
create table foo(id int,idx int,idy int,fld int,fldx varchar);

insert into foo values (1,2,3,55,'AA'),(2,3,4,77,'AB'),(3,4,8,55,'AX'),(9,10,15,77,'AR'),
                       (3,4,8,11,'AX'),(3,4,8,65,'AX'),(3,4,8,77,'AX');




   id,idx,idy, fld,fldx
    1 2   3    55  AA
    2 3   4    77  AB
    3 4   8    55  AX
    9 10  15   77  AR
    3 4   8    11  AX
    3 4   8    65  AX
    3 4   8    77  AX

我需要按降序选择列fld及其每个列的总计数(fld

预期结果:

fld count
---------
77  3
55  2
11  1
65  1

2 个答案:

答案 0 :(得分:0)

select fld
      ,count(fld) rw_count 
from foo 
group by fld 
order by rw_count desc

Group By

答案 1 :(得分:0)

select fld,count(*) from foo group by 1 order by 2 desc ;