简体   繁体   中英

PostgreSQL: Select particular column and its total row count

I will explain my problem with an sample example

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

I need to select only column fld and its total count of each column( fld ) in descending order

Expected Result :

fld count
---------
77  3
55  2
11  1
65  1
select fld
      ,count(fld) rw_count 
from foo 
group by fld 
order by rw_count desc

Group By

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM