子查询的问题

发布于 2024-10-24 06:38:56 字数 1464 浏览 1 评论 0原文

我有以下查询:

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 表中选择包裹详细信息:

  1. packgae 的状态必须为“已发货”。(通过将 PACKAGE_STATISTICS 的 PACKAGESTATUS 列设置为 2 来指示)
  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:

  1. The status of the packgae must be "Shipped".(Indicated by setting PACKAGESTATUS column of PACKAGE_STATISTICSto 2)
  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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

药祭#氼 2024-10-31 06:38:56

如果您只需要 PACKAGE_STATISTICS 中的最新日期,为什么不直接使用 MAX(CREATION_DATE) 呢?我现在无法测试它,但您可能需要这样的查询:

SELECT SP.PACKAGEID
      ,SP.ORDERID
      ,MAX(A.CREATIONDATE) AS SHIPPEDDATE
  FROM PACKAGES SP
 INNER JOIN PACKAGE_STATISTICS A ON A.PACKAGEID = SP.PACKAGEID
 WHERE SP.ISSUEID IN (402783)
   AND 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')
 GROUP BY SP.PACKAGEID
         ,SP.ORDERID

如果您想从具有最新创建日期的 package_statistics 行中获得更多详细信息,请考虑使用分析函数:

SELECT SP.PACKAGEID
      ,SP.ORDERID
      ,PTS.*
  FROM PACKAGES SP
 INNER JOIN (SELECT ROW_NUMBER() OVER(PARTITION BY PACKAGEID ORDER BY CREATIONDATE DESC) rn
                   ,A.*
               FROM PACKAGE_STATISTICS A
              WHERE 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')) PTS ON PTS.PACKAGEID =
                                                               SP.PACKAGEID
                                                           AND PTS.RN = 1;

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:

SELECT SP.PACKAGEID
      ,SP.ORDERID
      ,MAX(A.CREATIONDATE) AS SHIPPEDDATE
  FROM PACKAGES SP
 INNER JOIN PACKAGE_STATISTICS A ON A.PACKAGEID = SP.PACKAGEID
 WHERE SP.ISSUEID IN (402783)
   AND 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')
 GROUP BY SP.PACKAGEID
         ,SP.ORDERID

If you want more details from row of package_statistics with latest creationdate, consider using analytic functions:

SELECT SP.PACKAGEID
      ,SP.ORDERID
      ,PTS.*
  FROM PACKAGES SP
 INNER JOIN (SELECT ROW_NUMBER() OVER(PARTITION BY PACKAGEID ORDER BY CREATIONDATE DESC) rn
                   ,A.*
               FROM PACKAGE_STATISTICS A
              WHERE 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')) PTS ON PTS.PACKAGEID =
                                                               SP.PACKAGEID
                                                           AND PTS.RN = 1;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文