0

我有一个关于子查询的问题。但是我尝试了不同的方法,我仍然无法正确的语法。所以请帮忙。

我有一张这样的桌子

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:

什么是正确的语法?

4

2 回答 2

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;
于 2013-10-10T02:41:14.873 回答
0

如果您已在选择列表中定义别名,则可以直接在 order by 子句中使用别名

请在链接中使用 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;
于 2013-10-10T04:38:35.030 回答