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
我想以这样一种方式加入这三个表数据,它总是根据哪个表有最大记录和其他表(如果该表没有行或该表的最小值)在列值中显示 null 来为员工生成最大行数。
我不能使用左连接,因为我不知道哪个临时表可能有更多记录。
这是我当前数据的图像
我的预期输出应该是这个图像