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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:23:20+00:00 2026-05-25T11:23:20+00:00

I have a very tricky question for you all Suppose I have the following

  • 0

I have a very tricky question for you all

Suppose I have the following tables

Table AccountType:

Id       Type
 1     Checking
 2      Savings 

Table Customer_Account:

Customer_Id      Account_Type_Id
    450                 1
    450                 2
    451                 1

The table Customer_Account contains a list of Customers’ Ids with their Account type Ids. The Account_Type_Id is a foreign key coming from the AccountType.Id.

Suppose in the Customer_Account table, a customer named Josh (id 450) can have both a checking and a savings account as shown above. I can output this customer with his id and type of account like so by having a LEFT JOIN twice on the AccountType table:

SELECT CustAccount.Customer_Id AS Id, Account1.Type AS Account_Type_1, Account2.Type AS Account_Type_2
FROM Customer_Account CustAccount
LEFT JOIN AccountType Account1
ON Account1.Id = CustAccount.Account_Type_Id
LEFT JOIN AccountType Account2
ON Account2.Id = CustAccount.Account_Type_Id

The output will be:

 Id        Account_Type_1         Account_Type_2           
450           Checking               Checking 
450           Savings                Savings
451           Checking               Checking   

What I’m trying to do is that if a customer like Josh (id 450) has both a checking and a savings account, I want to output the two rows of data above into one row like so:

 Id        Account_Type_1      Account_Type_2
450           Checking            Savings

And also, if a customer has only one type of account (like customer with id 451 here), I want only that type of account to appear under the corresponding column like so:

 Id        Account_Type_1      Account_Type_2
451           Checking            

Or if customer with id 451 had only a Savings account the ouput should be:

Id        Account_Type_1      Account_Type_2
451                              Savings

I want ‘Checking’ to only appear under Accoun_Type_1 and ‘Savings’ under Account_Type_2. If I do a GROUP BY CustAccount.Customer_Id, I get this:

 Id        Account_Type_1      Account_Type_2
450           Checking            Checking
451           Checking            Checking

Any help from any expert will be very appreciated.

Thanks.

  • 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-25T11:23:20+00:00Added an answer on May 25, 2026 at 11:23 am

    This looks like a straight-forward application for a FULL OUTER JOIN:

    SELECT COALESCE(ac1.id, ac2.id) AS id, ac1.Account_Type_1, ac2.Account_Type_2
      FROM (SELECT c.Customer_ID AS Id, t.Type AS Account_Type_1
              FROM Customer_Account AS c
              JOIN AccountType      AS t ON c.Account_Type_ID = t.ID AND t.ID = 1) AS ac1
      FULL OUTER JOIN
           (SELECT c.Customer_ID AS Id, t.Type AS Account_Type_2
              FROM Customer_Account AS c
              JOIN AccountType      AS t ON c.Account_Type_ID = t.ID AND t.ID = 2) AS ac2
        ON ac1.Id = ac2.Id;
    

    If your DBMS does not support FULL OUTER JOIN but does support LEFT OUTER JOIN, then you can use:

    SELECT ac0.id, ac1.Account_Type_1, ac2.Account_Type_2
      FROM (SELECT DISTINCT c.Customer_ID AS Id FROM Customer_Account AS c) AS ac0
      LEFT OUTER JOIN
           (SELECT c.Customer_ID AS Id, t.Type AS Account_Type_1
              FROM Customer_Account AS c
              JOIN AccountType      AS t ON c.Account_Type_ID = t.ID AND t.ID = 1) AS ac1
        ON ac0.id = ac1.id
      LEFT OUTER JOIN
           (SELECT c.Customer_ID AS Id, t.Type AS Account_Type_2
              FROM Customer_Account AS c
              JOIN AccountType      AS t ON c.Account_Type_ID = t.ID AND t.ID = 2) AS ac2
        ON ac0.Id = ac2.Id;
    

    The first sub-query generates the list of customer IDs that exist; the second generates the list for account type 1 (Checking); the third generates the list for account type 2 (Saving). The joins ensure that every account is properly recognized.

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

Sidebar

Related Questions

Ok, here is a tricky question: First, I have a very small C application
I have a very tricky problem going on with content inside a textarea on
We've got into a very tricky scenario in a project. We have used lot
Here is question for all those who have delivered WCF solutions to their clients/customers
This is a really tricky question to ask as its very hard to explain
I have a very tricky assignment. There is a webpage that uses frameset. I
I have a tricky question and I'm not sure if it is even possible
I have what I consider a bit of a tricky question. I am currently
I have a tricky question. As everyone knows, Apple clearly says that ONE UIViewController
i have very serious problem if any tech expert can help...thank you in advance..

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.