1

我在 Stackoverflow 上看到了一些与比较行有关的问题,但与这个问题完全不同。我有一个表,其列类似于:

 - Month       (Date (01-Jan-2013)
 - Country     (Varchar2)
 - SubCustomer (Varchar2)
 - FTE         (Number) Represents a value that is manually entered by employees each month

我们可能有一些这样的数据..

 - 01-Jan-2013 USA Customer1 10
 - 01-Feb-2013 USA Customer1 15
 - 01-Mar-2013 USA Customer1 30
 - 01-Jan-2013 BRA Customer2 100
 - 01-Feb-2013 BRA Customer2 300
 - 01-Mar-2013 BRA Customer2 50

我的目标是比较输入的 FTE 并在单独的列中提供警报,如“高警报”或“低警报”或“确定”,如果输入的 FTE 是每个月上个月的 +/- 2x +国家 + 子客户。我一直在玩不同的案例陈述,但我似乎无法逐月进行比较。

使用上面的数据,客户 1 将在 2 月至 3 月产生“高警报”警报,客户 2 将在 1 月至 2 月产生高警报,从 2 月至 3 月产生低警报。

4

2 回答 2

1

听起来你应该使用 LAG / LEAD。

SELECT Month, Country, SubCustomer, FTE
, LAG(FTE, 1, 0) OVER (PARTITION BY Country, SubCustomer ORDER BY Month) as PrevFTE
FROM MyTable

你可以在这里找到一些很好的例子: http ://www.oracle-base.com/articles/misc/lag-lead-analytic-functions.php

于 2013-10-01T13:58:36.980 回答
0

试试这个:包含 NULL 或零的可能性

WITH TABLEDATA
    AS (SELECT
             TO_DATE ( '01-Jan-2013',
                     'DD-MON-YYYY' )
                 AS MONT,
             'USA' AS COUNTRY,
             'Customer1' AS SUBCUST,
             0 AS FTE
        FROM
             DUAL
        UNION ALL
        SELECT
             TO_DATE ( '01-Feb-2013',
                     'DD-MON-YYYY' )
                 AS MONT,
             'USA' AS COUNTRY,
             'Customer1' AS SUBCUST,
             15 AS FTE
        FROM
             DUAL
        UNION ALL
        SELECT
             TO_DATE ( '01-Mar-2013',
                     'DD-MON-YYYY' )
                 AS MONT,
             'USA' AS COUNTRY,
             'Customer1' AS SUBCUST,
             30 AS FTE
        FROM
             DUAL
        UNION ALL
        SELECT
             TO_DATE ( '01-Jan-2013',
                     'DD-MON-YYYY' )
                 AS MONT,
             'BRA' AS COUNTRY,
             'Customer2' AS SUBCUST,
             100 AS FTE
        FROM
             DUAL
        UNION ALL
        SELECT
             TO_DATE ( '01-Feb-2013',
                     'DD-MON-YYYY' )
                 AS MONT,
             'BRA' AS COUNTRY,
             'Customer2' AS SUBCUST,
             300 AS FTE
        FROM
             DUAL
        UNION ALL
        SELECT
             TO_DATE ( '01-Mar-2013',
                     'DD-MON-YYYY' )
                 AS MONT,
             'BRA' AS COUNTRY,
             'Customer2' AS SUBCUST,
             50 AS FTE
        FROM
             DUAL)
SELECT
      MONT,
      COUNTRY,
      SUBCUST,
      FTE,
      ROUND ( ( FTE
             - PREVIOUS_FTE )
            / NULLIF ( PREVIOUS_FTE,
                     0 ),
            2 )
      * 100
          CHANGE_IN_PERCENT,
      CASE
          WHEN ROUND ( ( FTE
                     - PREVIOUS_FTE )
                    / NULLIF ( PREVIOUS_FTE,
                             0 ),
                    2 )
              * 100 >= 200
          THEN
              'High Alert'
          WHEN ROUND ( ( FTE
                     - PREVIOUS_FTE )
                    / NULLIF ( PREVIOUS_FTE,
                             0 ),
                    2 )
              * 100 <= 200
          THEN
              'Low Alert'
          ELSE
              'OK'
      END
          AS ALERTS
FROM
      (SELECT
            MONT,
            COUNTRY,
            SUBCUST,
            FTE,
            LAG ( FTE,
                 1 )
            OVER (PARTITION BY COUNTRY, SUBCUST ORDER BY MONT)
                PREVIOUS_FTE
       FROM
            TABLEDATA);

结果:

MONT        COUNTRY SUBCUST FTE CHANGE_IN_PERCENT ALERTS
1/1/2013    BRA Customer2   100                   OK
2/1/2013    BRA Customer2   300 200               High Alert
3/1/2013    BRA Customer2   50  -83               Low Alert
1/1/2013    USA Customer1   0                     OK
2/1/2013    USA Customer1   15                    OK
3/1/2013    USA Customer1   30  100               Low Alert
于 2013-10-01T14:06:30.293 回答