case语句尝试使用聚合函数聚合case语句中的数据

时间:2016-10-05 20:26:35

标签: sql oracle

  SELECT c.DESCRIPTION,
         fobt.OBJECT_ID,
         fobt.FA_OBJECT_TRANS_VALUE_TYPE,
         fobt.OBJECT_TRANSACTION_ID,
         fobt.OBJECT_TRANSACTION_DATE,
         fobt.AMOUNT,
         fobt.FA_YEAR,
         fobt.FA_PERIOD,
         SUM(fobt.AMOUNT) OVER(
                                PARTITION BY fobt.
                                ORDER BY fobt., fobt.OBJECT_TRANSACTION_DATE
                              ) AS RUNTOTAL,
         DENSE_RANK() OVER(
                           PARTITION BY fobt.OBJECT_ID
                           ORDER BY fobt.FA_YEAR DESC
                          ) RK1,
         DENSE_RANK()
             OVER(
                  PARTITION BY fobt.OBJECT_ID, c.DESCRIPTION
                  ORDER BY
                      fobt.FA_YEAR,
                      fobt.FA_PERIOD,
                      c.DESCRIPTION DESC
                 )
             rnk2
    FROM FA_OBJECT_TRANSACTION fobt
         INNER JOIN (SELECT DISTINCT COMPANY,
                                     OBJECT_ID,
                                     OBJECT_TRANSACTION_ID,
                                     CODE_C
                       FROM FA_) FT
             ON     fobt. = FT.
                AND fobt. = FT.OBJECT_TRANSACTION_ID
         INNER JOIN CODE_C c
             ON     FT.CODE_C = c.CODE_C
                AND FT.COMPANY = c.COMPANY
   WHERE     fobt.OBJECT_ID = 12345
         AND fobt.FA_ IN ('Depreciation')
ORDER BY 2, 4

我正在尝试为OBJECT_ID,DESCRIPTION,YEAR和PERIOD修复DENSE_RANK。只有一个对象id = 1234但有2个位置(鹿特丹和南温莎)。设备A从2015年5月5日至2016年3月登录South Windsor,然后于2016年4月转移到鹿特丹。我希望通过OBJECT_ID和LOCATION通过PARTITION获得DENSE_RANK,但是order by无法正常工作。我想采用最新的日期(年份和月份的设备位置。例如,设备A在2016年3月的位置南温莎将排名1,因为它是该地点的最新日期,然后转移到鹿特丹在2016年4月的位置,也会被标记为1,因为它改变了位置。下面是当前的结果,第二个例子是期望的结果。

1 个答案:

答案 0 :(得分:0)

如果我理解了所有内容您需要在DESC fa_year之后仅dense_rank添加... dense_rank() over(partition by object_id, description order by fa_year desc, fa_period desc) as rk2

over (... order by transaction_id desc)

或者使用with t (DESCRIPTION, OBJECT_ID, transaction_id, AMOUNT, FA_YEAR, FA_PERIOD) as (select 'South Windsor, CT', 22208, 278620, 257, 2015, 5 from dual union all select 'South Windsor, CT', 22208, 287864, 257, 2015, 6 from dual union all select 'South Windsor, CT', 22208, 305508, 257, 2015, 7 from dual union all select 'South Windsor, CT', 22208, 322323, 257, 2015, 8 from dual union all select 'South Windsor, CT', 22208, 330902, 257, 2015, 9 from dual union all select 'South Windsor, CT', 22208, 340183, 257, 2015, 10 from dual union all select 'South Windsor, CT', 22208, 350218, 257, 2015, 11 from dual union all select 'South Windsor, CT', 22208, 359261, 257, 2015, 12 from dual union all select 'South Windsor, CT', 22208, 374836, 257, 2016, 1 from dual union all select 'South Windsor, CT', 22208, 382864, 257, 2016, 2 from dual union all select 'South Windsor, CT', 22208, 388910, 257, 2016, 3 from dual union all select 'Rotterdam, NY', 22208, 388915, 257, 2016, 4 from dual) select description, transaction_id, fa_year, fa_period, sum(amount) over (partition by object_id order by fa_year, fa_period) total, dense_rank() over(partition by object_id order by fa_year desc) rk1, dense_rank() over(partition by object_id, description order by fa_year desc, fa_period desc) rk2 from t order by object_id, transaction_id

测试:

DESCRIPTION       TRANSACTION_ID    FA_YEAR  FA_PERIOD      TOTAL    RK1    RK2
----------------- -------------- ---------- ---------- ---------- ------ ------
South Windsor, CT         278620       2015          5        257      2     11
South Windsor, CT         287864       2015          6        514      2     10
South Windsor, CT         305508       2015          7        771      2      9
South Windsor, CT         322323       2015          8       1028      2      8
South Windsor, CT         330902       2015          9       1285      2      7
South Windsor, CT         340183       2015         10       1542      2      6
South Windsor, CT         350218       2015         11       1799      2      5
South Windsor, CT         359261       2015         12       2056      2      4
South Windsor, CT         374836       2016          1       2313      1      3
South Windsor, CT         382864       2016          2       2570      1      2
South Windsor, CT         388910       2016          3       2827      1      1
Rotterdam, NY             388915       2016          4       3084      1      1

输出:

BEGIN
  <<outer_loop>>
  FOR value IN 2 .. 50 LOOP
    FOR divisor IN 2 .. value - 1 LOOP
      CONTINUE outer_loop WHEN MOD( value, divisor ) = 0;
    END LOOP;
    DBMS_OUTPUT.PUT_LINE( value || ' is prime' );
  END LOOP;
END;
/