划分两个PL / SQL select语句的结果

时间:2021-02-04 00:22:46

The results of my two PL/SQL select statements are integers 27 & 50, I want their division (27/50) 0.54 at output...how to do that?

我的两个PL / SQL选择语句的结果是整数27和50,我希望它们在输出中的除法(27/50)0.54 ......怎么做?

I have tried select * from ((select....)/(select ...)) but it does not work!!

我试过select * from((select ....)/(select ...))但它不起作用!!

2 个解决方案

#1


SELECT 
  (SELECT thefirst FROM singlerowtable) / 
  (SELECT theother FROM othersinglerow) AS result

You can also use CAST(thefirst AS FLOAT) if you want to ensure a FLOAT division, &c.

如果要确保FLOAT划分,也可以使用CAST(第一个AS FLOAT),&c。

#2


In your FROM clause you can only join result sets together, you can't use other operators like that.

在FROM子句中,您只能将结果集连接在一起,不能使用其他类似的运算符。

You can, however, use arithmetic operators in your SELECT clause, e.g. (as Alex has already said):

但是,您可以在SELECT子句中使用算术运算符,例如(正如亚历克斯已经说过的):

SELECT (SELECT thefirst ...)
       /
       (SELECT thesecond ...) AS result
FROM DUAL;

or, alternatively:

SELECT A.thefirst / B.thesecond AS result
FROM   (SELECT thefirst ...) A
      ,(SELECT thesecond ...) B;

The first approach will fail if the result sets do not have exactly one row each.

如果结果集中每个都没有恰好一行,则第一种方法将失败。

The second approach will work even if the result sets have more than one row - you may have to supply join criteria between A and B if you don't want a cartesian join between the two result sets.

即使结果集具有多行,第二种方法也会起作用 - 如果您不希望在两个结果集之间进行笛卡尔连接,则可能必须在A和B之间提供连接条件。

#1


SELECT 
  (SELECT thefirst FROM singlerowtable) / 
  (SELECT theother FROM othersinglerow) AS result

You can also use CAST(thefirst AS FLOAT) if you want to ensure a FLOAT division, &c.

如果要确保FLOAT划分,也可以使用CAST(第一个AS FLOAT),&c。

#2


In your FROM clause you can only join result sets together, you can't use other operators like that.

在FROM子句中,您只能将结果集连接在一起,不能使用其他类似的运算符。

You can, however, use arithmetic operators in your SELECT clause, e.g. (as Alex has already said):

但是,您可以在SELECT子句中使用算术运算符,例如(正如亚历克斯已经说过的):

SELECT (SELECT thefirst ...)
       /
       (SELECT thesecond ...) AS result
FROM DUAL;

or, alternatively:

SELECT A.thefirst / B.thesecond AS result
FROM   (SELECT thefirst ...) A
      ,(SELECT thesecond ...) B;

The first approach will fail if the result sets do not have exactly one row each.

如果结果集中每个都没有恰好一行,则第一种方法将失败。

The second approach will work even if the result sets have more than one row - you may have to supply join criteria between A and B if you don't want a cartesian join between the two result sets.

即使结果集具有多行,第二种方法也会起作用 - 如果您不希望在两个结果集之间进行笛卡尔连接,则可能必须在A和B之间提供连接条件。