declare @Tax Table
(
RowNumber int ,
FirstName nvarchar(50),
MiddleName nvarchar(50),
LastName nvarchar(50),
SSN nvarchar(50),
EmployeeCode nvarchar(50),
TaxName nvarchar(50),
Amount decimal(18,2),
GrossPay decimal(18,2),
CompanyId int,
EmployeeId int
)
INSERT into @Tax
select row_number() OVER (PARTITION BY E.EmployeeId order by E.EmployeeId ) as RowNumber,FirstName,MiddleName,LastName,SSN,EmployeeCode,TaxName,TC.Amount,dbo.[GrossIncomeCalculation](E.EmployeeId) as GrossPay
,E.CompanyId,E.EmployeeId
from Employee as E
cross apply (
select TT.*
from dbo.[StateFixedTaxesCalculation](dbo.[GrossIncomeCalculation](E.EmployeeId),E.EmployeeId,E.CompanyId,1006) as TT
where TT.EmployeeId=E.EmployeeId and E.CompanyId=1
) as TC
declare @Earnings Table
(
RowNumber int ,
EmployeeId int,
EarningDescription nvarchar(50),
Amount decimal(18,2)
)
INSERT into @Earnings
SELECT RowNumber,EC.EmployeeId,EarningDescription,Amount FROM Employee as E
CROSS APPLY
(
select EC.*
from dbo.[EarningCalculation](E.EmployeeId,E.CompanyId) as EC
WHERE E.CompanyId=1
) as EC
declare @Deductions Table
(
RowNumber int ,
EmployeeId int,
DeductionDescription nvarchar(50),
Amount decimal(18,2)
)
INSERT INTO @Deductions
SELECT RowNumber,EDD.EmployeeId,DeductionDescription,Amount FROM Employee as E
CROSS apply (
select ED.*
from dbo.[DeductionCalculation](E.EmployeeId,E.CompanyId) as ED
WHERE E.CompanyId=1
) as EDD
I want to join these three tables data in a such way that its always product maximum number of rows for employee according to which table have max records and other table if have no rows or min to that table show null in column values.
I cannot use left join because i don't know which temp table may have more records.
here is image of my current data
and my expected output should be look like this image