NVL功能无法正常工作

时间:2013-11-07 14:30:04

标签: sql database plsql

我应该做一个应该计算每月平均费用的脚本。 在下面的屏幕截图中,您可以看到我应该收到的平均值。

OP_Yearly           
         counter charge minutes
Oct 2012    1   5.16    1.32
Nov 2012    1   54.44   14.42
Dec 2012    0   0.00    0.00

    Average:    19.86464    5.246666667

然而,问题是我收到了这个平均值,其中费用没有除以具有零值的月份。

OP_Yearly           
         counter charge minutes
Oct 2012    1   5.16    1.32
Nov 2012    1   54.44   14.42
Dec 2012    0   0.00    0.00

    Average:    29.79696    7.87

这是我的脚本 -

   SELECT op,
       SUM (counter) AS counter,
       AVG (NVL (Charge_SDR, 0)) AS AVERAGE_CHARGE_SDR_YTD,
       AVG (NVL (Minutes, 0)) AS AVERAGE_MINUTES_YTD
  FROM (
  SELECT t.op AS op,
           COUNT (DISTINCT t.im) AS counter,
           (SUM (t.charge / POWER (10, t.decimals))) AS Charge_SDR,
           CAST (SUM (t.duration / 60) AS NUMBER (10, 2)) Minutes
      FROM top t, oper o
     WHERE t.op = o.op AND timestamp LIKE '201210%' AND o.TYPE = 'M'
GROUP BY t.op,
UNION ALL
  SELECT t.op AS op,
         COUNT (DISTINCT t.im) AS counter,
         (SUM (t.charge / POWER (10, t.decimals))) AS Charge_SDR,
         CAST (SUM (t.duration / 60) AS NUMBER (10, 2)) Minutes
    FROM top t, oper o
   WHERE t.op = o.op AND timestamp LIKE '201211%' AND o.TYPE = 'M'
GROUP BY t.op,
UNION ALL
  SELECT t.op AS op,
         COUNT (DISTINCT t.im) AS counter,
         (SUM (t.charge / POWER (10, t.decimals))) AS Charge_SDR,
         CAST (SUM (t.duration / 60) AS NUMBER (10, 2)) Minutes
    FROM top t, oper o
   WHERE t.op = o.op AND timestamp LIKE '201212%' AND o.TYPE = 'M'
GROUP BY t.op, )
GROUP BY op;

很容易将它除以3个月(sum(Charge_SDR)/ 3),但我想知道如何使用NVL功能。我已经包含在脚本NVL中,但它本月没有捕获到零值。任何想法如何纠正它? 感谢您提前帮助我

1 个答案:

答案 0 :(得分:0)

您的子查询在没有记录的月份中没有返回一行。使用带有月份列表的外部联接为它们创建空行。

SELECT op,
       SUM (counter) AS counter,
       AVG (NVL (Charge_SDR, 0)) AS AVERAGE_CHARGE_SDR_YTD,
       AVG (NVL (Minutes, 0)) AS AVERAGE_MINUTES_YTD
FROM (
  SELECT t.op AS op,
         SUBSTR(timestamp, 1, 6) AS month,
         COUNT (DISTINCT t.im) AS counter,
         (SUM (t.charge / POWER (10, t.decimals))) AS Charge_SDR,
         CAST (SUM (t.duration / 60) AS NUMBER (10, 2)) Minutes
    FROM top t, oper o
   WHERE t.op = o.op AND timestamp LIKE '201210%' AND o.TYPE = 'M'
GROUP BY t.op,
UNION ALL
  SELECT t.op AS op,
         SUBSTR(timestamp, 1, 6) AS month,
         COUNT (DISTINCT t.im) AS counter,
         (SUM (t.charge / POWER (10, t.decimals))) AS Charge_SDR,
         CAST (SUM (t.duration / 60) AS NUMBER (10, 2)) Minutes
    FROM top t, oper o
   WHERE t.op = o.op AND timestamp LIKE '201211%' AND o.TYPE = 'M'
GROUP BY t.op,
UNION ALL
  SELECT t.op AS op,
         SUBSTR(timestamp, 1, 6) AS month,
         COUNT (DISTINCT t.im) AS counter,
         (SUM (t.charge / POWER (10, t.decimals))) AS Charge_SDR,
         CAST (SUM (t.duration / 60) AS NUMBER (10, 2)) Minutes
    FROM top t, oper o
   WHERE t.op = o.op AND timestamp LIKE '201212%' AND o.TYPE = 'M') t
RIGHT JOIN (SELECT '201210' month
            FROM dual
            UNION SELECT '201211'
            FROM dual
            UNION SELECT '201212'
            FROM dual) m
ON t.month = m.month
GROUP BY op;