我在执行oracle sql时遇到问题,第一个代码示例在执行时正在工作,而第二个代码示例出现错误(“无效数字”),两者都应产生相同的结果。请问第二个SQL有什么问题吗?请指导我。
第一个SQL
select a5.LOTID,COUNT(a5.stephandle) AS STEPHANDLE from LOTFLOW_D a5,
(select distinct(LOTID),MAX(STEPHANDLE) AS STEPHANDLE from Lotmove_R
where QTAP_GROUP In ('8685','8686','8687','8688','8689','8533','8532','2534','2533','8684','8690')
and datadate>sysdate-3
and priority<=5
and SUBPLANID NOT LIKE '%RWK%' AND STEPHANDLE NOT LIKE '%8888%' AND STEPHANDLE LIKE '%.%.%'
group by lotid) a6
where a5.lotid=a6.lotid
and a5.STEPHANDLE > a6.STEPHANDLE AND A5.EQPTYPE NOT IN ('CUDDFINP','CUBDFINP','BDFINP','DDFINP','CUDFSEM','DFSEM','SORT','CUSORT','CUNPURGE','NPURGE','EDFINP','CUEDFINP')
group by a5.lotid
order by a5.lotid
第一个sql的输出(结果正是我想要的)
LOTID STEPHANDLE
_____ ___________
S8CN9 214
S7JY7 30
... .....
第二条SQL
Select a5.lotid,a5.STEPHANDLE from
(select lotid,COUNT(stephandle) as STEPHANDLE, EQPTYPE from LOTFLOW_D Group by lotid, EQPTYPE ) a5
left join
(select distinct(LOTID), MAX(STEPHANDLE) AS STEPHANDLE from Lotmove_R
where QTAP_GROUP In ('8685','8686','8687','8688','8689','8533','8532','2534','2533','8684','8690')
and datadate>sysdate-3
and priority<=5
and SUBPLANID NOT LIKE '%RWK%' AND STEPHANDLE NOT LIKE '%8888%' AND STEPHANDLE LIKE '%.%.%'
group by lotid) a6 on a5.lotid=a6.lotid
where a5.STEPHANDLE > a6.stephandle AND a5.EQPTYPE NOT IN ('CUDDFINP','CUBDFINP','BDFINP','DDFINP','CUDFSEM','DFSEM','SORT','CUSORT','CUNPURGE','NPURGE','EDFINP','CUEDFINP')
order by a5.lotid
第二个出现错误(max(stephandle)的数字无效)作为 stephandle是类似于1470.0.0400的东西,我知道这不是一个数字,但是它使用第一个sql可以很好地工作,并且达到了我的预期结果(以数字显示),我尝试了count(stephandle),但是结果是错误的,我需要使用左联接在我的主sql上运行的第二种sql格式
答案 0 :(得分:3)
在第一个查询中,您比较步进手柄
a5.STEPHANDLE > a6.STEPHANDLE
其中字符串按字母顺序进行比较,例如
'1.2.3' < '2.3.4' '1.2.3' > '10.0.0' '1.2.3' > '1.2.04' '1.2.3' > '1.2.20'
(正如您所说的那样,您的结果符合预期,您可能使用了固定长度的数字。但是像where stephandle like '__.__.__'
这样的东西比where stephandle like '%.%.%'
更合适。无论如何,我希望这个限制在两个表上,而不仅仅是一个。)
此比较导致每个lotid
的匹配项数,您用COUNT(a5.stephandle) AS STEPHANDLE
计算。当然,您也可以使用COUNT(*)
,它会更易读,因为您正在对行进行计数,并且不想检测空的空档以将它们从计数中排除(这就是{{1} })。此外,别名COUNT(a5.stephandle)
是不合适的,因为它不是您要在结果中显示的步进句柄,而是步进句柄的数量,因此STEPHANDLE
或NUM_OF_STEPHANDLES
或STEPHANDLE_COUNT
将是好得多的名字。
在第二个查询中,您对第一个查询中的每个STEPHANDLES
和lotid
进行记录计数(同样是eqptype
,最好是COUNT(stephandle)
)。这会给您数字,例如300、1000等。您再次将此结果称为COUNT(*)
,这使您可以进行比较
STEPHANDLE
您可能不会使用更好的名称,因为
a5.STEPHANDLE > a6.stephandle
显然没有任何意义。
我只能猜到你真正想要的是什么。看来您想计算表a5.STEPHANDLE_COUNT > a6.stephandle
中每个stephandles
有多少lotid
,而表lotflow_d
中没有更高的副本。像这样:
lotmove_r
答案 1 :(得分:0)
在您的a5查询中尝试将TO_CHAR用作 TO_CHAR(COUNT(stephandle))。以后应该将您的值作为字符串进行比较,这很好(在您的第一个查询中也是如此)。
我应该指出,在where子句中引用任何a6列会将您的左联接变为内部联接。 如果您确实希望查询充当左联接,而不是
ON a5.lotid = a6.lotid
WHERE a5.STEPHANDLE > a6.stephandle
使用:
ON (a5.lotid = a6.lotid AND a5.STEPHANDLE > a6.stephandle)
我还详细提到了该问题,作为对这个问题的答案: Why can't you use OR or IN with a OUTER JOIN operation?
完整代码:
SELECT a5.lotid, a5.STEPHANDLE
FROM ( SELECT lotid, TO_CHAR (COUNT (stephandle)) AS STEPHANDLE, EQPTYPE
FROM LOTFLOW_D
GROUP BY lotid, EQPTYPE) a5
LEFT JOIN ( SELECT DISTINCT (LOTID), MAX (STEPHANDLE) AS STEPHANDLE
FROM Lotmove_R
WHERE QTAP_GROUP IN ('8685',
'8686',
'8687',
'8688',
'8689',
'8533',
'8532',
'2534',
'2533',
'8684',
'8690')
AND datadate > SYSDATE - 3
AND priority <= 5
AND SUBPLANID NOT LIKE '%RWK%'
AND STEPHANDLE NOT LIKE '%8888%'
AND STEPHANDLE LIKE '%.%.%'
GROUP BY lotid) a6
ON (a5.lotid = a6.lotid AND a5.STEPHANDLE > a6.stephandle)
WHERE a5.EQPTYPE NOT IN ('CUDDFINP',
'CUBDFINP',
'BDFINP',
'DDFINP',
'CUDFSEM',
'DFSEM',
'SORT',
'CUSORT',
'CUNPURGE',
'NPURGE',
'EDFINP',
'CUEDFINP')
ORDER BY a5.lotid
我希望这会有所帮助:)