我有这个SQL,它创建了所需的数据集。我可以在Crystal Reports中使用它;但是,它们允许的行数限制存在一些问题。 (还有其他类似的SQL语句组)。我包含所有代码只是为了了解每个部分有多少行。
然而,4个小组在CR时间方面运行良好。但是,我想把它变成一个存储过程。
问题:
我可以从那里访问存储过程。
示例SQL:
SELECT
count(*) as PROGR5PST,
(
SELECT COALESCE(SUM(OdQty#),0) FROM ASTCCDTA.OEORH48,ASTCCDTA.TRNSTAT2,ASTDTA.OEORD1
WHERE OHCOM# = TSCOM# AND OHORD# = TSORD# AND OHCOM# = ODCOM# AND OHORD# = ODORD#
AND TSSTAT IN('AEP','BGE')
AND OHORDT IN('FCH','FIT')
AND OHREQD < replace(char(current date, iso), '-', '')
AND OHHLDC = ' '
AND ODPRLC = 'ENG'
AND substr(odprt#,1,5) <> 'NOENG' AND OHORD# in(SELECT a.TSORD# FROM ASTCCDTA.TRNSTAT2 a
WHERE a.tsstat IN('AEP','BGE','EAS','REL','STP'))) AS PROGR5PUN,
(SELECT count(*) FROM ASTCCDTA.OEORH48,ASTCCDTA.TRNSTAT2,ASTCCDTA.OETRA99
WHERE OHCOM# = TSCOM# AND OHORD# = TSORD#
AND (otCOM# = OHCOM# AND OTORD#= OHORD# AND ottrnc = 'AQC')
AND TSSTAT IN('AEP','BGE')
AND OHORDT IN('FCH','FIT')
AND OHREQD < replace(char(current date, iso), '-', '') AND OHHLDC = ' ' AND OHORD# in(SELECT a.TSORD# FROM ASTCCDTA.TRNSTAT2 a
WHERE a.tsstat IN('AEP','BGE','EAS','REL','STP'))
) AS PROGR5TOD,
(SELECT COALESCE(SUM(OdQty#),0) FROM ASTCCDTA.OEORH48,ASTCCDTA.TRNSTAT2,ASTDTA.OEORD1
WHERE OHCOM# = TSCOM# AND OHORD# = TSORD# AND OHCOM# = ODCOM# AND OHORD# = ODORD#
AND TSSTAT IN('AEP','BGE')
AND OHORDT IN('FCH','FIT')
AND OHREQD = replace(char(current date, iso), '-', '')
AND OHHLDC = ' '
AND ODPRLC = 'ENG'
AND substr(odprt#,1,5) <> 'NOENG' AND OHORD# in(SELECT a.TSORD# FROM ASTCCDTA.TRNSTAT2 a
WHERE a.tsstat IN('AEP','BGE','EAS','REL','STP'))
) AS PROGR5TUN,
(SELECT count(*) FROM ASTCCDTA.OEORH48,ASTCCDTA.TRNSTAT2,ASTCCDTA.OETRA99
WHERE OHCOM# = TSCOM# AND OHORD# = TSORD#
AND (otCOM# = OHCOM# AND OTORD#= OHORD# AND ottrnc = 'AQC')
AND TSSTAT IN('AEP','BGE')
AND OHORDT IN('FCH','FIT')
AND OHREQD = cast(replace(char(current date + 1 day, iso), '-', '')as int) AND OHHLDC = ' ' AND OHORD# in(SELECT a.TSORD# FROM ASTCCDTA.TRNSTAT2 a
WHERE a.tsstat IN('AEP','BGE','EAS','REL','STP'))
) AS PROGR5TOM,
(SELECT COALESCE(SUM(OdQty#),0) FROM ASTCCDTA.OEORH48,ASTCCDTA.TRNSTAT2,ASTDTA.OEORD1
WHERE OHCOM# = TSCOM# AND OHORD# = TSORD# AND OHCOM# = ODCOM# AND OHORD# = ODORD#
AND TSSTAT IN('AEP','BGE')
AND OHORDT IN('FCH','FIT')
AND OHREQD = cast(replace(char(current date + 1 day, iso), '-', '') as int)
AND OHHLDC = ' '
AND ODPRLC = 'ENG'
AND substr(odprt#,1,5) <> 'NOENG' AND OHORD# in(SELECT a.TSORD# FROM ASTCCDTA.TRNSTAT2 a
WHERE a.tsstat IN('AEP','BGE','EAS','REL','STP'))
) AS PROGR5MUN,
(SELECT count(*) FROM ASTCCDTA.OEORH48,ASTCCDTA.TRNSTAT2,ASTCCDTA.OETRA99
WHERE OHCOM# = TSCOM# AND OHORD# = TSORD#
AND (otCOM# = OHCOM# AND OTORD#= OHORD# AND ottrnc = 'AQC')
AND TSSTAT IN('EAS','REL','SCC')
AND OHORDT IN('FCH','FIT')
AND OHREQD < replace(char(current date, iso), '-', '') AND OHHLDC = ' ' AND OHORD# in(SELECT a.TSORD# FROM ASTCCDTA.TRNSTAT2 a
WHERE a.tsstat IN('AEP','BGE','EAS','REL','STP'))
) AS LASGR5PST,
(SELECT COALESCE(SUM(OdQty#),0) FROM ASTCCDTA.OEORH48,ASTCCDTA.TRNSTAT2,ASTDTA.OEORD1
WHERE OHCOM# = TSCOM# AND OHORD# = TSORD# AND OHCOM# = ODCOM# AND OHORD# = ODORD#
AND TSSTAT IN('EAS','REL','SCC')
AND OHORDT IN('FCH','FIT')
AND OHREQD < replace(char(current date, iso), '-', '')
AND OHHLDC = ' '
AND ODPRLC = 'ENG'
AND substr(odprt#,1,5) <> 'NOENG' AND OHORD# in(SELECT a.TSORD# FROM ASTCCDTA.TRNSTAT2 a
WHERE a.tsstat IN('AEP','BGE','EAS','REL','STP'))
) AS LASGR5PUN,
(SELECT count(*) FROM ASTCCDTA.OEORH48,ASTCCDTA.TRNSTAT2,ASTCCDTA.OETRA99
WHERE OHCOM# = TSCOM# AND OHORD# = TSORD#
AND (otCOM# = OHCOM# AND OTORD#= OHORD# AND ottrnc = 'AQC')
AND TSSTAT IN('EAS','REL','SCC')
AND OHORDT IN('FCH','FIT')
AND OHREQD = replace(char(current date, iso), '-', '') AND OHHLDC = ' ' AND OHORD# in(SELECT a.TSORD# FROM ASTCCDTA.TRNSTAT2 a
WHERE a.tsstat IN('AEP','BGE','EAS','REL','STP'))
) AS LASGR5TOD,
(SELECT COALESCE(SUM(OdQty#),0) FROM ASTCCDTA.OEORH48,ASTCCDTA.TRNSTAT2,ASTDTA.OEORD1
WHERE OHCOM# = TSCOM# AND OHORD# = TSORD# AND OHCOM# = ODCOM# AND OHORD# = ODORD#
AND TSSTAT IN('EAS','REL','SCC')
AND OHORDT IN('FCH','FIT')
AND OHREQD = replace(char(current date, iso), '-', '')
AND OHHLDC = ' '
AND ODPRLC = 'ENG'
AND substr(odprt#,1,5) <> 'NOENG' AND OHORD# in(SELECT a.TSORD# FROM ASTCCDTA.TRNSTAT2 a
WHERE a.tsstat IN('AEP','BGE','EAS','REL','STP'))
) AS LASGR5TUN,
(SELECT count(*) FROM ASTCCDTA.OEORH48,ASTCCDTA.TRNSTAT2,ASTCCDTA.OETRA99
WHERE OHCOM# = TSCOM# AND OHORD# = TSORD#
AND (otCOM# = OHCOM# AND OTORD#= OHORD# AND ottrnc = 'AQC')
AND TSSTAT IN('EAS','REL','SCC')
AND OHORDT IN('FCH','FIT')
AND OHREQD = cast(replace(char(current date + 1 day, iso), '-', '') as int) AND OHHLDC = ' ' AND OHORD# in(SELECT a.TSORD# FROM ASTCCDTA.TRNSTAT2 a
WHERE a.tsstat IN('AEP','BGE','EAS','REL','STP'))
) AS LASGR5TOM,
(SELECT COALESCE(SUM(OdQty#),0) FROM ASTCCDTA.OEORH48,ASTCCDTA.TRNSTAT2,ASTDTA.OEORD1
WHERE OHCOM# = TSCOM# AND OHORD# = TSORD# AND OHCOM# = ODCOM# AND OHORD# = ODORD#
AND TSSTAT IN('EAS','REL','SCC')
AND OHORDT IN('FCH','FIT')
AND OHREQD = cast(replace(char(current date + 1 day, iso), '-', '')as int)
AND OHHLDC = ' '
AND ODPRLC = 'ENG'
AND substr(odprt#,1,5) <> 'NOENG' AND OHORD# in(SELECT a.TSORD# FROM ASTCCDTA.TRNSTAT2 a
WHERE a.tsstat IN('AEP','BGE','EAS','REL','STP'))
) AS LASGR5MUN,
(SELECT count(*) FROM ASTCCDTA.OEORH48,ASTCCDTA.TRNSTAT2,ASTCCDTA.OETRA99
WHERE OHCOM# = TSCOM# AND OHORD# = TSORD#
AND (otCOM# = OHCOM# AND OTORD#= OHORD# AND ottrnc = 'AQC')
AND OHORDT IN('FCH','FIT') AND TSSTAT IN('ECT','STP')
AND OHREQD < replace(char(current date, iso), '-', '')
AND OHHLDC = ' 'AND OHORD# in(SELECT a.TSORD# FROM ASTCCDTA.TRNSTAT2 a
WHERE a.tsstat IN('AEP','BGE','EAS','REL','STP'))
) AS PCKGR5PST,
(SELECT COALESCE(SUM(OdQty#),0) FROM ASTCCDTA.OEORH48,ASTCCDTA.TRNSTAT2,ASTDTA.OEORD1
WHERE OHCOM# = TSCOM# AND OHORD# = TSORD# AND OHCOM# = ODCOM# AND OHORD# = ODORD#
AND TSSTAT IN('ECT','STP')
AND OHORDT IN('FCH','FIT')
AND OHREQD < replace(char(current date, iso), '-', '')
AND OHHLDC = ' ' AND ODPRT# = OdAls# AND substr(odprt#,1,3) <> 'FRT'
AND OHORD# in(SELECT a.TSORD#
FROM ASTCCDTA.TRNSTAT2 a
WHERE a.tsstat IN('AEP','BGE','EAS','REL','STP'))
) AS PCKGR5PUN,
(SELECT count(*) FROM ASTCCDTA.OEORH48,ASTCCDTA.TRNSTAT2,ASTCCDTA.OETRA99
WHERE OHCOM# = TSCOM# AND OHORD# = TSORD#
AND (otCOM# = OHCOM# AND OTORD#= OHORD# AND ottrnc = 'AQC')
AND OHORDT IN('FCH','FIT') AND TSSTAT IN('ECT','STP')
AND OHREQD = replace(char(current date, iso), '-', '')
AND OHHLDC = ' 'AND OHORD# in(SELECT a.TSORD# FROM ASTCCDTA.TRNSTAT2 a
WHERE a.tsstat IN('AEP','BGE','EAS','REL','STP'))
) AS PCKGR5TOD,
(SELECT COALESCE(SUM(OdQty#),0) FROM ASTCCDTA.OEORH48,ASTCCDTA.TRNSTAT2,ASTDTA.OEORD1
WHERE OHCOM# = TSCOM# AND OHORD# = TSORD# AND OHCOM# = ODCOM# AND OHORD# = ODORD#
AND TSSTAT IN('ECT','STP')
AND OHORDT IN('FCH','FIT')
AND OHREQD = replace(char(current date, iso), '-', '')
AND OHHLDC = ' ' AND ODPRT# = OdAls# AND substr(odprt#,1,3) <> 'FRT'
AND OHORD# in(SELECT a.TSORD#
FROM ASTCCDTA.TRNSTAT2 a
WHERE a.tsstat IN('AEP','BGE','EAS','REL','STP'))
) AS PCKGR5TUN,
(SELECT count(*) FROM ASTCCDTA.OEORH48,ASTCCDTA.TRNSTAT2,ASTCCDTA.OETRA99
WHERE OHCOM# = TSCOM# AND OHORD# = TSORD#
AND (otCOM# = OHCOM# AND OTORD#= OHORD# AND ottrnc = 'AQC')
AND OHORDT IN('FCH','FIT') AND TSSTAT IN('ECT','STP')
AND OHREQD = cast(replace(char(current date + 1 day, iso), '-', '') as int)
AND OHHLDC = ' 'AND OHORD# in(SELECT a.TSORD# FROM ASTCCDTA.TRNSTAT2 a
WHERE a.tsstat IN('AEP','BGE','EAS','REL','STP'))
) AS PCKGR5TOM,
(SELECT COALESCE(SUM(OdQty#),0) FROM ASTCCDTA.OEORH48,ASTCCDTA.TRNSTAT2,ASTDTA.OEORD1
WHERE OHCOM# = TSCOM# AND OHORD# = TSORD# AND OHCOM# = ODCOM# AND OHORD# = ODORD#
AND TSSTAT IN('ECT','STP')
AND OHORDT IN('FCH','FIT')
AND OHREQD = cast(replace(char(current date + 1 day, iso), '-', '') as int)
AND OHHLDC = ' ' AND ODPRT# = OdAls# AND substr(odprt#,1,3) <> 'FRT'
AND OHORD# in(SELECT a.TSORD#
FROM ASTCCDTA.TRNSTAT2 a
WHERE a.tsstat IN('AEP','BGE','EAS','REL','STP'))
) AS PCKGR5MUN,
(SELECT count(*) FROM ASTCCDTA.OEORH48,ASTCCDTA.TRNSTAT2,ASTCCDTA.OETRA99
WHERE OHCOM# = TSCOM# AND OHORD# = TSORD#
AND (otCOM# = OHCOM# AND OTORD#= OHORD# AND ottrnc = 'AQC')
AND OHORDT IN('FCH','FIT')
AND TSSTAT IN('EPT')
AND OHREQD < replace(char(current date, iso), '-', '') AND OHHLDC = ' '
AND OHORD# in(SELECT a.TSORD# FROM ASTCCDTA.TRNSTAT2 a
WHERE a.tsstat IN('AEP','BGE','EAS','REL','STP')))AS ASSGR5PST,
(SELECT COALESCE(SUM(OdQty#),0) FROM ASTCCDTA.OEORH48,ASTCCDTA.TRNSTAT2,ASTDTA.OEORD1
WHERE OHCOM# = TSCOM# AND OHORD# = TSORD# AND OHCOM# = ODCOM# AND OHORD# = ODORD#
AND TSSTAT IN('EPT')
AND OHORDT IN('FCH','FIT')
AND OHREQD < replace(char(current date, iso), '-', '')
AND OHHLDC = ' ' AND ODPRT# = OdAls# AND substr(odprt#,1,3) <> 'FRT'
AND OHORD# in(SELECT a.TSORD#
FROM ASTCCDTA.TRNSTAT2 a
WHERE a.tsstat IN('AEP','BGE','EAS','REL','STP'))
) AS ASSGR5PUN,
(SELECT count(*) FROM ASTCCDTA.OEORH48,ASTCCDTA.TRNSTAT2,ASTCCDTA.OETRA99
WHERE OHCOM# = TSCOM# AND OHORD# = TSORD#
AND (otCOM# = OHCOM# AND OTORD#= OHORD# AND ottrnc = 'AQC')
AND OHORDT IN('FCH','FIT')
AND TSSTAT IN('EPT')
AND OHREQD = replace(char(current date, iso), '-', '') AND OHHLDC = ' '
AND OHORD# in(SELECT a.TSORD# FROM ASTCCDTA.TRNSTAT2 a
WHERE a.tsstat IN('AEP','BGE','EAS','REL','STP'))) AS ASSGR5TOD,
(SELECT COALESCE(SUM(OdQty#),0) FROM ASTCCDTA.OEORH48,ASTCCDTA.TRNSTAT2,ASTDTA.OEORD1
WHERE OHCOM# = TSCOM# AND OHORD# = TSORD# AND OHCOM# = ODCOM# AND OHORD# = ODORD#
AND TSSTAT IN('EPT')
AND OHORDT IN('FCH','FIT')
AND OHREQD = replace(char(current date, iso), '-', '')
AND OHHLDC = ' ' AND ODPRT# = OdAls# AND substr(odprt#,1,3) <> 'FRT'
AND OHORD# in(SELECT a.TSORD#
FROM ASTCCDTA.TRNSTAT2 a
WHERE a.tsstat IN('AEP','BGE','EAS','REL','STP'))
) AS ASSGR5TUN,
(SELECT count(*) FROM ASTCCDTA.OEORH48,ASTCCDTA.TRNSTAT2,ASTCCDTA.OETRA99
WHERE OHCOM# = TSCOM# AND OHORD# = TSORD#
AND (otCOM# = OHCOM# AND OTORD#= OHORD# AND ottrnc = 'AQC')
AND OHORDT IN('FCH','FIT')
AND TSSTAT IN('EPT')
AND OHREQD = cast(replace(char(current date + 1 day, iso), '-', '') as int) AND OHHLDC = ' '
AND OHORD# in(SELECT a.TSORD# FROM ASTCCDTA.TRNSTAT2 a
WHERE a.tsstat IN('AEP','BGE','EAS','REL','STP'))
) AS ASSGR5TOM,
(SELECT COALESCE(SUM(OdQty#),0) FROM ASTCCDTA.OEORH48,ASTCCDTA.TRNSTAT2,ASTDTA.OEORD1
WHERE OHCOM# = TSCOM# AND OHORD# = TSORD# AND OHCOM# = ODCOM# AND OHORD# = ODORD#
AND TSSTAT IN('EPT')
AND OHORDT IN('FCH','FIT')
AND OHREQD =cast(replace(char(current date + 1 day, iso), '-', '') as int)
AND OHHLDC = ' ' AND ODPRT# = OdAls# AND substr(odprt#,1,3) <> 'FRT'
AND OHORD# in(SELECT a.TSORD#
FROM ASTCCDTA.TRNSTAT2 a
WHERE a.tsstat IN('AEP','BGE','EAS','REL','STP'))
) AS ASSGR5MUN,
(SELECT count(*) FROM ASTCCDTA.OEORH48,ASTCCDTA.TRNSTAT2,ASTCCDTA.OETRA99
WHERE OHCOM# = TSCOM# AND OHORD# = TSORD#
AND (otCOM# = OHCOM# AND OTORD#= OHORD# AND ottrnc = 'AQC')
AND OHORDT IN('FCH','FIT')
AND TSSTAT IN('CAT')
AND OHREQD < replace(char(current date, iso), '-', '') AND OHHLDC = ' '
AND OHORD# in(SELECT a.TSORD# FROM ASTCCDTA.TRNSTAT2 a
WHERE a.tsstat IN('AEP','BGE','EAS','REL','STP'))
) AS SHPGR5PST,
(SELECT COALESCE(SUM(OdQty#),0) FROM ASTCCDTA.OEORH48,ASTCCDTA.TRNSTAT2,ASTDTA.OEORD1
WHERE OHCOM# = TSCOM# AND OHORD# = TSORD# AND OHCOM# = ODCOM# AND OHORD# = ODORD#
AND TSSTAT IN('CAT')
AND OHORDT IN('FCH','FIT')
AND OHREQD < replace(char(current date, iso), '-', '')
AND OHHLDC = ' ' AND ODPRT# = OdAls# AND substr(odprt#,1,3) <> 'FRT'
AND OHORD# in(SELECT a.TSORD#
FROM ASTCCDTA.TRNSTAT2 a
WHERE a.tsstat IN('AEP','BGE','EAS','REL','STP'))
) AS SHPGR5PUN,
(SELECT count(*) FROM ASTCCDTA.OEORH48,ASTCCDTA.TRNSTAT2,ASTCCDTA.OETRA99
WHERE OHCOM# = TSCOM# AND OHORD# = TSORD#
AND (otCOM# = OHCOM# AND OTORD#= OHORD# AND ottrnc = 'AQC')
AND OHORDT IN('FCH','FIT')
AND TSSTAT IN('CAT')
AND OHREQD = replace(char(current date, iso), '-', '') AND OHHLDC = ' '
AND OHORD# in(SELECT a.TSORD# FROM ASTCCDTA.TRNSTAT2 a
WHERE a.tsstat IN('AEP','BGE','EAS','REL','STP'))
) AS SHPGR5TOD,
(SELECT COALESCE(SUM(OdQty#),0) FROM ASTCCDTA.OEORH48,ASTCCDTA.TRNSTAT2,ASTDTA.OEORD1
WHERE OHCOM# = TSCOM# AND OHORD# = TSORD# AND OHCOM# = ODCOM# AND OHORD# = ODORD#
AND TSSTAT IN('CAT')
AND OHORDT IN('FCH','FIT')
AND OHREQD = replace(char(current date, iso), '-', '')
AND OHHLDC = ' ' AND ODPRT# = OdAls# AND substr(odprt#,1,3) <> 'FRT'
AND OHORD# in(SELECT a.TSORD#
FROM ASTCCDTA.TRNSTAT2 a
WHERE a.tsstat IN('AEP','BGE','EAS','REL','STP'))
) AS SHPGR5TUN,
(SELECT count(*) FROM ASTCCDTA.OEORH48,ASTCCDTA.TRNSTAT2,ASTCCDTA.OETRA99
WHERE OHCOM# = TSCOM# AND OHORD# = TSORD#
AND (otCOM# = OHCOM# AND OTORD#= OHORD# AND ottrnc = 'AQC')
AND OHORDT IN('FCH','FIT')
AND TSSTAT IN('CAT')
AND OHREQD = cast(replace(char(current date + 1 day, iso), '-', '') as int) AND OHHLDC = ' '
AND OHORD# in(SELECT a.TSORD# FROM ASTCCDTA.TRNSTAT2 a
WHERE a.tsstat IN('AEP','BGE','EAS','REL','STP'))
) AS SHPGR5TOM,
(SELECT COALESCE(SUM(OdQty#),0) FROM ASTCCDTA.OEORH48,ASTCCDTA.TRNSTAT2,ASTDTA.OEORD1
WHERE OHCOM# = TSCOM# AND OHORD# = TSORD# AND OHCOM# = ODCOM# AND OHORD# = ODORD#
AND TSSTAT IN('CAT')
AND OHORDT IN('FCH','FIT')
AND OHREQD = cast(replace(char(current date + 1 day, iso), '-', '') as int)
AND OHHLDC = ' ' AND ODPRT# = OdAls# AND substr(odprt#,1,3) <> 'FRT'
AND OHORD# in(SELECT a.TSORD#
FROM ASTCCDTA.TRNSTAT2 a
WHERE a.tsstat IN('AEP','BGE','EAS','REL','STP'))
) AS SHPGR5MUN,
(SELECT count(*) FROM ASTCCDTA.OEORH48
WHERE OHORDT IN('FCH','FIT')
AND OHREQD < replace(char(current date, iso), '-', '') AND OHHLDC <> ' '
) AS HLDGR5PST,
(SELECT COALESCE(SUM(OdQty#),0) FROM ASTCCDTA.OEORH48,ASTDTA.OEORD1
WHERE OHCOM# = ODCOM# AND OHORD# = ODORD# AND ODPRT# = OdAls#
AND substr(odprt#,1,3) <> 'FRT'AND OHORDT IN('FCH','FIT')
AND OHREQD <replace(char(current date, iso), '-', '') AND OHHLDC <> ' '
) AS HLDGR5PUN,
(SELECT count(*) FROM ASTCCDTA.OEORH48
WHERE OHORDT IN('FCH','FIT')
AND OHREQD = replace(char(current date, iso), '-', '') AND OHHLDC <> ' '
) AS HLDGR5TOD,
(SELECT COALESCE(SUM(OdQty#),0) FROM ASTCCDTA.OEORH48,ASTDTA.OEORD1
WHERE OHCOM# = ODCOM# AND OHORD# = ODORD# AND ODPRT# = OdAls#
AND substr(odprt#,1,3) <> 'FRT'AND OHORDT IN('FCH','FIT')
AND OHREQD =replace(char(current date, iso), '-', '') AND OHHLDC <> ' '
) AS HLDGR5TUN,
(SELECT count(*) FROM ASTCCDTA.OEORH48
WHERE OHORDT IN('FCH','FIT')
AND OHREQD =cast(Right(Replace(Char(current date + 1 day, ISO), '-', ''),8)as int) AND OHHLDC <> ' '
) AS HLDGR5TOM,
(SELECT COALESCE(SUM(OdQty#),0) FROM ASTCCDTA.OEORH48,ASTDTA.OEORD1
WHERE OHCOM# = ODCOM# AND OHORD# = ODORD# AND ODPRT# = OdAls#
AND substr(odprt#,1,3) <> 'FRT'AND OHORDT IN('FCH','FIT')
AND OHREQD =cast(Right(Replace(Char(current date + 1 day, ISO), '-', ''),8)as int) AND OHHLDC <> ' '
) AS HLDGR5MUN
FROM ASTCCDTA.OEORH48,ASTCCDTA.TRNSTAT2,ASTCCDTA.OETRA99
WHERE OHCOM# = TSCOM# AND OHORD# = TSORD#
AND (otCOM# = OHCOM# AND OTORD#= OHORD# AND ottrnc = 'AQC')
AND TSSTAT IN('AEP','BGE')
AND OHORDT IN('FCH','FIT')
AND OHREQD < replace(char(current date, iso), '-', '') AND OHHLDC = ' ' AND OHORD# in(SELECT a.TSORD# FROM ASTCCDTA.TRNSTAT2 a
WHERE a.tsstat IN('AEP','BGE','EAS','REL','STP'))
答案 0 :(得分:2)
由于常量子查询,这个查询不仅过长,而且可能比它需要的要慢得多。你实际上应该能够将它们中的大部分结合起来,这应该有助于这两个方面。
请注意,我猜测哪些表包含哪些字段。这是唯一可能的,因为很明显iSeries使用特定于表的前缀的传统是有效的。这是一个长期的坏事,你最好使用专有名称和使用别名。
查看一些示例数据也非常有用,因为这会影响必须预先聚合的内容,以及可以保留的内容。我也担心你可能有一个或多个拼写错误 - 总和正在进行不同比较的子查询比计数的那些(ODPRLC = 'ENG'
对ottrnc = 'AQC'
)。这意味着聚合体不会测量相同的东西,这是有问题的。
警告 - 如果没有更多信息,我无法知道这是否有效。接下来的一切都是猜测,简单明了。
首先,让我们为日期范围构建一个小型虚拟表:
WITH Time_Slice AS (SELECT slice,
CAST(REPLACE(CHAR(s, ISO), '-', '') AS INT) AS startsOn,
CAST(REPLACE(CHAR(e, ISO), '-', '') AS INT) AS endsOn
FROM (VALUES (1, CAST('0001-01-01' AS DATE), CURRENT_DATE),
(2, CURRENT_DATE, CURRENT_DATE + 1 DAY),
(3, CURRENT_DATE + 1 DAY, CURRENT_DATE + 2 DAYS))
n(slice, s, e))
我假设与(OHREQD
)进行比较的列实际上是整数,而不是某种类型的字符 - 您与比较不一致。请注意,如果该列更改为实际的DATE
类型,则可以轻松修改查询。
我们还想要一个可用于对事务状态进行分组的表:
Status_Group AS (SELECT grouping, status
FROM (VALUES ('PRO', 'AEP'),
('PRO', 'BGE'),
('LAS', 'EAS'),
('LAS', 'REL'),
('LAS', 'SCC'),
('PCK', 'ECT'),
('PCK', 'STP'),
('ASS', 'EPT'),
('SHP', 'CAT')) g(grouping, status))
表OEORH48
是“常量” - 它总是对它进行相同的比较。这意味着它返回的行将始终相同。我们不会把它变成CTE,因为加入它的表格。但是,查询的新基础是:
SELECT ....
FROM ASTCCDTA.OEORH48 Order_History
JOIN Time_Slice
ON Order_History.ohReqD >= Time_Slice.startsOn
AND Order_History.ohReqD < Time_Slice.endsOn
......
WHERE Order_History.ohOrdT IN('FCH', 'FIT')
AND Order_History.ohHldC = ' '
(我们将利用稍后公开的Time_Slice
。)
以下子子查询存在问题:
SELECT a.TSORD#
FROM ASTCCDTA.TRNSTAT2 a
WHERE a.tsstat IN('AEP','BGE','EAS','REL','STP')
如果我们假设TSORD#
在给定的表中是唯一的,那么它是不必要的(因为你已经在外部子查询中有条件),并且在某些情况下实际上对你不利。如果不是,那么有时候同一条记录将被视为两次,这似乎是......不必要的。我会尽可能地消除它,但不能完全消除它。无论如何,我们只需要获取此信息一次,创建另一个CTE(这是有原因的):
Other_Transaction_Status AS (SELECT DISTINCT tsOrd#
FROM ASTCCDTA.TRNSTAT2
WHERE tsStat IN ('AEP','BGE','EAS','REL','STP'))
这是因为最后一组列的相互作用(不关心其他状态)。
表TRNSTAT2
也是不变的。对于分组'AEP', 'BGE'
,'EAS', 'REL', 'SCC'
,'ECT', 'STP'
,'EPT'
和'CAT'
,每次重复6次。我们可以将所有这些结合起来:
.........
JOIN ASTCCDTA.TRNSTAT2 Transaction_Status
ON Transaction_Status.tsCom# = Order_History.ohCom#
AND Transaction_Status.tsOrd# = Order_History.ohOrd#
AND Transaction_Status.tsStat IN('AEP', 'BGE', 'EAS', 'REL', 'SCC', 'ECT', 'STP', 'EPT', 'CAT')
..............
我们的两个“详细信息”表格OEORD1
和OETRA99
会重复多次。我们可以将它们减少到这些CTE(不确定我选择的名称是否正确):
Order_Detail_ENG AS (SELECT odCom#, odOrd#, SUM(odQty#) AS quantity
FROM ASTDTA.OEORD1
WHERE odPrLC = 'ENG'
AND SUBSTR(odPrt#, 1, 5) <> 'NOENG'
GROUP BY odCom#, odOrd#)
Order_Detail_Other AS (SELECT odCom#, odOrd#, SUM(odQty#) AS quantity
FROM ASTDTA.OEORD1
WHERE odPrT# = OdAls#
AND SUBSTR(odPrT#, 1, 3) <> 'FRT'
GROUP BY odCom#, odOrd#)
Order_Transaction AS (SELECT otCom#, otOrd#, COUNT(*) as quantity
FROM ASTCCDTA.OETRA99
WHERE otTrnC = 'AQC'
GROUP BY otCom#, otOrd#)
所有这些都被引用了30次。
现在,为了将所有内容放在一起,我们将使用SUM(...)
和COUNT(<expression>)
的良好属性 - 它们忽略空行!只要存在至少一行,它们就会返回数字结果。有了这个,我们可以关闭计算的行数:
WITH Time_Slice AS (SELECT slice,
CAST(REPLACE(CHAR(s, ISO), '-', '') AS INT) AS startsOn,
CAST(REPLACE(CHAR(e, ISO), '-', '') AS INT) AS endsOn
FROM (VALUES (1, CAST('0001-01-01' AS DATE), CURRENT_DATE),
(2, CURRENT_DATE, CURRENT_DATE + 1 DAY),
(3, CURRENT_DATE + 1 DAY, CURRENT_DATE + 2 DAYS))
n(slice, s, e)),
Status_Group AS (SELECT grouping, status
FROM (VALUES ('PRO', 'AEP'),
('PRO', 'BGE'),
('LAS', 'EAS'),
('LAS', 'REL'),
('LAS', 'SCC'),
('PCK', 'ECT'),
('PCK', 'STP'),
('ASS', 'EPT'),
('SHP', 'CAT')) g(grouping, status)),
Other_Transaction_Status AS (SELECT DISTINCT tsOrd#
FROM ASTCCDTA.TRNSTAT2
WHERE tsStat IN ('AEP','BGE','EAS','REL','STP')),
Order_Detail_ENG AS (SELECT odCom#, odOrd#, SUM(odQty#) AS quantity
FROM ASTDTA.OEORD1
WHERE odPrLC = 'ENG'
AND SUBSTR(odPrt#, 1, 5) <> 'NOENG'
GROUP BY odCom#, odOrd#),
Order_Detail_Other AS (SELECT odCom#, odOrd#, SUM(odQty#) AS quantity
FROM ASTDTA.OEORD1
WHERE odPrT# = OdAls#
AND SUBSTR(odPrT#, 1, 3) <> 'FRT'
GROUP BY odCom#, odOrd#),
Order_Transaction AS (SELECT otCom#, otOrd#, COUNT(*) as quantity
FROM ASTCCDTA.OETRA99
WHERE otTrnC = 'AQC'
GROUP BY otCom#, otOrd#)
SELECT SUM(CASE WHEN Time_Slice.slice = 1
AND Status_Group.grouping = 'PRO'
THEN Order_Transaction.quantity END) AS PROGR5PST,
SUM(CASE WHEN Time_Slice.slice = 1
AND Status_Group.grouping = 'PRO'
THEN Order_Detail_ENG.quantity END) AS PROGR5PUN,
SUM(CASE WHEN Time_Slice.slice = 2
AND Status_Group.grouping = 'PRO'
THEN Order_Transaction.quantity END) AS PROGR5TOD,
SUM(CASE WHEN Time_Slice.slice = 2
AND Status_Group.grouping = 'PRO'
THEN Order_Detail_ENG.quantity END) AS PROGR5TUN,
SUM(CASE WHEN Time_Slice.slice = 3
AND Status_Group.grouping = 'PRO'
THEN Order_Transaction.quantity END) AS PROGR5TOM,
SUM(CASE WHEN Time_Slice.slice = 3
AND Status_Group.grouping = 'PRO'
THEN Order_Detail_ENG.quantity END) AS PROGR5MUN,
SUM(CASE WHEN Time_Slice.slice = 1
AND Status_Group.grouping = 'LAS'
THEN Order_Transaction.quantity END) AS LASGR5PST,
SUM(CASE WHEN Time_Slice.slice = 1
AND Status_Group.grouping = 'LAS'
THEN Order_Detail_ENG.quantity END) AS LASGR5PUN,
SUM(CASE WHEN Time_Slice.slice = 2
AND Status_Group.grouping = 'LAS'
THEN Order_Transaction.quantity END) AS LASGR5TOD,
SUM(CASE WHEN Time_Slice.slice = 2
AND Status_Group.grouping = 'LAS'
THEN Order_Detail_ENG.quantity END) AS LASGR5TUN,
SUM(CASE WHEN Time_Slice.slice = 3
AND Status_Group.grouping = 'LAS'
THEN Order_Transaction.quantity END) AS LASGR5TOM,
SUM(CASE WHEN Time_Slice.slice = 3
AND Status_Group.grouping = 'LAS'
THEN Order_Detail_ENG.quantity END) AS LASGR5MUN,
SUM(CASE WHEN Time_Slice.slice = 1
AND Status_Group.grouping = 'PCK'
THEN Order_Transaction.quantity END) AS PCKGR5PST,
SUM(CASE WHEN Time_Slice.slice = 1
AND Status_Group.grouping = 'PCK'
THEN Order_Detail_Other.quantity END) AS PCKGR5PUN,
SUM(CASE WHEN Time_Slice.slice = 2
AND Status_Group.grouping = 'PCK'
THEN Order_Transaction.quantity END) AS PCKGR5TOD,
SUM(CASE WHEN Time_Slice.slice = 2
AND Status_Group.grouping = 'PCK'
THEN Order_Detail_Other.quantity END) AS PCKGR5TUN,
SUM(CASE WHEN Time_Slice.slice = 3
AND Status_Group.grouping = 'PCK'
THEN Order_Transaction.quantity END) AS PCKGR5TOM,
SUM(CASE WHEN Time_Slice.slice = 3
AND Status_Group.grouping = 'PCK'
THEN Order_Detail_Other.quantity END) AS PCKGR5MUN,
SUM(CASE WHEN Time_Slice.slice = 1
AND Status_Group.grouping = 'ASS'
THEN Order_Transaction.quantity END) AS ASSGR5PST,
SUM(CASE WHEN Time_Slice.slice = 1
AND Status_Group.grouping = 'ASS'
THEN Order_Detail_Other.quantity END) AS ASSGR5PUN,
SUM(CASE WHEN Time_Slice.slice = 2
AND Status_Group.grouping = 'ASS'
THEN Order_Transaction.quantity END) AS ASSGR5TOD,
SUM(CASE WHEN Time_Slice.slice = 2
AND Status_Group.grouping = 'ASS'
THEN Order_Detail_Other.quantity END) AS ASSGR5TUN,
SUM(CASE WHEN Time_Slice.slice = 3
AND Status_Group.grouping = 'ASS'
THEN Order_Transaction.quantity END) AS ASSGR5TOM,
SUM(CASE WHEN Time_Slice.slice = 3
AND Status_Group.grouping = 'ASS'
THEN Order_Detail_Other.quantity END) AS ASSGR5MUN,
SUM(CASE WHEN Time_Slice.slice = 1
AND Status_Group.grouping = 'SHP'
THEN Order_Transaction.quantity END) AS SHPGR5PST,
SUM(CASE WHEN Time_Slice.slice = 1
AND Status_Group.grouping = 'SHP'
THEN Order_Detail_Other.quantity END) AS SHPGR5PUN,
SUM(CASE WHEN Time_Slice.slice = 2
AND Status_Group.grouping = 'SHP'
THEN Order_Transaction.quantity END) AS SHPGR5TOD,
SUM(CASE WHEN Time_Slice.slice = 2
AND Status_Group.grouping = 'SHP'
THEN Order_Detail_Other.quantity END) AS SHPGR5TUN,
SUM(CASE WHEN Time_Slice.slice = 3
AND Status_Group.grouping = 'SHP'
THEN Order_Transaction.quantity END) AS SHPGR5TOM,
SUM(CASE WHEN Time_Slice.slice = 3
AND Status_Group.grouping = 'SHP'
THEN Order_Detail_Other.quantity END) AS SHPGR5MUN,
SUM(CASE WHEN Time_Slice.slice = 1 THEN 1 END) AS HLDGR5PST,
SUM(CASE WHEN Time_Slice.slice = 1
THEN Order_Detail_Other.quantity END) AS HLDGR5PUN,
SUM(CASE WHEN Time_Slice.slice = 2 THEN 1 END) AS HLDGR5TOD,
SUM(CASE WHEN Time_Slice.slice = 2
THEN Order_Detail_Other.quantity END) AS HLDGR5TUN,
SUM(CASE WHEN Time_Slice.slice = 3 THEN 1 END) AS HLDGR5TOM,
SUM(CASE WHEN Time_Slice.slice = 3
THEN Order_Detail_Other.quantity END) AS HLDGR5MUN,
FROM ASTCCDTA.OEORH48 Order_History
JOIN Time_Slice
ON Order_History.ohReqD >= Time_Slice.startsOn
AND Order_History.ohReqD < Time_Slice.endsOn
LEFT JOIN (ASTCCDTA.TRNSTAT2 Transaction_Status
JOIN Status_Group
ON Status_Group.status = Transactino_Status.ts.Stat
JOIN Other_Transaction_Status
ON Other_Transaction_Status.tsOrd# = Transaction_Status.tsOrd#) Transaction_Status
ON Transaction_Status.tsCom# = Order_History.ohCom#
AND Transaction_Status.tsOrd# = Order_History.ohOrd#
AND Transaction_Status.tsStat IN('AEP', 'BGE', 'EAS', 'REL', 'SCC', 'ECT', 'STP', 'EPT', 'CAT')
LEFT JOIN Order_Detail_ENG
ON Order_Detail_ENG.odCom# = Order_History.ohCom#
AND Order_Detail_ENG.odOrd# = Order_History.ohOrd#
LEFT JOIN Order_Detail_Other
ON Order_Detail_Other.odCom# = Order_History.ohCom#
AND Order_Detail_Other.odOrd# = Order_History.ohOrd#
LEFT JOIN Order_Transacion
ON Order_Transaction.otCom# = Order_History.ohCom#
AND Order_Transaction.otOrd# = Order_History.ohOrd#
WHERE Order_History.ohOrdT IN('FCH', 'FIT')
AND Order_History.ohHldC = ' '
(注意:我认为PROGR5PST
和PROGR5TOD
将返回相同的值作为拼写错误的事实 - 他们都在考虑相同的行集。看起来你是错过了第二个“今天”值,我假设它应该是PROGR5TOD
。你可能需要调整它。)
要考虑的其他一些事情:这可能会更快。这有点是因为系统必须运行SUM(CASE...)
。可以将这些条件作为 join 的一部分来执行,但最终会有30个表引用。您还可以先执行slice
和grouping
聚合(作为子查询),然后以这种方式进行转移。实验,看看哪些有用,以及这是否有帮助。
答案 1 :(得分:1)
阅读@ Clockwork-Muse的答案。有很多想法,以及关于如何构建SQL的一些非常好的建议,更重要的是关于如何思考关于SQL。
是外部的SQL类型。
外部过程使用除SQL之外的任何语言编写。如果要在SQL中执行所有操作,则需要创建SQL过程。如果您希望将SQL嵌入到RPG程序中(例如),则需要创建外部过程。
我是否制作临时表来保存数据?
不!如果编写存储过程的方法有误,那就是它。加载工作文件,对其进行排序然后打印它的30年概念与SQL运行方式的匹配非常差。
如果不是2,那么如何从Crystal Reports访问此数据。
您创建结果集。这是一个简单的SQL示例,可帮助您入门。网上有很多RPG示例。
create procedure lib/procedure_name
(in ...whatever... char(10))
language sql
dynamic result sets 1
begin
declare c1 cursor for
SELECT * FROM LIBRARY/TABLE ORDER BY 1;
open c1;
return;
end;