2016-05-29 211 views
0

通過表達創建視圖與子查詢和組爲了獲取特定的數據集,我在選擇查詢這是一組由表達式如下使用子查詢:在甲骨文

SELECT SUM(b.RESERVEDROOMS) AS "Reserved Rooms", 
     (SELECT country 
      FROM location 
     WHERE location_id = b.location_id) AS "Country" 
    FROM bookingsRevenue b, 
     location l 
WHERE b.location_id = l.location_id 
GROUP BY b.location_id 

它工作正常和正確的數據被檢索,但是當我嘗試從它創建一個視圖時,顯示以下錯誤。你能幫我解決這個問題嗎?

[錯誤]執行(21:74):ORA-00979:不是GROUP BY表達

+0

你爲什麼要使用一個標量子查詢 試試這個嗎? – dnoeth

回答

2
似乎

有在源代碼中的混亂的。 GROUP BY子句不正確,您也不需要使用相關的子查詢。如果你還想進行分組的LOCATION_ID然後

SELECT SUM(b.RESERVEDROOMS) AS "Reserved Rooms", l.country AS "Country" 
FROM bookingsRevenue b 
JOIN location l ON (b.location_id = l.location_id) 
GROUP BY l.country; 

SELECT SUM(b.RESERVEDROOMS) AS "Reserved Rooms", b.location_id, l.country AS "Country" 
FROM bookingsRevenue b 
JOIN location l ON (b.location_id = l.location_id) 
GROUP BY b.location_id, l.country; 
+0

或者你可以使用'MAX(l.country)AS country'。 – MT0

+0

是的,我認爲MAX(l.country)的作品,只是有l.country給我一個錯誤。 非常感謝您的建議! – Manujaya