skip to Main Content

Table has two columns which concatenate as ‘ab’. I’d like a count of the unique values from the concatenated columns ‘ab’, here is what I have so far;

initial_db=> SELECT concat(street_num, street_name) AS ab FROM recs limit 5;
     ab     
------------
 21ESSEX RD
 21ESSEX RD
 5AUSTIN ST
 5AUSTIN ST
 5AUSTIN ST
(5 rows)

initial_db=>

What I’d like is something like this;

count       ab
-----       -----
2           21ESSEX RD
3           5AUSTIN ST

Any help would be appreciated!

2

Answers


  1. SELECT count(*) as count,
    concat(street_num, street_name) AS ab
    FROM recs
    GROUP BY concat(street_num, street_name)
    
    Login or Signup to reply.
  2. You can use group by.
    Here a detailed explanation how it works:
    https://www.postgresqltutorial.com/postgresql-tutorial/postgresql-group-by/

    SELECT COUNT(*) as count, CONCAT(street_num, street_name) AS ab
    FROM recs
    GROUP BY ab;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search