基本查询,查看问题

时间:2013-10-10 22:08:48

标签: sql oracle

我尝试进行此查询

创建一个视图,列出过去6个月内购买量最多的前十大产品。

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名?

非常感谢

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分:0)

不幸的是,你不能在Oracle视图中指定ORDER BY子句(Here more details,搜索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; 
相关问题