Oracle基本子查询

时间:2013-10-10 02:35:56

标签: oracle

我对子查询有疑问。我尝试了不同的方法,我仍然无法正确的语法。所以请帮忙。

我有一张这样的表

DD_Products
(
    ProductID 
    Description  
    ProductPrice 
    RetailPrice 
    LaborEST 
);

并尝试用列表查看清单10的产品     最高利润(销售价值和成本之间的差异)。

所以我创建了这样的

CREATE VIEW Top10money_VW AS
SELECT ProductID,Money 
FROM (select *
      from DD_Products
      Order by ProductPrice - RetailPrice AS Money desc)
Where ROWNUM <= 10;

但它说

Error starting at line 1 in command:
CREATE VIEW Top10money_VW AS
SELECT ProductID,Money 
FROM (select *
      from DD_Products
      Order by ProductPrice - RetailPrice AS Money desc)
Where ROWNUM <= 10

Error at Command Line:5 Column:43
Error report:
SQL Error: ORA-00907: missing right parenthesis
00907. 00000 -  "missing right parenthesis"
*Cause:    
*Action:

这是什么语法?

2 个答案:

答案 0 :(得分:1)

您没有名为Money的专栏尝试此

CREATE VIEW Top10money_VW AS
SELECT ProductID,Money 
FROM (select ProductID, ProductPrice - RetailPrice Money
      from DD_Products
      Order by ProductPrice - RetailPrice desc)
Where ROWNUM <= 10;

答案 1 :(得分:0)

如果已在选择列表

中定义了别名,则可以直接在order by子句中使用别名

请使用link

中的sqlfiddle查找示例案例
CREATE VIEW Top10money_VW AS
SELECT ProductID,Money 
FROM (select a.*,ProductPrice - RetailPrice AS Money
  from DD_Products a
  Order by Money desc)
  Where ROWNUM <= 10;