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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:14:57+00:00 2026-06-13T09:14:57+00:00

I have the following tables : User_Group id group_id group_type ———————— 1 100 A

  • 0

I have the following tables :

User_Group

id  group_id  group_type
------------------------
1   100       A
1   100       B
2   101       B
2   102       A

Group_A

id   name
---------
100  A
101  B
102  C

Group_B

id   name
---------
100  D
101  E
102  F

I want the group names of all users (using array.agg()). We have to get the group name from group A if the user’s group type = A and from group B if the user’s group type = B. The result should be :

userid  groups
--------------
1       A,D
2       E,C

I have created a fiddle for this, and given a solution using union of 2 separate queries. Can it be done without the union, something in which I can decide on which table to pick the group name from with a single join of user_groups, group_A and group_B ?

  • 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-13T09:14:59+00:00Added an answer on June 13, 2026 at 9:14 am
    select ug.id, array_agg(
        case ug.group_type 
            when 'A' then g_a.name 
            when 'B' then g_b.name 
            else 'N/A' 
        end)
    from user_groups ug
    left outer join group_A g_a on ug.group_id = g_a.id
    left outer join group_B g_b on ug.group_id = g_b.id
    group by ug.id
    

    SQL Fiddle Example

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

Sidebar

Related Questions

I have the following tables TUser (user_id,firstname,lastname) PK user_id TGroup (group_id,name) PK group_id TUsersGroups
I have user and group tables set up as the following. users id int
I have the following tables: Table: user_groups (many-to-many) user_id (int) group_id (varchar) Table: profile_groups
I have the following two tables: auth_user id username is_active (boolean) ... useprofile_userprofile id
I have following tables questions -> id, question_data, user_id users -> id, fname, lname
I have 3 tables with the following columns : * users : user_id *
I've got the following problem: I have two tables: (simplified) +--------+ +-----------+ | User
I have the following tables in a PostgreSQL: Categories | Locations | Checkins |
I have the following tables - groups, contacts, contacts_groups (habtm join table) groups &
After doing some joins and stuff I have table in the following format name

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.