SQL查询比较2个报告的时间戳和显示增加/减少趋势

时间:2017-12-21 11:42:05

标签: sql oracle

我使用以下查询获取当前时间戳的Input_MBOutput_MB。 但是,我需要与之前的1小时数据进行比较,输入和输出值是如何存在于同一行中的。基于此我需要增加/减少趋势。

用于获取9:30和10:30报告的查询是:

SELECT
  A.last_updated,
  B.SERVER AS Server,
  D.STREAM_NAME AS Stream,
  A.INPUT AS Input,
  A.OUTPUT AS Output
FROM table1 A,
     table2 B,
     table3 C,
     table4 D
WHERE A.ALARM_BACKLOG = '1'
AND C.MONIT_STATUS = 'ACTIVE'
AND A.NODE_MAN_ID = B.NODE_MAN_ID
AND A.NODE_ID = C.NODE_ID
AND B.STR_ID = D.STR_ID
AND B.NODE_MAN_ID = C.NODE_MAN_ID
ORDER BY NM.HOST_SERVER_NAME, S.STREAM_NAME DESC

假设上午9:30报告是:

Last_updated    Server  stream  input   output
9:30 AM A   1   80  0
9:30 AM EFH 7   8   20
9:30 AM D   5   60  0
9:30 AM E   8   25  100

和上午10:30报告应该是:

Last_updated    Server  stream  input   output
10:30 AM    A   1   40  0
10:30 AM    C   7   0   8
10:30 AM    D   5   72  0
10:30 AM    B   3   0   0
10:30 AM    E   8   25  115

但是我想要10:30报告应该如下。

Last_updated    Server  stream  input   "Input 
Trend"  output  "Output
 Trend"
10:30 AM    A   1   40  50%-Down    0   No change
10:30 AM    C   7   0   New 8   New
10:30 AM    D   5   72  20%- Up 0   New
10:30 AM    B   3   0   New 0   New
10:30 AM    E   8   25  No change   15%-Up  New

1 个答案:

答案 0 :(得分:1)

也许试试这个(我没有经过考试就写了)

  Select A.last_updated, A.SERVER, B.Stream,A.INPUT,
Case 
    When B.INPUT is Null Then 'New'
    When A.INPUT=B.INPUT Then 'No change'
    When A.INPUT<B.INPUT Then Cast((B.INPUT-A.INPUT)/B.INPUT*100 as Varchar(10))+'%-Down'
    When A.INPUT>B.INPUT Then Cast((A.INPUT-B.INPUT)/B.INPUT*100 as Varchar(10))+'%-Up'
End INPUT_TREND,
A.OUTPUT,
Case 
    When B.OUTPUT is Null Then 'New'
    When A.OUTPUT=B.OUTPUT Then 'No change'
    When A.OUTPUT<B.OUTPUT Then Cast((B.OUTPUT-A.OUTPUT)/B.OUTPUT*100 as Varchar(10))+'%-Down'
    When A.OUTPUT>B.OUTPUT Then Cast((A.OUTPUT-B.OUTPUT)/B.OUTPUT*100 as Varchar(10))+'%-Up'
End INPUT_TREND

From 
(
 Select A.last_updated, B.SERVER, D.STREAM_NAME as Stream, A.INPUT, A.OUTPUT ,RANK() OVER (ORDER BY A.last_updated Desc) Row_id
 From table1 A
 Join table2 B on A.NODE_MAN_ID = B.NODE_MAN_ID
 Join table3 C on A.NODE_ID = C.NODE_ID and  B.NODE_MAN_ID= C.NODE_MAN_ID 
 Join table4 D on B.STR_ID = D.STR_ID
 Where A.ALARM_BACKLOG = '1' and C.MONIT_STATUS = 'ACTIVE') A
Left join 
(
 Select A.last_updated, B.SERVER, D.STREAM_NAME as Stream, A.INPUT, A.OUTPUT,RANK() OVER (ORDER BY A.last_updated Desc) Row_id 
 From table1 A
 Join table2 B on A.NODE_MAN_ID = B.NODE_MAN_ID
 Join table3 C on A.NODE_ID = C.NODE_ID and  B.NODE_MAN_ID= C.NODE_MAN_ID 
 Join table4 D on B.STR_ID = D.STR_ID
 Where A.ALARM_BACKLOG = '1' and C.MONIT_STATUS = 'ACTIVE') B on B.Row_id=A.Row_id+1 and A.SERVER=B.SERVER and A.Stream=B.Stream
Where A.last_updated='10:30 AM'