Skip to content
Advertisement

Create group column with values based on join [closed]

I have two tables 1) a customer table 2)Account table. I want to see what accounts are primary and which are secondary accounts.

In one table I have accountRowId and AccountNumber. In the other table I have PrimaryAccountRowId and SecondaryAccountRowId.

For my output I would like to have all AccountNumbers in one column with all the AccountRelationship(primary or seconday) in another column beside each AccountNumber.

In order to join table, for PrimaryAccounts I would join AccountRowId on PrimaryAccountRowId and for secondary Accounts I would just flip flop and instead of having the primaryAccountRowId it would be SecondaryAccountRowId.

My Account table:

Customer table:

What I want to see my table look like

Please provide some helpful logic/code of how I would achieve these results. Thanks

Advertisement

Answer

This can be achieved using a left join between the two tables. Basically by checking if the accountRowid exists in the column primaryAccountRowId from customer table you would know if the account_number is a primary, similarly the logic for secondary as well

eg:

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement