Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 与临时表的完全外部联接_Sql_Sql Server_Join_Full Outer Join - Fatal编程技术网

Sql 与临时表的完全外部联接

Sql 与临时表的完全外部联接,sql,sql-server,join,full-outer-join,Sql,Sql Server,Join,Full Outer Join,我希望以这样的方式连接这三个表数据,即它总是根据哪个表有max记录和其他表(如果没有行或该表的min)的employee的乘积最大行数在列值中显示null 我无法使用左联接,因为我不知道哪个临时表可能有更多记录。 这是我当前数据的图像 我的预期输出应该是这样的图像 我会做类似的事情 declare @Tax Table ( RowNumber int , FirstName nvarchar(50), MiddleName nvarchar(50), LastN

我希望以这样的方式连接这三个表数据,即它总是根据哪个表有max记录和其他表(如果没有行或该表的min)的employee的乘积最大行数在列值中显示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 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

解释这样的结构会更容易。

我会这样做

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

解释这样的结构会更容易。

您仍然可以使用完全联接,只需在第二个联接条件上使用
ISNULL

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

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



您仍然可以使用完全联接,只需在第二个联接条件下使用
ISNULL

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

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



似乎你应该使用并集,而不是联接。似乎你应该使用并集,而不是联接。