为什么这个字段没有显示在结果中?

时间:2012-07-05 16:08:16

标签: tsql

当我运行下面的选项时,我在结果集中没有得到Field3,为什么?

    Select 
        a.Field1,
        a.Field2,
        a.Field3,
        sum(IsNull(a.Field4, 0)) AS SomeAlias1,
        a.SomeField5,
        a.SomeField6,
        a.SomeField7
From SomeTable a
INNER JOIN SomeView1 v on v.au = a.au
inner join (select Username, House from Users userBuildings where UserName = @UserName) as userHouses on userHouses.au = a.au
WHERE
(((where claus logic here....

Group BY    a.Field1, 
        a.Field2, 
        a.SomeAlias1, 
        a.Field3,
        a.Field4,
        a.Field5,
        a.Field6,
        a.Fielf7
)

Select 
    transBudget.Field1,
    transBudget.Field2,
    transDiscount.Field4,
    ... some other fields...
    IsNull(transDiscount.Actual, 0) - IsNull(transBudget.Actual, 0) AS Variance 
from (Select * from Transactdions Where TransDesc = 'Budget') AS transBudget
    FULL OUTER JOIN
    (Select * from Transactions Where TransDesc = 'Discount') AS transDiscount
        ON transBudget.Market = transDiscount.Market AND transBudget.SubMarket = transDiscount.SubMarket

由于某种原因,我看到除了Field3之外的所有领域,这超出了我的预测。

1 个答案:

答案 0 :(得分:2)

在查询的第二部分中,您缺少字段3.

Select 
    transBudget.Field1,
    transBudget.Field2,
    transDiscount.Field4,
    ... some other fields...
    IsNull(transDiscount.Actual, 0)

您似乎在那里有两个单独的SQL查询。第一个包含Field3,但第二个不包含。