0

我尝试进行此查询

创建一个视图,列出过去 6 个月内购买次数最多的 10 种产品。

CREATE VIEW Top10product6month_VW AS
SELECT ProductID
FROM (select ProductID,SYSDATE - OrderDate AS OrderAge
      from DD_OrderLine
      WHERE SYSDATE - OrderDate <= 183)
Where ROWNUM <= 10;

我无法获得top10。

我的 OrderLine 表是

CREATE TABLE DD_OrderLine
(
OrderDate DATE,
SUMofPrice NUMBER(8,2),
OrderID NUMBER(6),
ProductID NUMBER(6),
CONSTRAINT DD_orderid_productid_pk PRIMARY KEY (OrderID,ProductID )
);

和记录是

INSERT INTO DD_OrderLine (OrderDate,SUMofPrice, QuantityPurchased,OrderID,ProductID)
VALUES ((to_date('2013/9/25', 'yyyy/mm/dd')),000010.00,1,000117,001116);
--
INSERT INTO DD_OrderLine (OrderDate,SUMofPrice, QuantityPurchased,OrderID,ProductID)
VALUES ((to_date('2013/9/14', 'yyyy/mm/dd')),000010.00,1,000118,001112);
--
INSERT INTO DD_OrderLine (OrderDate,SUMofPrice, QuantityPurchased,OrderID,ProductID)
VALUES ((to_date('2013/9/14', 'yyyy/mm/dd')),000010.00,1,000118,001111);
--
INSERT INTO DD_OrderLine (OrderDate,SUMofPrice, QuantityPurchased,OrderID,ProductID)
VALUES ((to_date('2013/9/15', 'yyyy/mm/dd')),000010.00,1,000119,001111);

INSERT INTO DD_OrderLine (OrderDate,SUMofPrice, QuantityPurchased,OrderID,ProductID)
VALUES ((to_date('2013/9/15', 'yyyy/mm/dd')),000010.00,1,000119,001112);

INSERT INTO DD_OrderLine (OrderDate,SUMofPrice, QuantityPurchased,OrderID,ProductID)
VALUES ((to_date('2013/9/24', 'yyyy/mm/dd')),000010.00,1,000120,001115);

INSERT INTO DD_OrderLine (OrderDate,SUMofPrice, QuantityPurchased,OrderID,ProductID)
VALUES ((to_date('2013/9/24', 'yyyy/mm/dd')),000010.00,1,000120,001114);

INSERT INTO DD_OrderLine (OrderDate,SUMofPrice, QuantityPurchased,OrderID,ProductID)
VALUES ((to_date('2013/9/24', 'yyyy/mm/dd')),000010.00,1,000120,001113);

INSERT INTO DD_OrderLine (OrderDate,SUMofPrice, QuantityPurchased,OrderID,ProductID)
VALUES ((to_date('2013/9/24', 'yyyy/mm/dd')),000010.00,1,000120,001112);
----
INSERT INTO DD_OrderLine (OrderDate,SUMofPrice, QuantityPurchased,OrderID,ProductID)
VALUES ((to_date('2013/9/24', 'yyyy/mm/dd')),000010.00,1,000120,001111);

INSERT INTO DD_OrderLine (OrderDate,SUMofPrice, QuantityPurchased,OrderID,ProductID)
VALUES ((to_date('2013/9/24', 'yyyy/mm/dd')),000010.00,1,000120,001116);

INSERT INTO DD_OrderLine (OrderDate,SUMofPrice, QuantityPurchased,OrderID,ProductID)
VALUES ((to_date('2013/9/24', 'yyyy/mm/dd')),000010.00,1,000120,001117);

INSERT INTO DD_OrderLine (OrderDate,SUMofPrice, QuantityPurchased,OrderID,ProductID)
VALUES ((to_date('2013/9/30', 'yyyy/mm/dd')),000010.00,1,000121,001112);

INSERT INTO DD_OrderLine (OrderDate,SUMofPrice, QuantityPurchased,OrderID,ProductID)
VALUES ((to_date('2013/9/30', 'yyyy/mm/dd')),000010.00,1,000122,001112);

有什么方法可以统计 ProductID 并进入前 10 名?

太感谢了

4

2 回答 2

0

不幸的是,您不能在 Oracle 视图中指定 ORDER BY 子句(这里有更多详细信息,搜索 ORDER BY 即可找到)

无论如何,如果您需要存储前十个结果的查询,即使没有排序,您也可以使用:

CREATE VIEW Top10product6month_VW 
AS
SELECT ProductID
  FROM (select ProductID,
               SYSDATE - OrderDate AS OrderAge
          from DD_OrderLine
         WHERE SYSDATE - OrderDate <= 183
      order by QuantityPurchased desc)
 Where ROWNUM <= 10; 
于 2013-10-10T22:43:16.097 回答
0

怎么样:

SELECT ProductID
FROM (select ProductID, SUM(QuantityPurchased) AS cnt
      from DD_OrderLine
      WHERE SYSDATE - OrderDate <= 183
      GROUP BY ProductID 
      ORDER BY cnt DESC)
Where ROWNUM <= 10;
于 2013-10-10T22:24:24.223 回答