SQL:使用少于运算符复数的解码

时间:2013-09-06 14:48:15

标签: sql

我想只选择有GAS<的行。 0.9和油<来自以下工作查询的0.9。

以下查询工作正常,但我无法选择仅在GAS< 0.9和油< 0.9值,因为我已经使用groupby;我不确定这是获得这个结果的最佳方式。

SELECT
         rf.DAYTIME,
         GEO_FIELD_CODE,
         f.NAME,
         ROUND(decode(THEOR_GAS_RATE_SM3, 0, NULL, NULL, NULL,ABS(ALLOC_GAS_VOL_SM3/THEOR_GAS_RATE_SM3)),3) AS GAS,
         ROUND(decode(THEOR_NET_OIL_RATE_STM3, 0, NULL, NULL, NULL,ABS(ALLOC_NET_OIL_VOL_STM3/THEOR_NET_OIL_RATE_STM3)),3) AS OIL
FROM (SELECT 
         DAYTIME,
         GEO_FIELD_CODE,
         SUM(THEOR_GAS_RATE_SM3) AS THEOR_GAS_RATE_SM3,
         SUM(ALLOC_GAS_VOL_SM3) AS ALLOC_GAS_VOL_SM3,
         SUM(THEOR_NET_OIL_RATE_STM3) AS THEOR_NET_OIL_RATE_STM3,
         SUM(ALLOC_NET_OIL_VOL_STM3) AS ALLOC_NET_OIL_VOL_STM3
    FROM XYZ
   WHERE DAYTIME >= TO_DATE ('2013-01-01', 'yyyy-mm-dd')
    GROUP BY DAYTIME, GEO_FIELD_CODE
ORDER BY GEO_FIELD_CODE, DAYTIME) rf, ECKERNEL_HALE.OV_FIELD f
WHERE rf.GEO_FIELD_CODE = f.CODE

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

我看到两种不同的解决方法。您可以使用用于计算条件中的值的表达式:

WHERE
  rf.GEO_FIELD_CODE = f.CODE AND
  ROUND(decode(THEOR_GAS_RATE_SM3, 0, NULL, NULL, NULL,ABS(ALLOC_GAS_VOL_SM3/THEOR_GAS_RATE_SM3)),3) < 0.9 AND
  ROUND(decode(THEOR_NET_OIL_RATE_STM3, 0, NULL, NULL, NULL,ABS(ALLOC_NET_OIL_VOL_STM3/THEOR_NET_OIL_RATE_STM3)),3) < 0.9

查询规划器应该足够聪明,看它是相同的表达式,这样它们实际上不会被计算两次。另一种方法是将整个查询包装在另一个查询中,以便您可以访问表达式的结果:

SELECT DAYTIME, GEO_FIELD_CODE, NAME, GAS, OIL
FROM (
  ... your current query goes here
) x
WHERE GAS < 0.9 AND OIL < 0.9