不确定在这里我在做什么错。每当我运行此代码时,我都会不断收到此错误
信息8120,第16级,州1,第55行
选择列表中的“ Vendors.VendorName”列无效,因为它既不包含在聚合函数中,也不包含在GROUP BY子句中。
这是代码
select
VendorName, VendorContactFName, VendorContactLName,
sum(InvoiceTotal - CreditTotal- PaymentTotal) as BalanceDue
from
Vendors
full join
Invoices on Vendors.VendorID = Invoices.VendorID
where
InvoiceTotal - CreditTotal- PaymentTotal > 0
order by
BalanceDue desc
答案 0 :(得分:2)
这可能是您想要的:
select v.VendorName, v.VendorContactFName, v.VendorContactLName,
sum(i.InvoiceTotal - i.CreditTotal - i.PaymentTotal) as BalanceDue
From Vendors v join
Invoices i
on v.VendorID = i.VendorID
where i.InvoiceTotal - i.CreditTotal - i.PaymentTotal > 0
group by v.VendorName, v.VendorContactFName, v.VendorContactLName
order by BalanceDue desc;
注意:
sum()
中有select
,但没有group by
。这是主要问题。full join
不适合。实际上,您需要一个inner join
。 where
子句要求invoice
表必须匹配。 group by
要求vendor
表必须匹配。