在 DB2 中创建视图

CREATE VIEW IN DB2

我有一个 DB2 SQL 语句,它 select 成功地获取了所需的行。由于它是一个耗时的查询,我正在尝试使用 create view 语句将其转换为视图。

WITH firstequiprrn 
     AS (SELECT oeord#      equiporder, 
                Min(RRN(a)) firstrow 
         FROM   iesqafile.OPEQUIP a 
         GROUP  BY oeord# 
         ORDER  BY 1), 
         
     firstequiprow 
     AS (SELECT oeord#, 
                oetrlr equipmentnumber 
         FROM   firstequiprrn 
                INNER JOIN iesqafile.OPEQUIP b 
                        ON equiporder = oeord# 
                           AND firstrow = RRN(b)), 
                           
     ordermiles 
     AS (SELECT mmord#, 
                mmtotal 
         FROM   iesqafile.mmiles 
         WHERE  mmord# IN(SELECT orodr# 
                          FROM   iesqafile.order) 
                AND mmrectype = 'O' 
                AND mmdsp# = '00'), 

     stopgroup 
     AS (SELECT soord       stoporder, 
                COUNT(*)    stopsremain, 
                Min(sostp#) nextstop, 
                Max(soappr) apptreq, 
                Max(soaptm) apptmade 
         FROM   iesqafile.stopoff 
                INNER JOIN iesqafile.order 
                        ON orodr# = soord 
         WHERE  soardt = 0 
         GROUP  BY soord 
         ORDER  BY 1) SELECT a.orodr# orodr_, 
       orcust, 
       orldat, 
       CASE orpdat 
         WHEN 0 THEN Varchar(0) 
         ELSE Varchar_format(CHAR(orpdat), 'YYYY-MM-DD') 
       END      erdat2, 
       CASE orptim 
         WHEN 0 THEN Varchar(0) 
         ELSE CONCAT(CONCAT(SUBSTRING(orptim, 1, 2), ':'), 
              SUBSTRING(orptim, 3, 4)) 
       END      AS ertim2, 
       CASE orapdt 
         WHEN 0 THEN Varchar(0) 
         ELSE Varchar_format(CHAR(orapdt), 'YYYY-MM-DD') 
       END      ltdat2, 
       CASE oraptm 
         WHEN 0 THEN Varchar(0) 
         ELSE CONCAT(CONCAT(SUBSTRING(oraptm, 1, 2), ':'), 
              SUBSTRING(oraptm, 3, 4)) 
       END      AS lttim2, 
       CASE orddat 
         WHEN 0 THEN Varchar(0) 
         ELSE Varchar_format(CHAR(orddat), 'YYYY-MM-DD') 
       END      dldat2, 
       CASE ordtim 
         WHEN 0 THEN Varchar(0) 
         ELSE CONCAT(CONCAT(SUBSTRING(ordtim, 1, 2), ':'), 
              SUBSTRING(ordtim, 3, 4)) 
       END      AS dltim2, 
       orestr, 
       a.ordv#  ordv_, 
       orcons, 
       oreqty, 
       orspec, 
       a.orstp# orstp_, 
       orcomc, 
       ( CASE 
           WHEN mmtotal IS NOT NULL THEN mmtotal 
           ELSE 0 
         END )  mmtotal, 
       orwgt, 
       orocty, 
       orost, 
       ordcty, 
       ordst, 
       orara    asarea, 
       orpdrv   oprdrv, 
       orld#    orld_, 
       ordsp#   ordsp_, 
       orshdt, 
       orshtm, 
       ornwpk, 
       ( CASE 
           WHEN equipmentnumber IS NOT NULL THEN equipmentnumber 
           ELSE '' 
         END )  EquipmentNumber, 
       ( CASE 
           WHEN apptreq IS NOT NULL THEN apptreq 
           ELSE 'N' 
         END )  apptreq, 
       ( CASE 
           WHEN apptmade IS NOT NULL THEN apptmade 
           ELSE 'N' 
         END )  apptmade, 
       ( CASE 
           WHEN ununit IS NOT NULL THEN ununit 
           ELSE ' ' 
         END )  UNUNIT, 
       ( CASE 
           WHEN unsupr IS NOT NULL THEN unsupr 
           ELSE ' ' 
         END )  asdrmgr, 
       ( CASE 
           WHEN unfmgr IS NOT NULL THEN unfmgr 
           ELSE ' ' 
         END )  asflmgr, 
       ( CASE 
           WHEN untrl1 IS NOT NULL THEN untrl1 
           ELSE ' ' 
         END )  UNTRL1 
FROM   iesqafile.order a 
       LEFT OUTER JOIN ordermiles 
                    ON mmord# = a.orodr# 
       LEFT OUTER JOIN firstequiprow 
                    ON a.orodr# = oeord# 
       LEFT OUTER JOIN stopgroup 
                    ON a.orodr# = stoporder 
       LEFT OUTER JOIN iesqafile.units 
                    ON ununit = a.orpdrv 
WHERE  orld# <> ordsp# 
       AND ( orspec = 'N# C' 
              OR orspec = 'N# P' ) 
       AND orpdrv <> '' 
       AND orpdat >= 2018001 
UNION 
SELECT d.orodr# as ordddd, 
       orcust, 
       orldat, 
       CASE orpdat 
         WHEN 0 THEN Varchar(0) 
         ELSE Varchar_format(CHAR(orpdat), 'YYYY-MM-DD') 
       END     erdat2, 
       CASE orptim 
         WHEN 0 THEN Varchar(0) 
         ELSE CONCAT(CONCAT(SUBSTRING(orptim, 1, 2), ':'), 
              SUBSTRING(orptim, 3, 4)) 
       END     AS ertim2, 
       CASE orapdt 
         WHEN 0 THEN Varchar(0) 
         ELSE Varchar_format(CHAR(orapdt), 'YYYY-MM-DD') 
       END     ltdat2, 
       CASE oraptm 
         WHEN 0 THEN Varchar(0) 
         ELSE CONCAT(CONCAT(SUBSTRING(oraptm, 1, 2), ':'), 
              SUBSTRING(oraptm, 3, 4)) 
       END     AS lttim2, 
       CASE orddat 
         WHEN 0 THEN Varchar(0) 
         ELSE Varchar_format(CHAR(orddat), 'YYYY-MM-DD') 
       END     dldat2, 
       CASE ordtim 
         WHEN 0 THEN Varchar(0) 
         ELSE CONCAT(CONCAT(SUBSTRING(ordtim, 1, 2), ':'), 
              SUBSTRING(ordtim, 3, 4)) 
       END     AS dltim2, 
       orestr, 
       ordv#, 
       orcons, 
       oreqty, 
       orspec, 
       orstp#, 
       orcomc, 
       ( CASE 
           WHEN mmtotal IS NOT NULL THEN mmtotal 
           ELSE 0 
         END ) mmtotal, 
       orwgt, 
       orocty, 
       orost, 
       ordcty, 
       ordst, 
       orara, 
       orpdrv, 
       orld#, 
       ordsp#, 
       orshdt, 
       orshtm, 
       ornwpk, 
       ( CASE 
           WHEN equipmentnumber IS NOT NULL THEN equipmentnumber 
           ELSE ' ' 
         END ) EquipmentNumber, 
       ( CASE 
           WHEN apptreq IS NOT NULL THEN apptreq 
           ELSE 'N' 
         END ) apptreq, 
       ( CASE 
           WHEN apptmade IS NOT NULL THEN apptmade 
           ELSE 'N' 
         END ) apptmade, 
       ( CASE 
           WHEN ununit IS NOT NULL THEN ununit 
           ELSE ' ' 
         END ) UNUNIT, 
       ( CASE 
           WHEN unsupr IS NOT NULL THEN unsupr 
           ELSE ' ' 
         END ) UNSUPR, 
       ( CASE 
           WHEN unfmgr IS NOT NULL THEN unfmgr 
           ELSE ' ' 
         END ) UNFMGR, 
       ( CASE 
           WHEN untrl1 IS NOT NULL THEN untrl1 
           ELSE ' ' 
         END ) UNTRL1 
FROM   iesqafile.opplan 
       LEFT OUTER JOIN iesqafile.order d 
                    ON d.orodr# = opord# 
       LEFT OUTER JOIN ordermiles 
                    ON mmord# = d.orodr# 
       LEFT OUTER JOIN firstequiprow 
                    ON d.orodr# = oeord# 
       LEFT OUTER JOIN stopgroup 
                    ON d.orodr# = stoporder 
       LEFT OUTER JOIN iesqafile.units 
                    ON ununit = d.orpdrv

所以我第一次尝试创建视图只是在第一条语句的开头添加一个 CREATE VIEW AS。这导致了以下错误:

A column list must be specified because the result columns are unnamed.

我无法理解这个错误,因为确实指定了列标题,因为 select 可以正常工作。所有列都显示有适当的标题。

但是我尝试通过添加以下内容而不是简单的创建视图来修改查询。

create view pavt.v1 (v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,v16,v17,v18,v19,v20,v21,v22,v23,v24,v25,v26,v27,v28,v29,v30,v31,v32,v33,v34,v35,v36) as

这似乎奏效了,视图是在库 PAVT 中创建的。但是,当我在视图上 运行 a select 时,我得到一个算术溢出错误,DB2 错误代码为 -802。

我不明白这一点,因为我能够查看结果,但问题仅在尝试创建查询时出现。有人可以指导吗?

更详细的错误描述如下:

 Message . . . . :   Select or omit error on field                             
   Cast(Translate(ORDER_14.ORPTIM, *UNNAMED Table) AS member V.                
 Cause . . . . . :   A select or omit error occurred in record 0, record format
   *FIRST, member number 1 of file V in library PAVT, because of condition 1 of
   the following conditions:                                                   
     1 - The data was not valid in a decimal field.                            

此刻我真正想知道的是我创建视图所遵循的方法是否正确(至少就语法而言)。也许我看到的错误是数据错误?

编辑-1:

create view pavt.CCC1 as
WITH firstequiprrn 
     AS (SELECT oeord#      equiporder, 
                Min(RRN(a)) firstrow 
         FROM   iesqafile.OPEQUIP    a 
         GROUP  BY oeord# 
         ORDER  BY 1), 
     firstequiprow 
     AS (SELECT oeord#, 
                oetrlr equipmentnumber 
         FROM   firstequiprrn 
                INNER JOIN iesqafile.OPEQUIP    b 
                        ON equiporder = oeord# 
                           AND firstrow = RRN(b)), 
     ordermiles 
     AS (SELECT mmord#, 
                mmtotal 
         FROM   iesqafile.mmiles 
         WHERE  mmord# IN(SELECT orodr# 
                          FROM   iesqafile.order) 
                AND mmrectype = 'O' 
                AND mmdsp# = '00'), 
     stopgroup 
     AS (SELECT soord       stoporder, 
                COUNT(*)    stopsremain, 
                Min(sostp#) nextstop, 
                Max(soappr) apptreq, 
                Max(soaptm) apptmade 
         FROM   iesqafile.stopoff 
                INNER JOIN iesqafile.order 
                        ON orodr# = soord 
         WHERE  soardt = 0 
         GROUP  BY soord 
         ORDER  BY 1) SELECT a.orodr# orodr_, 
       orcust, 
       orldat, 
       CASE orpdat 
         WHEN 0 THEN Varchar(0) 
         ELSE Varchar_format(CHAR(orpdat), 'YYYY-MM-DD') 
       END      erdat2, 
       CASE orptim 
         WHEN 0 THEN Varchar(0) 
         ELSE CONCAT(CONCAT(SUBSTRING(char(orptim), 1, 2), ':'), 
              SUBSTRING(char(orptim), 3, 4)) 
       END      AS ertim2, 
       CASE orapdt 
         WHEN 0 THEN Varchar(0) 
         ELSE Varchar_format(CHAR(orapdt), 'YYYY-MM-DD') 
       END      ltdat2, 
       CASE oraptm 
         WHEN 0 THEN Varchar(0) 
         ELSE CONCAT(CONCAT(SUBSTRING(oraptm, 1, 2), ':'), 
              SUBSTRING(oraptm, 3, 4)) 
       END      AS lttim2, 
       CASE orddat 
         WHEN 0 THEN Varchar(0) 
         ELSE Varchar_format(CHAR(orddat), 'YYYY-MM-DD') 
       END      dldat2, 
       CASE ordtim 
         WHEN 0 THEN Varchar(0) 
         ELSE CONCAT(CONCAT(SUBSTRING(ordtim, 1, 2), ':'), 
              SUBSTRING(ordtim, 3, 4)) 
       END      AS dltim2, 
       orestr, 
       a.ordv#  ordv_, 
       orcons, 
       oreqty, 
       orspec, 
       a.orstp# orstp_, 
       orcomc, 
       ( CASE 
           WHEN mmtotal IS NOT NULL THEN mmtotal 
           ELSE 0 
         END )  mmtotal, 
       orwgt, 
       orocty, 
       orost, 
       ordcty, 
       ordst, 
       orara    asarea, 
       orpdrv   oprdrv, 
       orld#    orld_, 
       ordsp#   ordsp_, 
       orshdt, 
       orshtm, 
       ornwpk, 
       ( CASE 
           WHEN equipmentnumber IS NOT NULL THEN equipmentnumber 
           ELSE '' 
         END )  EquipmentNumber, 
       ( CASE 
           WHEN apptreq IS NOT NULL THEN apptreq 
           ELSE 'N' 
         END )  apptreq, 
       ( CASE 
           WHEN apptmade IS NOT NULL THEN apptmade 
           ELSE 'N' 
         END )  apptmade, 
       ( CASE 
           WHEN ununit IS NOT NULL THEN ununit 
           ELSE ' ' 
         END )  UNUNIT, 
       ( CASE 
           WHEN unsupr IS NOT NULL THEN unsupr 
           ELSE ' ' 
         END )  asdrmgr, 
       ( CASE 
           WHEN unfmgr IS NOT NULL THEN unfmgr 
           ELSE ' ' 
         END )  asflmgr, 
       ( CASE 
           WHEN untrl1 IS NOT NULL THEN untrl1 
           ELSE ' ' 
         END )  UNTRL1 
FROM   iesqafile.order a 
       LEFT OUTER JOIN ordermiles 
                    ON mmord# = a.orodr# 
       LEFT OUTER JOIN firstequiprow 
                    ON a.orodr# = oeord# 
       LEFT OUTER JOIN stopgroup 
                    ON a.orodr# = stoporder 
       LEFT OUTER JOIN iesqafile.units 
                    ON ununit = a.orpdrv 
WHERE  orld# <> ordsp# 
       AND ( orspec = 'N# C' 
              OR orspec = 'N# P' ) 
       AND orpdrv <> '' 
       AND orpdat >= 2018001 
UNION 
SELECT d.orodr# orodr_, 
       orcust, 
       orldat, 
       CASE orpdat 
         WHEN 0 THEN Varchar(0) 
         ELSE Varchar_format(CHAR(orpdat), 'YYYY-MM-DD') 
       END     erdat2, 
       CASE orptim 
         WHEN 0 THEN Varchar(0) 
         ELSE CONCAT(CONCAT(SUBSTRING(char(orptim), 1, 2), ':'), 
              SUBSTRING(char(orptim), 3, 4)) 
       END     AS ertim2, 
       CASE orapdt 
         WHEN 0 THEN Varchar(0) 
         ELSE Varchar_format(CHAR(orapdt), 'YYYY-MM-DD') 
       END     ltdat2, 
       CASE oraptm 
         WHEN 0 THEN Varchar(0) 
         ELSE CONCAT(CONCAT(SUBSTRING(oraptm, 1, 2), ':'), 
              SUBSTRING(oraptm, 3, 4)) 
       END     AS lttim2, 
       CASE orddat 
         WHEN 0 THEN Varchar(0) 
         ELSE Varchar_format(CHAR(orddat), 'YYYY-MM-DD') 
       END     dldat2, 
       CASE ordtim 
         WHEN 0 THEN Varchar(0) 
         ELSE CONCAT(CONCAT(SUBSTRING(ordtim, 1, 2), ':'), 
              SUBSTRING(ordtim, 3, 4)) 
       END     AS dltim2, 
       orestr, 
       ordv# ordv_, 
       orcons, 
       oreqty, 
       orspec, 
       orstp# orstp_, 
       orcomc, 
       ( CASE 
           WHEN mmtotal IS NOT NULL THEN mmtotal 
           ELSE 0 
         END ) mmtotal, 
       orwgt, 
       orocty, 
       orost, 
       ordcty, 
       ordst, 
       orara asarea, 
       orpdrv oprdrv, 
       orld# orld_, 
       ordsp# ordsp_, 
       orshdt, 
       orshtm, 
       ornwpk, 
       ( CASE 
           WHEN equipmentnumber IS NOT NULL THEN equipmentnumber 
           ELSE ' ' 
         END ) EquipmentNumber, 
       ( CASE 
           WHEN apptreq IS NOT NULL THEN apptreq 
           ELSE 'N' 
         END ) apptreq, 
       ( CASE 
           WHEN apptmade IS NOT NULL THEN apptmade 
           ELSE 'N' 
         END ) apptmade, 
       ( CASE 
           WHEN ununit IS NOT NULL THEN ununit 
           ELSE ' ' 
         END ) UNUNIT, 
       ( CASE 
           WHEN unsupr IS NOT NULL THEN unsupr 
           ELSE ' ' 
         END ) asdrmgr, 
       ( CASE 
           WHEN unfmgr IS NOT NULL THEN unfmgr 
           ELSE ' ' 
         END ) asflmgr, 
       ( CASE 
           WHEN untrl1 IS NOT NULL THEN untrl1 
           ELSE ' ' 
         END ) UNTRL1 
FROM   iesqafile.opplan 
       LEFT OUTER JOIN iesqafile.order d 
                    ON d.orodr# = opord# 
       LEFT OUTER JOIN ordermiles 
                    ON mmord# = d.orodr# 
       LEFT OUTER JOIN firstequiprow 
                    ON d.orodr# = oeord# 
       LEFT OUTER JOIN stopgroup 
                    ON d.orodr# = stoporder 
       LEFT OUTER JOIN iesqafile.units 
                    ON ununit = d.orpdrv

第一个错误是因为在您的 2 个主要 SELECT 语句(联合在一起)中您有不同的列名 - 因此视图不知道如何称呼它们并且需要您明确命名它们例如每条语句的第一列:

SELECT a.orodr# orodr_ ...
SELECT d.orodr# as ordddd ...

对于第二个错误,ORPTIM 列的数据类型是什么?

我猜是这种结构导致了问题:

CASE orptim
        WHEN 0
            THEN Varchar(0)
            ELSE CONCAT(CONCAT(SUBSTRING(orptim, 1, 2), ':'), SUBSTRING(orptim, 3, 4))

如果 orptim 是 number/decimal,则“WHEN 0”将起作用,但“SUBSTRING(orptim, 1, 2)”将不起作用 - 因为您不能对数字进行子字符串化。如果 orptim 是一个字符串,那么 "WHEN 0" w= 可能会导致问题。