Jed Jed - 7 months ago 24
SQL Question

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

Answer

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
Comments