创建视图时的SQL命令

时间:2018-03-08 17:34:47

标签: oracle

我正在尝试在以下代码中在oracle 11g中创建一个视图     但得到错误说命令没有正确结束,任何人都在     我的位置指向正确的方向或纠正我。

CREATE 
OR 
replace FORCE editionable VIEW "GINSOBADEV_DW"."V_PSI" ( "PARENT_PI_NUMBER","REQUEST_NUM","PI_CANDIDATE_NUM","PSI_SCORE","PSI_Xmation","PSI_BAND" ) AS
SELECT request_num, 
       pi_candidate_num, 
       psi_score, 
       psi_xmation, ( 
       CASE 
              WHEN dd.psi_xmation <=      -0.880927 THEN 1 
              WHEN dd.psi_xmation BETWEEN -0.880926 AND    -0.063391 THEN 2 
              WHEN dd.psi_xmation BETWEEN -0.063390 AND    0.706751 THEN 3 
              WHEN dd.psi_xmation >= 0.706752 THEN 4 
       END ) AS psi_band 
FROM   ( 
              SELECT request_num, 
                     pi_candidate_num, 
                     request_last_modified_dt, 
                     coalesce (gco_ad_knowledge_test_score,psi_overall_score)                       AS psi_score,
                     (coalesce(gco_ad_knowledge_test_score, psi_overall_score) -39.035024)/8.439997 AS psi_xmation
              FROM  ( 
                            SELECT arank, 
                                   parent_pi_number, 
                                   request_num, 
                                   pi_candidate_num, 
                                   psi_overall_score, 
                                   gco_ad_knowledge_test_score, 
                                   request_last_modified_dt 
                            FROM  ( 
                                            SELECT   row_number() over (PARTITION BY pi_candidate_num ORDER BY request_last_modified_dt DESC,request_num ) arank ,
                                                     parent_pi_number, 
                                                     request_num, 
                                                     pi_candidate_num, 
                                                     psi_overall_score, 
                                                     gco_ad_knowledge_test_score, 
                                                     request_last_modified_dt 
                                            FROM     wc_application_event_f 
                                                     --   where rownum =1 
                                            WHERE    ( 
                                                              psi_overall_score IS NOT NULL 
                                                     OR       gco_ad_knowledge_test_score IS NOT NULL)
                                                     -- GCO_AD_KNOWLEDGE_TEST_SCORE != '10100' and    --AND PI_CANDIDATE_NUM IN (4119830,4781686      ) 
                                   ) cc 
                            WHERE  arank =1 ) bb )dd;

2 个答案:

答案 0 :(得分:0)

首先,我简化了您提交的查询。老实说,就像K Sly,Zak和APC所说,你的格式很糟糕。

这让我来到这里:

CREATE OR replace FORCE editionable VIEW "GINSOBADEV_DW"."V_PSI" ( "PARENT_PI_NUMBER","REQUEST_NUM","PI_CANDIDATE_NUM","PSI_SCORE","PSI_Xmation","PSI_BAND" ) AS
SELECT dd.request_num, 
       dd.pi_candidate_num, 
       dd.psi_score, 
       dd.psi_xmation, 
       (CASE 
              WHEN dd.psi_xmation <=      -0.880927 THEN 1 
              WHEN dd.psi_xmation BETWEEN -0.880926 AND    -0.063391 THEN 2 
              WHEN dd.psi_xmation BETWEEN -0.063390 AND    0.706751 THEN 3 
              WHEN dd.psi_xmation >= 0.706752 THEN 4 
       END) AS psi_band 
  FROM (SELECT bb.request_num, 
               bb.pi_candidate_num, 
               bb.request_last_modified_dt, 
               coalesce (bb.gco_ad_knowledge_test_score, bb.psi_overall_score)                        AS psi_score,
               ((coalesce(bb.gco_ad_knowledge_test_score, bb.psi_overall_score) -39.035024)/8.439997) AS psi_xmation
          FROM (SELECT cc.arank, 
                          cc.parent_pi_number, 
                          cc.request_num, 
                          cc.pi_candidate_num, 
                          cc.psi_overall_score, 
                          cc.gco_ad_knowledge_test_score, 
                          cc.request_last_modified_dt 
                     FROM (SELECT row_number() over (PARTITION BY pi_candidate_num ORDER BY request_last_modified_dt DESC, request_num ASC) arank ,
                                  parent_pi_number, 
                                  request_num, 
                                  pi_candidate_num, 
                                  psi_overall_score, 
                                  gco_ad_knowledge_test_score, 
                                  request_last_modified_dt 
                             FROM wc_application_event_f 
                            WHERE (psi_overall_score IS NOT NULL OR gco_ad_knowledge_test_score IS NOT NULL)) cc 
                    WHERE cc.arank =1)bb)dd;

我觉得这个查询对于你想要达到的目标来说仍然太复杂了。更不用说,你有进入的参数没有在任何地方使用。

