Table A:
- AccountID (PK, int, not null)
- Agent (varchar(50), null)
- AccountType (varchar(50), null)
Sample Table:
╔═══════════╦════════════════╦═══════╗
║ AccountID ║ AccountType ║ Agent ║
╠═══════════╬════════════════╬═══════╣
║ 413393 ║ Invoice ║ A ║
║ 417811 ║ Credit ║ NULL ║
╚═══════════╩════════════════╩═══════╝
Table B:
- AccountID(int, not null) - This is the foreign key, and I am extracting the data from both the tables based on matching AccountID records.
- Ref_AccountID (int, null)
Sample Table:
╔═══════════╦════════════════╦
║ AccountID ║ Ref_AccountID ║
╠═══════════╬════════════════╬
║ 413393 ║ NULL ║
║ 417811 ║ 413393 ║
╚═══════════╩════════════════╩
Description: If the AccountType is invoice, then there would be a Agent associated with it. From Table A, you can see that it is associated with Agent A.
Current Output:
╔═══════════╦═════════════╦═══════════════╦═══════╗
║ AccountID ║ AccountType ║ Ref_AccountID ║ Agent ║
╠═══════════╬═════════════╬═══════════════╬═══════╣
║ 413393 ║ Invoice ║ NULL ║ A ║
║ 417811 ║ Credit ║ 413393 ║ NULL ║
╚═══════════╩═════════════╩═══════════════╩═══════╝
Expected Output:
╔═══════════╦═════════════╦═══════════════╦═══════╗
║ AccountID ║ AccountType ║ Ref_AccountID ║ Agent ║
╠═══════════╬═════════════╬═══════════════╬═══════╣
║ 413393 ║ Invoice ║ NULL ║ A ║
║ 417811 ║ Credit ║ 413393 ║ A ║
╚═══════════╩═════════════╩═══════════════╩═══════╝
The Agent should be displayed based on the Ref_AccountID. In this example, the Ref_AccountID is 413393, and for this AccountID in table A, the Agent is "A".
Thanks