子查询的问题
我有以下查询:
SELECT SP.PACKAGEID,SP.ORDERID,PTS.CREATIONDATE AS SHIPPEDDATE
FROM PACKAGES SP
INNER JOIN
(
SELECT * FROM
(
SELECT *FROM PACKAGE_STATISTICS A
WHERE((A.PACKAGEID=SP.PACKAGEID)
AND(A.PACKAGESTATUS=2)
AND(TO_DATE(to_char(A.CREATIONDATE,'mm/dd/yyyy'),'mm/dd/yyyy')
BETWEEN TO_DATE('2/19/2011','mm/dd/yyyy') AND (TO_DATE('3/21/2011','mm/dd/yyyy'))))
ORDER BY A.CREATIONDATE
) WHERE ROWNUM<2
) PTS ON PTS.PACKAGEID=SP.PACKAGEID
WHERE SP.ISSUEID IN(402783)
此查询根据以下条件从 PACKAGES 表中选择包裹详细信息:
- packgae 的状态必须为“已发货”。(通过将 PACKAGE_STATISTICS 的 PACKAGESTATUS 列设置为 2 来指示)
- 如果有多个“已发货”条目一个包,选择最新的(基于 CREATIONDATE 列值)。
当我运行上面的查询时,它抛出了 ORA-00904 错误,因为级别 2 的子查询引用了表名称。因此,我修改了查询,如下所示:
SELECT SP.PACKAGEID,SP.ORDERID, (SELECT CREATIONDATE FROM
(
SELECT *FROM PACKAGE_STATISTICSA
WHERE
((A.PACKAGEID=SP.PACKAGEID)
AND(A.PACKAGESTATUS=2)
AND(TO_DATE(to_char(A.CREATIONDATE,'mm/dd/yyyy'),'mm/dd/yyyy')
BETWEEN TO_DATE('2/19/2011','mm/dd/yyyy') AND (TO_DATE('3/21/2011','mm/dd/yyyy'))))
ORDER BY A.CREATIONDATE
) WHERE ROWNUM<2) AS SHIPPEDDATE
FROM PACKAGESSP
WHERE
(SHIPPEDDATE BETWEEN TO_DATE('2/19/2011','mm/dd/yyyy')
AND (TO_DATE('3/21/2011','mm/dd/yyyy')))
AND SP.ISSUEID IN(402783)
它再次抛出 ORA-00904 SHIPPEDDATE 无效标识符。 请让我知道我该怎么做? 谢谢大家, 普拉迪普
I have the following query:
SELECT SP.PACKAGEID,SP.ORDERID,PTS.CREATIONDATE AS SHIPPEDDATE
FROM PACKAGES SP
INNER JOIN
(
SELECT * FROM
(
SELECT *FROM PACKAGE_STATISTICS A
WHERE((A.PACKAGEID=SP.PACKAGEID)
AND(A.PACKAGESTATUS=2)
AND(TO_DATE(to_char(A.CREATIONDATE,'mm/dd/yyyy'),'mm/dd/yyyy')
BETWEEN TO_DATE('2/19/2011','mm/dd/yyyy') AND (TO_DATE('3/21/2011','mm/dd/yyyy'))))
ORDER BY A.CREATIONDATE
) WHERE ROWNUM<2
) PTS ON PTS.PACKAGEID=SP.PACKAGEID
WHERE SP.ISSUEID IN(402783)
This query selects package details from PACKAGES table based on the following conditions:
- The status of the packgae must be "Shipped".(Indicated by setting PACKAGESTATUS column of PACKAGE_STATISTICSto 2)
- If there are multiple "Shipped" entries for a package, select the latest(based on CREATIONDATE column value).
When I run the above query it throwed ORA-00904 error as the subquery of level 2 refres the table name. So I have modified the query as given below:
SELECT SP.PACKAGEID,SP.ORDERID, (SELECT CREATIONDATE FROM
(
SELECT *FROM PACKAGE_STATISTICSA
WHERE
((A.PACKAGEID=SP.PACKAGEID)
AND(A.PACKAGESTATUS=2)
AND(TO_DATE(to_char(A.CREATIONDATE,'mm/dd/yyyy'),'mm/dd/yyyy')
BETWEEN TO_DATE('2/19/2011','mm/dd/yyyy') AND (TO_DATE('3/21/2011','mm/dd/yyyy'))))
ORDER BY A.CREATIONDATE
) WHERE ROWNUM<2) AS SHIPPEDDATE
FROM PACKAGESSP
WHERE
(SHIPPEDDATE BETWEEN TO_DATE('2/19/2011','mm/dd/yyyy')
AND (TO_DATE('3/21/2011','mm/dd/yyyy')))
AND SP.ISSUEID IN(402783)
It again throwed ORA-00904 SHIPPEDDATE Invalid Identifier.
Please let me know how can I do this?
Thanking you all,
Pradeep
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您只需要 PACKAGE_STATISTICS 中的最新日期,为什么不直接使用 MAX(CREATION_DATE) 呢?我现在无法测试它,但您可能需要这样的查询:
如果您想从具有最新创建日期的 package_statistics 行中获得更多详细信息,请考虑使用分析函数:
If you need only the latest date from PACKAGE_STATISTICS, why don't you simply use MAX(CREATION_DATE)? I can't test it right now, but you probably need a query like this:
If you want more details from row of package_statistics with latest creationdate, consider using analytic functions: