I want ,when Opening_value and Opening_Date exist in table #tbl_Account_L_Four ,then opening_Value will get from #tbl_Account_L_Four and after opening_date value will get from #tbl_Transection table on wards,if Opening_Date is null then data will get from #tbl_transection table.
as you can see below image ,in which opening_Value of against Level_Four_ID(1222) in a table #tbl_Account_L_Four exist.
Create table #tbl_Account_L_Four (Level_Four_ID int,Level_Four_Name
varchar(50),Opening_Value decimal(10,2),Opening_Date date)
Create table #tbl_trans_type (Trans_Type_ID int,trans_type_name varchar(50))
Create table #tbl_transection (Trans_ID int,Level_Four_ID_C int,Level_Four_ID_D int,Trans_Amount Decimal(10,2),Trans_date date,Trans_Type_ID int,sell_ID int)
INSERT INTO #tbl_Account_L_Four VALUES(1231,'Abdul Rauf',null,null)
INSERT INTO #tbl_Account_L_Four VALUES(1222,'Cheque In Hand',5000,'2021-01-18')
INSERT INTO #tbl_Account_L_Four VALUES(1215,'MBL 833968',null,null)
insert into #tbl_trans_type VALUES(1,'Online')
insert into #tbl_trans_type VALUES(2,'Cheque')
insert into #tbl_trans_type VALUES(3,'Deposite')
insert into #tbl_trans_type VALUES(4,'Tranfer')
insert into #tbl_trans_type VALUES(5,'Return')
INSERT INTO #tbl_transection VALUES(1,1231,1222,50000,'2021-01-18',2,null)
INSERT INTO #tbl_transection VALUES(2,1231,1222,50000,'2021-01-18',2,null)
INSERT INTO #tbl_transection VALUES(3,1222,1215,44444,'2021-01-18',3,null)
INSERT INTO #tbl_transection VALUES(4,1215,1222,44444,'2021-01-18',5,null)
INSERT INTO #tbl_transection VALUES(5,1222,1231,44444,'2021-01-19',2,null)
INSERT INTO #tbl_transection VALUES(6,1231,1222,500,'2021-01-20',2,null)
Note : Output filter on date .secondly 1231 ID which is in tbl_Account_L_Four .