Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 关于标识符的ORACLE问题_Sql_Oracle - Fatal编程技术网

Sql 关于标识符的ORACLE问题

Sql 关于标识符的ORACLE问题,sql,oracle,Sql,Oracle,如果您创建了一个函数和名称,有没有办法在以后的语句中使用该名称 例如,在下面的代码中,我将sellprice buyprice命名为PROFIT,但我似乎无法再次使用它,因为它错误地将其作为无效标识符。如果我不能这样做,请让我知道如何在该列中显示最大利润条目 SELECT item, buyprice, sellprice, sellprice-buyprice as “PROFIT” FROM auctions WHERE PROFIT = (select MAX(PROFIT) from a

如果您创建了一个函数和名称,有没有办法在以后的语句中使用该名称

例如,在下面的代码中,我将sellprice buyprice命名为PROFIT,但我似乎无法再次使用它,因为它错误地将其作为无效标识符。如果我不能这样做,请让我知道如何在该列中显示最大利润条目

SELECT item, buyprice, sellprice,
sellprice-buyprice as “PROFIT”
FROM auctions
WHERE PROFIT = (select MAX(PROFIT) from auctions); 

你能试试这句话吗 从选择项中选择*作为“利润”
按4描述从拍卖订单中删除,其中rownum=1

您将在where子句中使用函数名,引用别名在那里无效

SELECT item, buyprice, sellprice,
sellprice-buyprice as “PROFIT”
FROM auctions
WHERE sellprice-buyprice = (select MAX(PROFIT) from auctions);
如果确实要使用别名,也可以执行以下操作

select item, buyprice, sellprice, profit
from
(
  SELECT item, buyprice, sellprice, sellprice-buyprice as “PROFIT”
  FROM auctions)
)
WHERE PROFIT = (select MAX(PROFIT) from auctions);

除了使用子查询(正如已经建议的那样),您还可以通过子查询重构来实现这一点:

WITH auctions_p AS (SELECT   item,
                             buyprice,
                             sellprice,
                             sellprice - buyprice AS profit
                      FROM   auctions)
SELECT   item,
         buyprice,
         sellprice,
         profit
  FROM   auctions_p
 WHERE   profit = (SELECT   MAX(profit) FROM auctions_p);
如果您打算大量使用此功能,并且使用11g,则可以通过将此计算定义为虚拟列使其永久可用:

ALTER TABLE auctions ADD (profit AS (sellprice - buyprice));

SELECT   item,
         buyprice,
         sellprice,
         profit
  FROM   auctions
 WHERE   profit = (SELECT   MAX(profit) FROM auctions);

减号字符是否在标识符中被授权?@Luc M:它实际上是减号运算符,sellprice减去buyprice。我同意这看起来有点模棱两可。我自己更喜欢操作符周围的空格。@Andriy我去年退出了Cobol编程。减号可能在变量名中。我仍然保留一些旧习惯:-返回一个项目,但不是利润最高的项目。谢谢!如果你有时间的话,你能给我解释一下这是怎么结束的吗?order by和rownum partSure。在我的第一个示例中,有一个错误——当oracle获取where rownum>x子句时,它只获取x行,然后按order子句对它们进行排序。在第二次尝试中,oracle选择所有行,然后按“sellprice buyprice”值降序排列。此时,查询中的第一行是利润值最高的行。然后,'选择*从。。。其中rownum=1'只选择第一行。这就是全部: