在DB2中创建视图



我有一个DB2SQL语句,它成功地选择了所需的行。由于这是一个耗时的查询,我正尝试使用createview语句将其转换为视图。

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.

我无法理解这个错误,因为列标题确实被指定为没有问题的精选作品。所有列都以适当的标题显示。

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

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中创建的。然而,当我在视图上运行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

第一个错误是因为在您的两个主SELECT语句(UNIONed在一起(中,您有不同的列名-因此View不知道该调用它们什么,并要求您显式命名它们,例如每个语句中的第一列:

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

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

我猜是这种类型的构造导致了这个问题:

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

如果吸收比是一个数字/小数;当0〃;将起作用,但";SUBSTRING(吸收比,1,2(";不会,因为你不能用一个数字的子串。如果吸收比是一个字符串;当0〃;w=可能造成问题。

相关内容

  • 没有找到相关文章

最新更新