我的资料栏中显示的值有误。我究竟做错了什么?

时间:2018-11-28 18:12:07

标签: sql oracle

因此,我有一个从CSV / XML加载时插补/替换数据的过程。每当值被“估算” /“替换”时,我都会在 DATA_CODE 列中使用 'IMP' 代码标记原始(无效)值。然后,在 DATA_CODE 列中用 'ORG' 代码插入“估算”(或“替换”)值。 以下是一些示例数据:

  VALUE        HOUR                 HOUR_NUM        CONT       TYPE     DATA_CODE
    777     09-SEP-18 02:00:00 PM       1             SMP        SUM     ORG
    -48     09-SEP-18 02:00:00 PM       1             SMP        SUM     IMP
     99     09-SEP-18 03:00:00 PM       2             SMP        SUM     ORG 
      0     09-SEP-18 04:00:00 PM       3             SMP        SUM     ORG
  94181     09-SEP-18 05:00:00 PM       4             SMP        SUM     IMP
    842     09-SEP-18 05:00:00 PM       4             SMP        SUM     ORG
   null     09-SEP-18 06:00:00 PM       5             SMP        SUM     IMP
    842     09-SEP-18 06:00:00 PM       5             SMP        SUM     ORG

我需要一个类似于以下视图的视图:

HOUR                    HOUR_NUM   CONT      TYPE    IMPUTED_VAL   ORIG_VAL
09-SEP-18 02:00:00 PM   1          SMP       SUM     777           -48
09-SEP-18 03:00:00 PM   2          SMP       SUM     null           99
09-SEP-18 04:00:00 PM   3          SMP       SUM     null           0
09-SEP-18 05:00:00 PM   4          SMP       SUM     842            94181
09-SEP-18 06:00:00 PM   5          SMP       SUM     842            null

我尝试了以下操作:

select HOUR
     , HOUR_NUM
     , CONT
     , TYPE
     , MAX(CASE WHEN (data_code IN ('IMP', 'SUB') AND TYPE <> 'FAKE')
                THEN value
                ELSE null
                 END)    AS               IMPUTED_VALUE
     , MAX(CASE WHEN (data_code NOT IN ('IMP', 'SUB') OR data_code is null OR TYPE = 'FAKE')
                THEN value
                ELSE null
                 END)   AS                ORIG_VAL
  FROM Table1
  GROUP BY HOUR, HOUR_NUM, CONT, TYPE;

但是,我返回的结果是在IMPUTED_VAL和ORIG_VAL之间切换的。即使我切换两列的逻辑,我仍然会收到以下结果:

HOUR                    HOUR_NUM   CONT      TYPE    IMPUTED_VAL   ORIG_VAL
09-SEP-18 02:00:00 PM   1          SMP       SUM     -48            777
09-SEP-18 03:00:00 PM   2          SMP       SUM     null           99
09-SEP-18 04:00:00 PM   3          SMP       SUM     null           0

不确定这是否足够的信息,但由于我已经坚持了很长时间,因此认为伸手去拿不会有什么坏处。

谢谢!

1 个答案:

答案 0 :(得分:1)

我不明白您的要求,因为表中的数据看起来与您编写的内容正确。第一个Imputed_val为-48,因为data_code'IMP'中,而777将在Orig_val中,因为它不在'IMP''SUB'中这看起来不错。如果需要,可以切换为每列相反的case语句。

select DISTINCT tb1.HOUR
     , tb1.HOUR_NUM
     , tb1.CONT
     , tb1.TYPE
     , (CASE WHEN (tb1.data_code IN ('IMP', 'SUB') AND tb1.TYPE <> 'FAKE')
                THEN (SELECT tb2.value
                      FROM table1 tb2
                      WHERE tb2.hour = tb1.hour
                      AND tb2.data_code IN ('ORG'))
                ELSE null
                 END)    AS               IMPUTED_VALUE
     , (CASE WHEN (SELECT tb2.data_code
                   FROM table1 tb2 
                   WHERE tb2.hour = tb1.hour
                   AND tb2.data_code IN 'IMP') IS NULL 
                   OR tb1.data_code IN ('IMP') or data_code IS NULL or TYPE = 'FAKE')
                THEN value
                ELSE null
                 END)   AS                ORIG_VAL
  FROM Table1 tb1
  GROUP BY HOUR, HOUR_NUM, CONT, TYPE;

在这种情况下,我只是切换了IN和NOT IN语句并获得了想要的结果。您可能还想更改TYPE吗?