Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8119777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:54:26+00:00 2026-06-06T04:54:26+00:00

I have two columns A and B, and would like to get a list

  • 0

I have two columns A and B, and would like to get a list of items(and their counts) in column B grouped by items in column A, and create a new table with the information. So the new table will look something like:

newCol1 | newCol2
--------+--------
a1,     | b1:3,b4:1,b7:11
a2,     | b2:1,b3:5,b4:3,b8:2

…and so forth. (delimiters can be anything, though. If concatenating item and count is not possible, I could also have one column with a list of items and another column with a list of counts separated by a delimiter.)

I can do this in Java by first getting all the items and storing them in a map with count updates, and then update the new table, but I was wondering if there’s any way to do this in PostgreSQL (perhaps by writing a function).

I’ve looked at array function in PostgreSQL but didn’t get far. Any pointers as well as suggestions for storing such data would be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T04:54:28+00:00Added an answer on June 6, 2026 at 4:54 am

    a and b are of type text, I assume.

    SELECT a, array_agg(bs) AS b_list
    FROM  (
        SELECT a, b || ':' || count(*) AS bs  -- coerced to text automatically
        FROM   tbl
        GROUP  BY a, b
        ORDER  BY a, b   -- to sort b_list in the result
        ) x
    GROUP  BY a;
    

    Or use string_agg() as @a_horse demonstrates to get a string instead of an array as result.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a two column layout: http://jsfiddle.net/KqQ42/1/ Now I would like that the left
I have two columns that are joined together on certain criteria, but I would
I have two columns. Column E extends up to 99504 (values) and column I
I have two columns, each with many rows/divs. I'm trying to get jQuery UI
I have two columns in my table and the rows are created dynamically. <table
I have a table with two columns - id and desc . Is it
I have a list L of data frames with two columns each...a key, and
I have an excel spreadsheet with two columns. The first column is a label
I have MySQL database with two tables: users and items. They look like this:
I would like to compare if two columns are equal in the where clause.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.