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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:48:16+00:00 2026-05-24T21:48:16+00:00

Here is my mock up data: Groups: MAINGROUP SUBGROUP 1 A 2 A 3

  • 0

Here is my mock up data:

Groups:

MAINGROUP SUBGROUP
1         A
2         A
3         A 
1         B
2         B
3         C
1         D
2         D
3         D
2         E
3         E

I’m trying to write a query that will return me one row per SUBGROUP, with one field for each MAINGROUP displaying the group number. If the subgroup does not have a matching group, I want that group field to be NULL.

Here’s what I want:

Subgroup  G1  G2  G3
A         1   2   3
B         1   2 
C                 3

Here’s my query:

SELECT 
    g.subgroup AS Subgroup, 
    1.maingroup AS G1, 
    2.maingroup AS G2, 
    3.maingroup AS G3, 
FROM Groups g
LEFT JOIN(
        SELECT maingroup, subgroup
        FROM Groups
        WHERE maingroup = 1
        ) 1
        ON g.subgroup=1.subgroup
        AND g.maingroup=1.maingroup
LEFT JOIN(
        SELECT maingroup, subgroup
        FROM Groups
        WHERE maingroup = 2
        ) 2
        ON g.subgroup=2.subgroup
        AND g.maingroup=2.maingroup
LEFT JOIN(
        SELECT maingroup, subgroup
        FROM Groups
        WHERE maingroup = 3
        ) 3
        ON g.subgroup=3.subgroup
        AND g.maingroup=3.maingroup
WHERE g.subgroup IN ('A','B','C')

And here’s what I’m getting:

Subgroup  G1  G2  G3
A         1     
A             2 
A                 3
B         1     
B             2 
B
C           
C           
C                 3

Any help and advice much 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-05-24T21:48:16+00:00Added an answer on May 24, 2026 at 9:48 pm

    Try this:

    SELECT g.subgroup AS Subgroup, 
        mg1.maingroup AS G1, 
        mg2.maingroup AS G2, 
        mg3.maingroup AS G3
    FROM (SELECT DISTINCT subgroup FROM Groups) g
    LEFT JOIN Groups mg1 ON mg1.maingroup = 1 AND mg1.subgroup = g.subgroup
    LEFT JOIN Groups mg2 ON mg2.maingroup = 2 AND mg2.subgroup = g.subgroup
    LEFT JOIN Groups mg3 ON mg3.maingroup = 3 AND mg3.subgroup = g.subgroup
    WHERE g.subgroup IN ('A','B','C');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's my issue, I'd like to mock a class that creates a thread at
Here is the issue I am having: I have a large query that needs
Here is the scenario: I'm writing an app that will watch for any changes
I have a core data application that uses an NSPredicate in one of its
This is probably a noob question that I will get slated for but here
I'm trying to do a mock-up in Matlab on some image data in a
I am trying to unit test a thin data access layer that I've written.
Here's a basic regex technique that I've never managed to remember. Let's say I'm
Here's my scenario - I have an SSIS job that depends on another prior
Here's a coding problem for those that like this kind of thing. Let's see

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.