Oracle中的案例表达

时间:2017-06-21 16:42:19

标签: oracle

我在下面的临时表中有三条记录

LIBS += -L$$OUT_PWD/../Library/release -lLibrary

这是一个修改后的代码,最初由Gurwinder Singh撰写

 CREATE TABLE #TEMP
(
LOC VARCHAR (10),
SSN INT,
CASE_NUM INT,
INCIDENT_NUM VARCHAR(25),
PER_NUM INT,
FILE_DT DATETIME,
CREATE_DT DATETIME,
CLOSED_DT DATETIME,
CHG_CD VARCHAR (50)

)

INSERT INTO #TEMP VALUES('USA','5325353','538133','USA1510000188','538395378','10/15/2015 11:10 PM','10/17/2016 05:51 AM','10/17/2016 12:00 AM','18 DEA 1324(a)(2)')
INSERT INTO #TEMP VALUES('USA','5325353','8874353','USA1510000188','538395378','10/15/2015 11:10 PM','10/17/2016 10:59 AM','10/17/2016 12:00 AM','18 DEA 1324(a)(2)')
INSERT INTO #TEMP VALUES('USA','5325353','538133','USA1510000188','538395378','10/15/2015 11:10 PM','10/17/2016 05:51 AM','','18 DEA 1326(a)(2)')

我预计SAME_CHG_CD字段应为YES,YES,NO,因为CHG_CD字段有2条记录,其中18条DEA 1324(a)(2),1条记录有18条DEA 1326(a)(2)。

出于某种原因(高于我的智商),SAME_CHG_CD字段显示NO al。

2 个答案:

答案 0 :(得分:1)

对我来说很好......这是一个更简单的查询(它实际上有效...你所拥有的东西对我所知道的Oracle不起作用:

  • “#TEMP”不是有效的表名
  • INT不是有效的数据类型
  • VARCHAR可以工作,但是......好吧,Oracle文档建议使用VARCHAR2 ..现在已经有很长一段时间了。 而你的日期,没有格式,是一个等待发生的错误..

在任何情况下..这里是一个更简单的例子,希望...显示为什么它在所有情况下都显示为NO ..以及为什么它是正确的

  with t_temp as (
           select '538395378' per_num, to_date('15-oct-2015 23:10:00','dd-mon-yyyy hh24:mi:ss') file_dt, to_date('17-oct-2016 05:51:00', 'dd-mon-yyyy hh24:mi:ss') create_dt,to_date('17-oct-2016 00:00:00','dd-mon-yyyy hh24:mi:ss') closed_dt, '18 DEA 1324(a)(2)' chg_cd from dual union all
           select '538395378' per_num, to_date('15-oct-2015 23:10:00','dd-mon-yyyy hh24:mi:ss') file_dt, to_date('17-oct-2016 10:59:00', 'dd-mon-yyyy hh24:mi:ss') create_dt,to_date('17-oct-2016 00:00:00','dd-mon-yyyy hh24:mi:ss') closed_dt, '18 DEA 1324(a)(2)' chg_cd from dual union all
           select '538395378' per_num, to_date('15-oct-2015 23:10:00','dd-mon-yyyy hh24:mi:ss') file_dt, to_date('17-oct-2016 05:51:00', 'dd-mon-yyyy hh24:mi:ss') create_dt,NULL closed_dt, '18 DEA 1326(a)(2)' chg_cd from dual
           )
  select *
    from t_temp
  /

这只是一个简化的数据集 - 使用WITH子句来模拟您的TEMP表...并只显示我的原始数据:

  SQL> 

  PER_NUM   FILE_DT   CREATE_DT CLOSED_DT CHG_CD
  --------- --------- --------- --------- -----------------
  538395378 15-OCT-15 17-OCT-16 17-OCT-16 18 DEA 1324(a)(2)
  538395378 15-OCT-15 17-OCT-16 17-OCT-16 18 DEA 1324(a)(2)
  538395378 15-OCT-15 17-OCT-16           18 DEA 1326(a)(2)

  SQL>

现在,模仿您的查询:

  with t_temp as (
           select '538395378' per_num, to_date('15-oct-2015 23:10:00','dd-mon-yyyy hh24:mi:ss') file_dt, to_date('17-oct-2016 05:51:00', 'dd-mon-yyyy hh24:mi:ss') create_dt,to_date('17-oct-2016 00:00:00','dd-mon-yyyy hh24:mi:ss') closed_dt, '18 DEA 1324(a)(2)' chg_cd from dual union all
           select '538395378' per_num, to_date('15-oct-2015 23:10:00','dd-mon-yyyy hh24:mi:ss') file_dt, to_date('17-oct-2016 10:59:00', 'dd-mon-yyyy hh24:mi:ss') create_dt,to_date('17-oct-2016 00:00:00','dd-mon-yyyy hh24:mi:ss') closed_dt, '18 DEA 1324(a)(2)' chg_cd from dual union all
           select '538395378' per_num, to_date('15-oct-2015 23:10:00','dd-mon-yyyy hh24:mi:ss') file_dt, to_date('17-oct-2016 05:51:00', 'dd-mon-yyyy hh24:mi:ss') create_dt,NULL closed_dt, '18 DEA 1326(a)(2)' chg_cd from dual
           )
  select MAX(CHG_CD) OVER (PARTITION BY PER_NUM)  tmax,
         MIN(CHG_CD) OVER (PARTITION BY PER_NUM)  tmin,
           CASE WHEN MAX(CHG_CD) OVER (PARTITION BY PER_NUM) = MIN(CHG_CD) OVER (PARTITION BY PER_NUM) THEN 'YES' ELSE 'NO' END AS SAME_CHG_CD
    from t_temp
  /

我们看到以下内容:

  SQL>

  TMAX              TMIN              SAM
  ----------------- ----------------- ---
  18 DEA 1326(a)(2) 18 DEA 1324(a)(2) NO
  18 DEA 1326(a)(2) 18 DEA 1324(a)(2) NO
  18 DEA 1326(a)(2) 18 DEA 1324(a)(2) NO

  SQL>

注意ENTIRE组的MIN和ENTIRE组的MAX不一样..对于整个组。 :)

