Skip to content
Advertisement

SQL Join based on a column value

I want to Join different tables based on column value

if [dbo].[fin_FixedAssetRegister].TransactionType = ‘IOD’ then join [dbo].[sms_IssueOrderDetail]

if [dbo].[fin_FixedAssetRegister].TransactionType = ‘GRND’ then join [dbo].[sms_GoodsReceivedNoteDetail]

if [dbo].[fin_FixedAssetRegister].TransactionType = ‘OBL’ then join [dbo].[sms_OpeningBalanceSMSDetail]

Advertisement

Answer

You would typically use several left joins. Your question gives very little information about the underlying structures, but the idea is:

select ...
from fin_FixedAssetRegister r 
left join sms_IssueOrderDetail od 
    on od.<???> = r.<???> and r.TransactionType = 'IOD'
left join sms_GoodsReceivedNoteDetail nd 
    on nd.<???> = r.<???> and r.TransactionType = 'GRND'
left join sms_OpeningBalanceSMSDetail bd
    on bd.<???> = r.<???> and r.TransactionType = 'OBL'

THe <???> represent the names of the columns that are used to join the tables, which you did not provide.

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