需要帮助找出我的代码有什么问题

时间:2017-07-16 18:38:17

标签: sql

  1. 我想要一个结果集,它会显示供应商分组的发票。
  2. 我想要一个结果集,它会向我显示供应商以及从最少开具发票金额到大部分开具发票金额的发票总金额。
  3. 我想要一个结果集,显示供应商以及总计金额超过$ 2000.00的已开票总额。
  4. Select * from Invoices group by VendorID;
    
    Select Vendors.VendorID,Vendors.VendorName,sum(Invoices.InvoiceTotal) 
    from Vendors inner join Invoices on Vendors.VendorID = Invoices.VendorID group by Vendors.VendorID order by sum(Invoices.InvoiceTotal);
    
    
    Select Vendors.VendorID,Vendors.VendorName,sum(Invoices.InvoiceTotal) 
    from Vendors inner join Invoices on Vendors.VendorID = Invoices.VendorID group by Vendors.VendorID having sum(Invoices.InvoiceTotal) > 2000;
    

    此代码给出了同样的错误:

      

    Msg 8120,Level 16,State 1,Line 2

         

    列'Invoices.InvoiceID'在选择列表中无效,因为它   不包含在聚合函数或GROUP BY中   子句。

         

    Msg 8120,Level 16,State 1,Line 4

         

    列'Vendors.VendorName'在选择列表中无效,因为它   不包含在聚合函数或GROUP BY中   子句。

         

    Msg 8120,Level 16,State 1,Line 8

         

    列'Vendors.VendorName'在选择列表中无效,因为它   不包含在聚合函数或GROUP BY中   子句。

2 个答案:

答案 0 :(得分:0)

尝试制作联接的子查询

Select * from 
(Select Vendors.VendorID, Vendors.VendorName, sum(Invoices.InvoiceTotal) as total 
from Vendors inner join Invoices on Vendors.VendorID = Invoices.VendorID) 
as T1 group by T1.VendorID order by T1.total;

答案 1 :(得分:0)

#1:对我来说按供应商分组可能意味着new VariableTable(vt)或更可能意味着简单的order by vendorID

#2& 3:您必须将COUNT添加到Vendors.VendorName列表