0

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

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 中,但它没有捕捉到这个月的零值。任何想法如何纠正它?感谢您提前帮助我

4

1 回答 1

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;
于 2013-11-07T14:51:24.980 回答