mysqlsqloracle-databaseoracle-xeora-00932

Complicated Calculation within SQL Query


I posted a similar question yesterday with the same code, I have rewritten and it should be perfect to a new specification but for some reason it is not working.

I am using Oracle 10g Express.

The code below is working out the top 10% of earning solicitors.

CREATE VIEW rich_solicitors AS
select notes.time_spent*rate.rate_amnt+coalesce(special_rate.s_rate_amnt,0)
AS solicitor_made, notes.solicitor_id
FROM notes, rate, solicitor_rate, solicitor, case, contract, special_rate
WHERE notes.solicitor_id = solicitor.solicitor_id
AND solicitor.solicitor_id = solicitor_rate.solicitor_id
AND solicitor_rate.rate_id = rate.rate_id
AND notes.case_id = case.case_id
AND case.contract_id = contract.contract_id
AND contract.contract_id = special_rate.contract_id (+)
ORDER BY -solicitor_made;


SELECT * FROM rich_solicitors
WHERE ROWNUM <= (SELECT COUNT(*)/10 FROM rich_solicitors);

I need to work out the top 10% of last year, I thought it would be as easy as adding the start_date and expiry_date to the SELECT and adding the following WHERE function:

AND contract.start_date >= 01-01-10
AND contract.expiry_date <= 01-01-11

I have inserting the date corectly using TO_DATE. My lecturer spent a good hour with me working out why this wouldnt work but to no avail.

It kept on returning the error - ORA-00932: inconsistent datatypes: expected CHAR got NUMBER

I also tried defining the date format and that still returned the same error

CREATE VIEW rich_solicitors1 AS
SELECT  notes.time_spent*rate.rate_amnt+coalesce(special_rate.s_rate_amnt,0) AS solicitor_made, notes.solicitor_id, TO_CHAR(contract.start_date, 'DD-MM-YY'), TO_CHAR(contract.expiry_date, 'DD-MM-YY')
FROM notes, rate, solicitor_rate, solicitor, case, contract, special_rate
WHERE notes.solicitor_id = solicitor.solicitor_id
AND solicitor.solicitor_id = solicitor_rate.solicitor_id
AND solicitor_rate.rate_id = rate.rate_id
AND notes.case_id = case.case_id
AND case.contract_id = contract.contract_id
AND contract.contract_id = special_rate.contract_id (+)
AND contract.start_date >= 01-01-10
AND contract.expiry_date <= 01-01-11
ORDER BY -solicitor_made;

any ideas?

Many thanks, any criticism is much appreciated, I am a student and at my very first steps with this, before about 9 days ago I had no SQL knowledge at all.


Solution

  • Can you try i have replaced your coalesce by NVL function

    SELECT  notes.time_spent*rate.rate_amnt+NVL(special_rate.s_rate_amnt,0) 
    AS solicitor_made, notes.solicitor_id, TO_CHAR(contract.start_date, 'DD-MM-YY'), 
    TO_CHAR(contract.expiry_date, 'DD-MM-YY') 
    FROM notes, rate, solicitor_rate, solicitor, case, contract, special_rate 
    WHERE notes.solicitor_id = solicitor.solicitor_id 
    AND solicitor.solicitor_id = solicitor_rate.solicitor_id 
    AND solicitor_rate.rate_id = rate.rate_id AND notes.case_id = case.case_id 
    AND case.contract_id = contract.contract_id 
    AND contract.contract_id = special_rate.contract_id (+) 
    AND contract.start_date >= to_date('01-01-10','MM-DD-YY')
    AND contract.expiry_date <= to_date('01-01-11' ,'MM-DD-YY')
    ORDER BY solicitor_made