CREATE PROCEDURE SalesByYear
-- Add the parameters for the stored procedure here
@year VARCHAR(4)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
SELECT
a.customerid
,a.customername
,b.orderid
,b.orderdate
,d.productid
,d.productname
FROM customer AS a,orders AS b,orderdetails AS c,products AS d,
(SELECT TOP 10 a.customerid,SUM(c.unitprice * (1-b.discount) * b.qty) AS total
FROM orders AS a,orderdetails AS b,products AS c
WHERE a.orderid = b.orderid AND b.productid = c.productid
AND CONVERT(VARCHAR(4),orderdate,112) = @year
GROUP BY a.customerid
ORDER BY total DESC) AS e
WHERE a.customerid = b.customerid
AND b.orderid = c.orderid
AND c.productid = d.productid
AND a.customerid = e.customerid
END
GO
如果不算折扣SUM(c.unitprice * (1-b.discount) * b.qty)改成
SUM(c.unitprice * b.qty)
一语句不就搞定了吗