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

  • SEARCH
  • Home
  • 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 8534589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:13:49+00:00 2026-06-11T10:13:49+00:00

This is how the data is formatted: item_name | item_serial | sub_group | conc_stuff

  • 0

This is how the data is formatted:

item_name | item_serial | sub_group | conc_stuff | other_data | more_data
----------+-------------+-----------+------------+------------+-----------
foo        bar-01-a      widget      b-3          towel        dent
foo        bar-02-a      widget      a-1          42           mouse
foo        bar-03-a      widget      p-1          babel        dolphin
foo3       bar-21-f      widget      f-1          42           marvin
foo3       bar-22-f      widget      x-1          poetry       vogon

I have gotten the query to perform the way I want it to, the problem is I need to return more data.

SELECT item_name, 
       array_to_string(array_agg(conc_stuff), ',') as stuff 
FROM dataset 
WHERE some_selector = 'X' 
GROUP BY item_name 
ORDER BY item_name;

I have tried what seems simple yet logical to me:

SELECT item_name, 
       item_serial, 
       sub_group, 
       array_to_string(array_agg(conc_stuff), ',') as stuff 
FROM dataset 
WHERE some_selector = 'X' 
GROUP BY item_name 
ORDER BY item_name;

I need to return something that looks like this:

item_name | item_serial | sub_group |   stuff
----------+-------------+-----------+-------------
foo        bar-01-a      widget      a-1,b-3,p-1
foo3       bar-21-f      widget      f-1,x-1,g-5
foo6       bar-81-z      widget      r-1,d-8,w-0

instead of just this:

item_name |   stuff
----------+--------------
foo        a-1,b-3,p-1
foo3       f-1,x-1,g-5
foo6       r-1,d-8,w-0

When I try to add additional fields to the query, I get :

ERROR: column "stuff.item_serial" must appear in the GROUP BY clause or be used in an aggregate function

But I dont want to GROUP BY item_serial, I just want it to be returned with the aggregate, right?

Do I need to run a subquery? Im sure this is simple. The if there are multiple methods, which is most efficient? Some of the text I will be concatenating are coordinates (LARGE string of text).

  • 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-11T10:13:50+00:00Added an answer on June 11, 2026 at 10:13 am

    You need to pick one value for each name you have. You can’t have each name only returned once but the item_serial value multiple times.

    Picking one value out of those that are there for a group value is done through aggregate functions:

    SELECT item_name,
           min(item_serial),
           min(sub_group),
           array_to_string(array_agg(conc_stuff),',') AS stuff
    FROM dataset
    WHERE sub_group = 'widget'
    GROUP BY item_name
    ORDER BY item_name;
    

    This picks th4e “first” item_serial and sub_group for each name.

    If you want the last value, use max instead.

    But the important thing to understand is that you have to pick one value for the non-grouped columns. And you need to tell the DBMS exactly which one it should use by supplying an aggregate function which picks one value.

    SQLFiddle example: http://www.sqlfiddle.com/#!1/58009/1

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

Sidebar

Related Questions

I have an overridden ToString() method that I want to output formatted data. This
I've got some data sent via a server, formatted like this: [ {Username:user1@domain.com, id:1},
This data is for a holiday cottage's simple accommodation calendars. The data is simple
I have this data in cell A1: Majestic Properties Design District, LLC, Ste. 101,
If user insert this data 16 HOUSEHOLD STAND FAN AA070021A the output should be
I need to make this data variable global: $.ajax({ url: get_data.php, cache: false, dataType:
i Want to fetch this data using json decode. Please Help.. So far i
i have this data | car_name | year | price | Honda | 2011
I am accessing this data from a web server using NSURL, what I am
I have this data from a xml file: <?xml version=1.0 encoding=utf-8 ?> <words> <id>...</id>

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.