简体   繁体   中英

missing expression in Select SQL

I'm new to SQL and sub queries, When I run the sub query by itself I get the correct data output. But when I run the full query I get the error message

SELECT * Error at line 3: ORA-00936: missing expression

Here's my code:

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)

SOLUTION:

   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)
);

You needs two parentheses more :)

  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

The Select into MAX have to be in paranthesis.

The logic is not clear. This give you the total pay per activity:

  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

If you want to see, which activity has the biggest payment, then order by desc :

  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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM