2013-04-28 79 views
2

我想通過計數表中屬性編號出現的次數來計算YR_Inspection表中每個屬性的檢查次數,然後我需要顯示下一個到該物業所在城市的城鎮以及該財產所屬的分支在同一查詢中。這裏是鏈接到我的ERD,試圖給這個問題的一些背景,多表連接查詢,在Oracle SQL中計數

enter image description here

http://www.gliffy.com/pubdoc/4239520/L.png

這是到目前爲止的代碼,它只要我添加YR_Branch.CITY工作這麼遠,但我得到的,

ORA-00979:不是GROUP BY表達式

SELECT YR_Property.PROPERTYNUM, COUNT(YR_Inspection.PROPERTYNUM) AS Number_of_inspections 
FROM YR_Property 
INNER JOIN YR_Inspection 
    ON YR_Property.PROPERTYNUM = YR_Inspection.PROPERTYNUM 
JOIN YR_Branch 
    ON YR_Property.BRANCHNUM = YR_Branch.BRANCHNUM 
GROUP BY YR_Property.PROPERTYNUM 

回答

2

要添加的分支數目和城市的BR爲每個查詢添加max值 - 如下所示:

SELECT YR_Property.PROPERTYNUM, 
     COUNT(YR_Inspection.PROPERTYNUM) AS Number_of_inspections, 
     MAX(YR_Branch.BRANCHNUM) AS Branch_Number, 
     MAX(YR_Branch.CITY) AS Branch_City 
FROM YR_Property 
INNER JOIN YR_Inspection 
    ON YR_Property.PROPERTYNUM = YR_Inspection.PROPERTYNUM 
JOIN YR_Branch 
    ON YR_Property.BRANCHNUM = YR_Branch.BRANCHNUM 
GROUP BY YR_Property.PROPERTYNUM 
+0

第一次工作,非常感謝。 – 2013-04-28 14:26:58

+0

@Insert_into_knowledge:不客氣。 – 2013-04-28 14:29:42