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 8780027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:56:31+00:00 2026-06-13T19:56:31+00:00

maybe someone can help me out with a postgres query. the table structure looks

  • 0

maybe someone can help me out with a postgres query.

the table structure looks like this

  nummer   nachname   vorname   cash
 +-------+----------+----------+------+
   2       Bert       Brecht    0,758
   2       Harry      Belafonte 1,568
   3       Elvis      Presley   0,357
   4       Mark       Twain     1,555
   4       Ella       Fitz      0,333
   …

How can I coalesce the fields where “nummer” are the same and sum the cash values?
My output should look like this:

   2       Bert, Brecht      2,326
           Harry, Belafonte 
   3       Elvis, Presley    0,357
   4       Mark, Twain       1,888
           Ella, Fitz   

I think the part to coalesce should work something like this:

 array_to_string(array_agg(nachname|| ', ' ||coalesce(vorname, '')), '<br />') as name,   

Thanks for any help,

tony

  • 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-13T19:56:33+00:00Added an answer on June 13, 2026 at 7:56 pm
    SELECT
      nummer, 
      string_agg(nachname||CASE WHEN vorname IS NULL THEN '' ELSE ', '||vorname END, E'\n') AS name,
      sum(cash) AS total_cash
    FROM Table1
    GROUP BY nummer;
    

    See this SQLFiddle; note that it doesn’t display the newline characters between names, but they’re still there.

    The CASE statement is used instead of coalesce so you don’t have a trailing comma on entries with a last name but no first name. If you want a trailing comma, use format('%s, %s',vorname,nachname) instead and avoid all that ugly string concatenation business:

    SELECT
      nummer, string_agg(format('%s, %s', nachname, vorname), E'\n'),
      sum(cash) AS total_cash
    FROM Table1
    GROUP BY nummer;
    

    If string_agg doesn’t work, get a newer PostgreSQL, or mention the version in your questions so it’s clear you’re using an obsolete version. The query is trivially rewritten to use array_to_string and array_agg anyway.

    If you’re asking how to sum numbers that’re actually represented as text strings like 1,2345 in the database: don’t do that. Fix your schema. Format numbers on input and output instead, store them as numeric, float8, integer, … whatever the appropriate numeric type for the job is.

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

Sidebar

Related Questions

I would like to know some way to do this, maybe someone can help
I'm having much trouble figuring this out, so maybe someone can help me. This
I can't figure this out so maybe someone here can help me. I have
Okay, the specs have changed on this one somewhat. Maybe someone can help me
Maybe someone can help me out. I have created a simple web user control,
I'm going slidely mad over here, maybe someone can help me figure out what's
Maybe someone somewhere had the same issue and can help me out. I'm using
maybe someone can help me with a regular expression. This one wont work: public
I am struggling really hard to understand this behavior so maybe someone can shed
Please can anyone help me out with this problem? This code below works perfectly

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.