SQL Select语句SubSelect工作不正确

时间:2013-11-20 13:53:05

标签: sql select join subquery

我在两个seperare列中有两个SQL Select Queries。我刚刚编写的代码确实如此,但它也提供了2个空值。这是我的代码:

Select a.Budget, b.Actual_Income FROM

(Select sum(subscriptions.Bill_Amount) as Budget
From imis.dbo.Name Name INNER JOIN imis.dbo.Subscriptions Subscriptions ON     Name.ID=Subscriptions.ID
Where Member_Type = 'MM' and Name.Status = 'a' and Product_Code = 'Annual' and Subscriptions.Status = 'a') AS a

FULL JOIN

(SELECT sum(Amount * -1) as Actual_Income
FROM   imis.dbo.Name Name INNER JOIN imis.dbo.Trans Trans ON Name.ID=Trans.BT_ID
WHERE  PRODUCT_CODE ='ANNUAL' and Transaction_Date >= '21/10/2013' and Batch_Num <>    'DD131031-3') AS b
 ON a.Budget = b.Actual_Income

这是它返回的内容

Budget         Actual_Income
6367005.00      NULL
NULL            665712.37

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

你有一个完整的加入。将其更改为INNER JOIN。

点击此链接可更好地了解SQL JOINS

如果您的结果如下:

Budget         Actual_Income
6367005.00      665712.37

然后你可以做subselect或aggregate:

子选择:

Select (Select sum(subscriptions.Bill_Amount) as Budget
From imis.dbo.Name Name INNER JOIN imis.dbo.Subscriptions Subscriptions ON     Name.ID=Subscriptions.ID
Where Member_Type = 'MM' and Name.Status = 'a' and Product_Code = 'Annual' and Subscriptions.Status = 'a') Budget, 
(SELECT sum(Amount * -1) as Actual_Income
FROM   imis.dbo.Name Name INNER JOIN imis.dbo.Trans Trans ON Name.ID=Trans.BT_ID
WHERE  PRODUCT_CODE ='ANNUAL' and Transaction_Date >= '21/10/2013' and Batch_Num <>    'DD131031-3') as Actual_Income 

骨料:

Select MAX(a.Budget), MAX(b.Actual_Income) FROM

(Select sum(subscriptions.Bill_Amount) as Budget
From imis.dbo.Name Name INNER JOIN imis.dbo.Subscriptions Subscriptions ON     Name.ID=Subscriptions.ID
Where Member_Type = 'MM' and Name.Status = 'a' and Product_Code = 'Annual' and Subscriptions.Status = 'a') AS a

FULL JOIN

(SELECT sum(Amount * -1) as Actual_Income
FROM   imis.dbo.Name Name INNER JOIN imis.dbo.Trans Trans ON Name.ID=Trans.BT_ID
WHERE  PRODUCT_CODE ='ANNUAL' and Transaction_Date >= '21/10/2013' and Batch_Num <>    'DD131031-3') AS b