SQL Server连接

时间:2016-05-13 11:15:16

标签: sql sql-server tsql

查询1结果为87行:

SELECT        
    MIN(LEFT(Date_Last_Updated, 4)) AS Year_of_Transfer,  
    MIN(RIGHT(LEFT(Date_Last_Updated, 6), 2)) AS Month_of_Transfer, 
    MIN(RIGHT(LEFT(Date_Last_Updated, 8), 2)) AS Day_of_Transfer, 
    CRS, Loan_Code, 
    MIN(Date_Last_Updated) AS Min_Date_Last_Updated
FROM            
    dbo.Transfer_Final_Accounts_CO_SH
WHERE        
    (LEFT(Date_Last_Updated, 4) >= '2016') 
    AND (Subcategory = 'Transfer to Workout')
GROUP BY 
    CRS, Loan_Code
ORDER BY 
    Loan_Code

查询2导致3400000行:

SELECT        
    Date_Last_Updated, 
    SUM(NPL_Amount_Last_Quarter) AS NPL_Amount_Last_Quarter, 
    Loan_Code, 
    SUM([Total_Balance_€]) AS Total_Balance, 
    SUM(On_Balance_Amount_Last_Quarter) AS On_Balance_Last_Q, 
    SUM([Off_Balance_Amount_€]) AS Off_Balance_Last_Q, CRS, 
    [On_Balance_Amount_€], [Off_Balance_Amount_€], [Total_Balance_€], 
    NPL_Amount_Last_Quarter AS NPL_Amount_Last_Q, 
    [NPL_Amount_€], On_Balance_Amount_Last_Quarter, 
    Material_Bucket, Material_Bucket_Last_Quarter
FROM
    dbo.Transfer_Final_Accounts_COM_WORK
GROUP BY 
    CRS, Date_Last_Updated, Loan_Code, [On_Balance_Amount_€], 
    [Off_Balance_Amount_€], [Total_Balance_€], NPL_Amount_Last_Quarter, 
    [NPL_Amount_€], On_Balance_Amount_Last_Quarter, Material_Bucket, 
    Material_Bucket_Last_Quarter

查询3连接上面的2并产生0行:

SELECT        
    dbo.Transfer_to_Workout_Total_Balances.Total_Balance,  
    dbo.Transfer_to_Workout_Total_Balances.On_Balance_Last_Q, 
    dbo.Transfer_to_Workout_Total_Balances.Off_Balance_Last_Q,    
    dbo.Transfer_to_Workout_Total_Balances.NPL_Amount_Last_Quarter, 
    dbo.Transfer_to_workout_min_dates.Year_of_Transfer, 
    dbo.Transfer_to_workout_min_dates.Month_of_Transfer, 
    dbo.Transfer_to_workout_min_dates.Day_of_Transfer, 
    dbo.Transfer_to_workout_min_dates.CRS AS Expr1, 
    dbo.Transfer_to_workout_min_dates.Loan_Code AS Expr2, 
    dbo.Transfer_to_workout_min_dates.Min_Date_Last_Updated, 
    dbo.Transfer_to_Workout_Total_Balances.[On_Balance_Amount_€], 
    dbo.Transfer_to_Workout_Total_Balances.[Off_Balance_Amount_€], 
    dbo.Transfer_to_Workout_Total_Balances.[Total_Balance_€], 
    dbo.Transfer_to_Workout_Total_Balances.[NPL_Amount_€], 
    dbo.Transfer_to_Workout_Total_Balances.NPL_Amount_Last_Q, 
    dbo.Transfer_to_Workout_Total_Balances.On_Balance_Amount_Last_Quarter, 
    dbo.Transfer_to_Workout_Total_Balances.Material_Bucket, 
    dbo.Transfer_to_Workout_Total_Balances.Material_Bucket_Last_Quarter
FROM 
    dbo.Transfer_to_workout_min_dates 
INNER JOIN
    dbo.Transfer_to_Workout_Total_Balances ON dbo.Transfer_to_workout_min_dates.Loan_Code = dbo.Transfer_to_Workout_Total_Balances.Loan_Code 
                    AND dbo.Transfer_to_workout_min_dates.Min_Date_Last_Updated = dbo.Transfer_to_Workout_Total_Balances.Date_Last_Updated

连接有什么问题我为什么不从上面得到结果?

1 个答案:

答案 0 :(得分:0)

如果您的查询3没有返回一行,但下层表格dbo.Transfer_to_workout_min_datesdbo.Transfer_to_Workout_Total_Balances包含数据,则很可能不符合连接条件(ON)这些表格中的任何条目。

尝试删除其中一个术语(我将从Min_Date_Last_Updated一个开始)。它应该改变一些东西。然后在结果中检查两个字段。你会发现它们不同,因此永远不会平等。