查找按日期分组或两个旋转列之间的两行之间的差异

问题描述 投票:0回答:2

我的OBIEE报告中有两组不同日期的值:

------------------------------------------------------------------
Option   Date     Value 
------|---------|-------
OPT1    Date 1    5     
OPT1    Date 2    2     
OPT2    Date 1    9     
OPT2    Date 2    1     
OPT3    Date 1    7     
OPT3    Date 2    13    
OPT4    Date 1    5     
OPT4    Date 2    6   

我希望在每组日期之间获得值的差异,并以下列格式显示数据,按选项分组:

Option  Date               Diff
        Date 1   Date 2 
------|--------|--------|-------  
OPT1    5        2        3
OPT2    9        1        8
OPT3    7        13       -6
OPT4    5        6        -1

可以使用Pivot处理Date部分,但是我无法找到现在已转动的列之间的差异。

我相信如果在一组日期中找到两个值之间的差异(如下所示)然后转动,它可能会做到这一点,但我无法找到一组的差异。

Option  Date      Value   Diff
-------|---------|-------|-------
OPT1    Date 1    5       null
OPT1    Date 2    2        3
OPT2    Date 1    9       null
OPT2    Date 2    1        8
OPT3    Date 1    7       null
OPT3    Date 2    13       -6
OPT4    Date 1    5       null 
OPT4    Date 2    6       -1

任何帮助表示赞赏。

谢谢,Junaid

sql oracle oracle11g obiee
2个回答
0
投票

您可以使用pivot子句生成的列来引用和进行计算。假设您有一些键值链接日期1和2的值对,您可以执行以下操作:

-- CTE for sample data, with made-up keys
with your_table (some_key, some_date, value) as (
            select 1, date '2019-04-01', 5 from dual
  union all select 1, date '2019-04-15', 2 from dual
  union all select 2, date '2019-04-01', 9 from dual
  union all select 2, date '2019-04-15', 1 from dual
  union all select 3, date '2019-04-01', 7 from dual
  union all select 3, date '2019-04-15', 13 from dual
  union all select 4, date '2019-04-01', 5 from dual
  union all select 4, date '2019-04-15', 6 from dual
)
-- actual query
select some_key, date1, date2, date1 - date2 as diff
from your_table
pivot (max(value) for some_date in (date '2019-04-01' as date1, date '2019-04-15' as date2))
order by some_key;

  SOME_KEY      DATE1      DATE2       DIFF
---------- ---------- ---------- ----------
         1          5          2          3
         2          9          1          8
         3          7         13         -6
         4          5          6         -1

date1 - date2 as diff表达式中,date1date2是来自枢轴的名称/别名。您通常不能在定义它的同一级别的查询中使用列别名,但通过枢轴可以让您逃脱它。


0
投票

好的,我想我得到了按选项划分的逐行减法的解决方案

VALUE - LAG(VALUE,1,NULL)OVER(选项按选项排序)

但是,由于某些限制,我无法在OBIEE中使用DB函数。我想了解一个不涉及使用DB函数的解决方案(如LAG / LEAD)。

© www.soinside.com 2019 - 2024. All rights reserved.