我怀疑你可能想要利用一些窗口条款..但是,如何根据你给出的信息订购数据并不是很清楚..所以这是一个有根据的猜测: 我添加了一个新列,简称为ID ..并手动编号..这是手动,清楚地表示数据的“顺序”。如果您可以在逻辑上定义订单,则可以简单地用您的逻辑而不是我的使用ID列替换ORDER BY:)

  with t_temp as (
           select 1 ID, '538395378' per_num, to_date('15-oct-2015 23:10:00','dd-mon-yyyy hh24:mi:ss') file_dt, to_date('17-oct-2016 05:51:00', 'dd-mon-yyyy hh24:mi:ss') create_dt,to_date('17-oct-2016 00:00:00','dd-mon-yyyy hh24:mi:ss') closed_dt, '18 DEA 1324(a)(2)' chg_cd from dual union all
           select 2 ID, '538395378' per_num, to_date('15-oct-2015 23:10:00','dd-mon-yyyy hh24:mi:ss') file_dt, to_date('17-oct-2016 10:59:00', 'dd-mon-yyyy hh24:mi:ss') create_dt,to_date('17-oct-2016 00:00:00','dd-mon-yyyy hh24:mi:ss') closed_dt, '18 DEA 1324(a)(2)' chg_cd from dual union all
           select 3 ID, '538395378' per_num, to_date('15-oct-2015 23:10:00','dd-mon-yyyy hh24:mi:ss') file_dt, to_date('17-oct-2016 05:51:00', 'dd-mon-yyyy hh24:mi:ss') create_dt,NULL closed_dt, '18 DEA 1326(a)(2)' chg_cd from dual
           )
  select MAX(CHG_CD) OVER (PARTITION BY PER_NUM order by id)  tmax,
         MIN(CHG_CD) OVER (PARTITION BY PER_NUM order by id)  tmin,
           CASE WHEN MAX(CHG_CD) OVER (PARTITION BY PER_NUM order by id) = MIN(CHG_CD) OVER (PARTITION BY PER_NUM order by id) THEN 'YES' ELSE 'NO' END AS SAME_CHG_CD
    from t_temp
  /

和结果:

  SQL> 

  TMAX              TMIN              SAM
  ----------------- ----------------- ---
  18 DEA 1324(a)(2) 18 DEA 1324(a)(2) YES
  18 DEA 1324(a)(2) 18 DEA 1324(a)(2) YES
  18 DEA 1326(a)(2) 18 DEA 1324(a)(2) NO

  SQL>

因此使用分析逻辑的Windowing子句。 如果还不完全清楚它在做什么,请在这里或新线程问:):

答案 1 :(得分:0)

运行以下内容,答案应该非常明显:

SELECT 
    LOC,
    INCIDENT_NUM,
    CHG_CD
    , MAX(CHG_CD) OVER  (PARTITION BY PER_NUM)    MAX_CHG_CD
    , MIN(CHG_CD) OVER (PARTITION BY PER_NUM) Min_CHG_CD
    , MAX(PER_NUM) OVER (PARTITION BY PER_NUM) MAX_PER_NUM
    , MIN(PER_NUM) OVER (PARTITION BY PER_NUM) MIN_PER_NUM
    , MAX(SSN) OVER (PARTITION BY PER_NUM) MAX_SSN
    , MIN(SSN) OVER (PARTITION BY PER_NUM) MIN_SSN
    , MAX(INCIDENT_NUM) OVER (PARTITION BY PER_NUM) MAX_INCIDENT_NUM
    , MIN(INCIDENT_NUM) OVER (PARTITION BY PER_NUM) MIN_INCIDENT_NUM
    , MAX(FILE_DT) OVER (PARTITION BY PER_NUM) MAX_FILE_DT
    , MIN(FILE_DT) OVER (PARTITION BY PER_NUM) MIN_FILE_DT
    , SSN
    , FILE_DT
    , CREATE_DT
    , CASE_NUM
    , PER_NUM
    , ROW_NUMBER()OVER(PARTITION BY CASE_NUM ORDER BY CASE_NUM) AS SEQ
    FROM #TEMP
;