SQL在SELECT语句中声明变量

时间:2017-01-04 17:39:32

标签: sql oracle

尝试运行SELECT语句以创建执行某些计算的视图。

我想在另一个字段中使用计算值,而不必再次计算

Shape字段存储地理数据,sde.st_y / x是一个返回形状的纬度/长度的函数

示例:

SELECT
    t.shape AS "Shape",
    t.code AS "GeocodeTo",
    sde.st_x(t.shape) AS "Longitude_decimal",
    --The following field calculates sde.st_x again unnecesairly
    TRUNC(ABS(sde.st_x(t.shape)) as "Absolute"         
FROM
    towers t

2 个答案:

答案 0 :(得分:1)

如果您不想两次调用函数,请尝试使用

select "Shape","GeocodeTo",TRUNC(ABS(Longitude_decimal)) 
from 
(
SELECT
    t.shape AS "Shape",
    t.code AS "GeocodeTo",
    sde.st_x(t.shape) AS "Longitude_decimal"
FROM
    towers t
) a 

但我认为Oracle会足够聪明,只能在原始查询中调用该函数一次,尽管你调用它两次

答案 1 :(得分:1)

在子查询中调用函数并重用外部值:

SELECT "Shape",
  "GeocodeTo",
  "Longitude_decimal",
  TRUNC(ABS("Longitude_decimal") AS "Absolute"
FROM
  (SELECT t.shape     AS "Shape",
    t.code            AS "GeocodeTo",
    sde.st_x(t.shape) AS "Longitude_decimal",
  FROM towers t
  );