所以我简化了这个查询:

CREATE OR replace FORCE  VIEW "V_PSI" AS
with calculated_values as
         (select request_num,
                 pi_candidate_num, 
                 coalesce (gco_ad_knowledge_test_score, psi_overall_score) AS psi_score,
                 ((coalesce(gco_ad_knowledge_test_score, psi_overall_score) -39.035024)/8.439997) AS psi_xmation,
                 row_number() over (PARTITION BY pi_candidate_num ORDER BY request_last_modified_dt DESC, request_num ASC) arank
            from wc_application_event_f
           where psi_overall_score IS NOT NULL OR gco_ad_knowledge_test_score IS NOT NULL)
SELECT request_num, 
       pi_candidate_num, 
       psi_score, 
       psi_xmation, 
       (CASE 
              WHEN psi_xmation <=      -0.880927 THEN 1 
              WHEN psi_xmation BETWEEN -0.880926 AND    -0.063391 THEN 2 
              WHEN psi_xmation BETWEEN -0.063390 AND    0.706751 THEN 3 
              WHEN psi_xmation >= 0.706752 THEN 4 
       END) AS psi_band 
  FROM calculated_values
  where arank = 1;

然后当我添加可编辑条款时,我遇到了各种各样的麻烦(顺便说一下,实际上是“正在编辑”)

首先,Oracle不喜欢“with”声明;它生成了一个ORA-00922,所以我再次将它移动到子查询中。

然后它抱怨ORA-00936这是一个缺失的陈述。

长话短说,我经历了几次迭代,并且不能在视图中执行查询。我认为您需要提供更多背景信息,说明您在这里使用可编辑视图的原因。另外,为什么强迫结果限于第一个?

您可以参考的资源:

  1. enter image description here 摩根的库几乎可以用于任何SQL和PL / SQL

  2. https://www.morganslibrary.org/reference/editioning_views.html SQL Fiddle允许您指定数据库,构建模式并测试语句。全部免费。

  3. http://sqlfiddle.com/ 这是Oracle的可编辑视图参考表。负责任地使用它。

  4. https://docs.oracle.com/cd/E11882_01/appdev.112/e41502/adfns_editions.htm#ADFNS020 问汤姆深入编辑版本。需要阅读很多内容,但他有一些可靠的例子。

答案 1 :(得分:-1)

我想这取决于你如何实现这一点。如果在SQL * Plus中运行它,它会将双新行字符作为新语句读取。

尝试删除换行符,以便每行不超过一个换行符。删除评论也会有所帮助。 EG:

CREATE OR REPLACE FORCE EDITIONABLE VIEW "GINSOBADEV_DW"."V_PSI" (
"PARENT_PI_NUMBER","REQUEST_NUM","PI_CANDIDATE_NUM","PSI_SCORE","PSI_Xmation","PSI_BAND"
)  AS  SELECT  REQUEST_NUM,PI_CANDIDATE_NUM,PSI_SCORE,PSI_Xmation,
(CASE  WHEN DD.PSI_Xmation <= -0.880927 THEN 1 WHEN DD.PSI_Xmation
BETWEEN -0.880926 AND -0.063391 THEN 2 WHEN DD.PSI_Xmation BETWEEN
-0.063390 AND  0.706751 THEN 3 WHEN DD.PSI_Xmation >= 0.706752 THEN 4  END ) AS PSI_BAND
FROM  ( SELECT
REQUEST_NUM,PI_CANDIDATE_NUM,REQUEST_LAST_MODIFIED_DT, COALESCE
(GCO_AD_KNOWLEDGE_TEST_SCORE,PSI_OVERALL_SCORE) AS PSI_SCORE,
(COALESCE(GCO_AD_KNOWLEDGE_TEST_SCORE, PSI_OVERALL_SCORE)
-39.035024)/8.439997 AS PSI_Xmation
FROM(
SELECT
ARANk,PARENT_PI_NUMBER,REQUEST_NUM,PI_CANDIDATE_NUM,PSI_OVERALL_SCORE,GCO_AD_KNOWLEDGE_TEST_SCORE,REQUEST_LAST_MODIFIED_DT
FrOM( SELECT     ROW_NUMBER() OVER (PARTITION  BY PI_CANDIDATE_NUM
ORDER BY REQUEST_LAST_MODIFIED_DT DESC,REQUEST_NUM  ) ARANK  
,PARENT_PI_NUMBER,REQUEST_NUM,PI_CANDIDATE_NUM,PSI_OVERALL_SCORE,GCO_AD_KNOWLEDGE_TEST_SCORE,REQUEST_LAST_MODIFIED_DT
FROM    WC_APPLICATION_EVENT_F
WHERE (PSI_OVERALL_SCORE IS NOT NULL OR GCO_AD_KNOWLEDGE_TEST_SCORE
IS NOT Null)
     ) CC   WHERE  ARANK =1 ) BB )DD;