子查询脑冻结

时间:2016-05-27 12:18:07

标签: sql subquery

自从我完成子查询以来已经有一段时间了,对于我的生活,我无法看到我的查询有什么问题。

执行时得到的错误信息是:

ORA-00904: "SUB"."PRO_REFNO": invalid identifier

这是我的查询。我显然做错了什么,但我不能看到它。

SELECT
 prop.PRO_ADR_1_LINE,
 ele.POE_START_DATE,
 ele.POE_ELEMENT_DESCR,
 ele.POE_VALUE,
 ele.POE_ATTRIBUTE,
 ele.POE_FURTHER_ATTRIBUTE,
 ele.POE_FURTHER_ATTRIBUTE_DESCR,
 prop.PRO_SCHEME,
 prop.PRO_SCHEME_DESCR,
 GEO.GEO_BUS_UNIT,
 GEO.GEO_REGION,
 GEO.GEO_REGION_DESCR,
 prop.PRO_NEIGHBOURHOOD_DESCR
 --sub.pro_refno

FROM property prop 
--inner join



left join GEO on prop.PRO_GEO_PATCH=GEO.GEO_PATCH
left join PROPERTY_OTHER_ELEMENT ele on ele.POE_PRO_REFNO =prop.PRO_REFNO
inner join(
SELECT
property.PRO_SCHEME,
count(distinct property.PRO_REFNO)
FROM
PROPERTY
WHERE
property.pro_type  =  'P'
GROUP BY
property.PRO_SCHEME
)sub
on sub.pro_refno = prop.PRO_REFNO
where
  ele.POE_START_DATE  BETWEEN '01-APR-2016'  AND  sysdate
  AND
  ele.POE_ELEMENT  LIKE  'EST%'
  AND
  ele.POE_ELEMENT_DESCR  <>  'Estate Walkabout - Would you live in this neighbourhood ?'
  AND
  ele.POE_VALUE  IN  ( '1','2','3','4','5','6','7','8','9','10'  )

外部查询和子查询分别运行正常。就像我说它已经有一段时间了,所以我猜它有些愚蠢我做过/没做过。

由于

亚当

2 个答案:

答案 0 :(得分:0)

您没有为聚合列命名:

inner join(
SELECT
property.PRO_SCHEME,
count(distinct property.PRO_REFNO) -- No name!!!
FROM
PROPERTY
WHERE
property.pro_type  =  'P'
GROUP BY
property.PRO_SCHEME
)sub
on sub.pro_refno = prop.PRO_REFNO

将其更改为:

inner join(
SELECT
property.PRO_SCHEME,
count(distinct property.PRO_REFNO) As PRO_REFNO
FROM
PROPERTY
WHERE
property.pro_type  =  'P'
GROUP BY
property.PRO_SCHEME
)sub
on sub.pro_refno = prop.PRO_REFNO

答案 1 :(得分:0)

您的子查询未选择PRO_REFNO,因此外部查询在JOIN谓词中无法与其匹配。尝试使用子查询:

SELECT
    property.PRO_SCHEME,
    property.PRO_REFNO,
    count(distinct property.PRO_REFNO)
FROM PROPERTY
WHERE property.pro_type = 'P'
GROUP BY
property.PRO_SCHEME,
property.PRO_REFNO

此外,您的COUNT(DISTINCT ...)没有给出别名,如果它将在外部选择中使用,您将需要该别名。