1
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 来为员工生成最大行数。

我不能使用左连接,因为我不知道哪个临时表可能有更多记录。

这是我当前数据的图像

当前查询

我的预期输出应该是这个图像

在此处输入图像描述

4

2 回答 2

2

您仍然可以使用 FULL JOIN,只需ISNULL在第二个连接条件上使用:

SELECT  RowNumber = COALESCE(t.RowNumber, e.RowNumber, d.RowNumber),
        EmployeeID = COALESCE(t.EmployeeID, e.EmployeeID, d.EmployeeID),
        t.FirstName,
        t.MiddleName,
        t.LastName,
        t.SSN,
        t.EmployeeCode,
        t.TaxName,
        t.Amount,
        t.GrossPay,
        t.CompanyId,
        e.EarningDescription,
        EarningAmount = e.Amount,
        d.DeductionDescription,
        DeductionAmount = d.Amount
FROM    @Tax t
        FULL JOIN @Earnings e
            ON t.EmployeeID = e.EmployeeID
            AND t.RowNumber = e.RowNumber
        FULL JOIN @Deductions D
            ON d.EmployeeID = ISNULL(t.EmployeeID, e.EmployeeID)
            AND d.RowNumber = ISNULL(t.RowNumber, e.RowNumber);

下面的工作示例(除了连接所需的列之外的所有列都是空的


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 @Tax  (RowNumber, EmployeeID)
VALUES (1, 1), (2, 1), (3, 1), (4, 1);

DECLARE @Earnings TABLE
(
   RowNumber int , 
   EmployeeId int,  
   EarningDescription nvarchar(50),  
   Amount decimal(18,2)
)
INSERT @Earnings  (RowNumber, EmployeeID)
VALUES (1, 1), (2, 1);

DECLARE @Deductions TABLE 
(
    RowNumber int , 
    EmployeeId int,  
    DeductionDescription nvarchar(50),  
    Amount decimal(18,2)
) 
INSERT @Deductions  (RowNumber, EmployeeID)
VALUES (1, 1), (2, 1), (3, 1), (4, 1), (5, 1), (6, 1);  


SELECT  RowNumber = COALESCE(t.RowNumber, e.RowNumber, d.RowNumber),
        EmployeeID = COALESCE(t.EmployeeID, e.EmployeeID, d.EmployeeID),
        t.FirstName,
        t.MiddleName,
        t.LastName,
        t.SSN,
        t.EmployeeCode,
        t.TaxName,
        t.Amount,
        t.GrossPay,
        t.CompanyId,
        e.EarningDescription,
        EarningAmount = e.Amount,
        d.DeductionDescription,
        DeductionAmount = d.Amount
FROM    @Tax t
        FULL JOIN @Earnings e
            ON t.EmployeeID = e.EmployeeID
            AND t.RowNumber = e.RowNumber
        FULL JOIN @Deductions D
            ON d.EmployeeID = ISNULL(t.EmployeeID, e.EmployeeID)
            AND d.RowNumber = ISNULL(t.RowNumber, e.RowNumber);
于 2013-10-30T11:32:47.280 回答
-1

我会做类似的事情

SELECT UserId, Amount, 1 TableType FROM @Tax
UNION
SELECT UserId, Amount, 2 TableType FROM @Earnings
UNION
SELECT UserId, Amount, 3 TableType FROM @Deductions

解释这种结构会更容易。

于 2013-10-30T11:25:04.467 回答