2016-04-28 123 views
0

我是新來的SQL和子查詢,當我自己運行子查詢時,我得到正確的數據輸出。但是,當我運行完整的查詢我收到錯誤消息在選擇SQL中缺少表達式

SELECT *錯誤在第3行:ORA-00936:缺少表達

這裏是我的代碼:

SELECT 
MAX(
    SELECT 
    SUM(
     ALLOCATION.HourlyRate 
     * 
     ACTION.HrsWorked 
     ) 
    FROM ALLOCATION 
    INNER JOIN ACTION 
    ON ((ACTION.ActId = ALLOCATION.ActId) AND (ACTION.EmpId = ALLOCATION.EmpId)) 
    GROUP BY (ALLOCATION.ActId) 
) 
FROM ALLOCATION 
GROUP BY (ALLOCATION.ActId) 

解決方案:

SELECT MAX(sum_total_pay) 
FROM 
(
    SELECT SUM(ALLOCATION.HourlyRate * ACTION.HrsWorked) AS sum_total_pay 
    FROM ALLOCATION 
    INNER JOIN ACTION 
    ON ((ACTION.ActId = ALLOCATION.ActId) AND (ACTION.EmpId = ALLOCATION.EmpId)) 
    GROUP BY (ALLOCATION.ActId) 
); 
+0

嘗試刪除所有那些沒用的括號 - 特別是那些圍繞「group by」子句中的列。但是'select'列表中的標量子查詢並不合理。你究竟想達到什麼目的? –

+0

我會做一個基於派生表的查詢。你不能在SUM()中做一個SELECT。 – jarlh

+0

我試圖找到最大的「總工資」,它是給定活動ID的hourlyRate列和hrsWorked列的所有產品的總和。即活動1'python coding'可能有總支付(3 * 4 + 5 * 10.5 + 12 * 16),而活動2'testin'可能有總支付(4 * 10 + 5 * 8),我需要找到這兩個人的最高工資總額。 – Jed

回答

0

你需要兩個括號更多:)

SELECT MAX (( SELECT SUM (ALLOCATION.HourlyRate * ACTION.HrsWorked) 
        FROM ALLOCATION INNER JOIN ACTION ON (ACTION.ActId = ALLOCATION.ActId) AND (ACTION.EmpId = ALLOCATION.EmpId) 
       GROUP BY (ALLOCATION.ActId))) 
    FROM ALLOCATION 
GROUP BY ALLOCATION.ActId 

Select into MAX必須符合要求。

邏輯不清楚。這給你每次活動的總工資:

SELECT ALLOCATION.ActId, SUM (ALLOCATION.HourlyRate * ACTION.HrsWorked) total_pay 
    FROM ALLOCATION b INNER JOIN ACTION ON ACTION.ActId = ALLOCATION.ActId AND ACTION.EmpId = ALLOCATION.EmpId 
GROUP BY ALLOCATION.ActId 

如果你想看到的,其活動具有最大的支付,然後才能通過遞減:

SELECT * 
    FROM ( SELECT ALLOCATION.ActId, SUM (ALLOCATION.HourlyRate * ACTION.HrsWorked) total_pay 
       FROM ALLOCATION b INNER JOIN ACTION ON ACTION.ActId = ALLOCATION.ActId AND ACTION.EmpId = ALLOCATION.EmpId 
      GROUP BY ALLOCATION.ActId) 
ORDER BY total_pay DESC