带有临时表的FULL OUTER JOIN

时间:2013-10-30 11:12:46

标签: sql sql-server join full-outer-join

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

我希望以这样的方式连接这三个表数据:根据哪个表具有最大记录,它始终是产品最大行数;如果没有行或min表,则其他表在列值中显示null。

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

这是我当前数据的图像

current query

我的预期输出应该看起来像这个图像

enter image description here

2 个答案:

答案 0 :(得分: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);

下面的工作示例(除连接所需的列之外的所有列都为null,但


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);

答案 1 :(得分:-1)

我会做像

这样的事情
SELECT UserId, Amount, 1 TableType FROM @Tax
UNION
SELECT UserId, Amount, 2 TableType FROM @Earnings
UNION
SELECT UserId, Amount, 3 TableType FROM @Deductions

更容易解释这种结构。