-1

所以标题非常准确,我想我快到了这就是我所拥有的

编辑:视图应该选择列供应商名称,列 LastInvoice 是列中的最新日期和 invoiceTotal 列的总计。我希望它只显示前 10 个已支付的 invoiceTotals 以及相应的最新发票日期和供应商名称。

CREATE VIEW TOP10PAIDInvoices
AS
  SELECT VendorName,
         MAX(InvoiceDate)  AS LastInvoice,
         SUM(InvoiceTotal) AS SumOfInvoices
  FROM   Vendors
         JOIN Invoices
           ON Vendors.VendorID = Invoices.VendorID
  WHERE  (SELECT TOP 10 SumOfInvoices
          FROM   TOP10PAIDInvoices
          GROUP  BY SumOfInvoices
          HAVING SumOfInvoices > 0)
  ORDER  BY SumOfInvoices 
4

1 回答 1

1

您的语法不正确,部分原因是您的视图 def 正在尝试自我引用。

ORDER BY与 TOP N 表达式一起使用时,在视图 def 中是合法的。

CREATE VIEW TOP10PAIDInvoices AS
SELECT TOP 10 VendorName = MAX(VendorName), MAX(InvoiceDate) AS LastInvoice, SUM(InvoiceTotal) AS SumOfInvoices
FROM Vendors JOIN Invoices
ON Vendors.VendorID = Invoices.VendorID
GROUP BY Vendors.VendorID
HAVING SUM(InvoiceTotal) > 0
ORDER BY SUM(InvoiceTotal) DESC;
GO
于 2013-08-18T16:33:39.250 回答