1

我昨天用相同的代码发布了一个类似的问题,我已经重写了它应该是完美的新规范,但由于某种原因它不起作用。

我正在使用 Oracle 10g Express。

下面的代码正在计算收入最高的 10% 的律师。

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

我需要计算出去年的前 10%,我认为这就像将 start_date 和 expiry_date 添加到 SELECT 并添加以下 WHERE 函数一样简单:

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

我已经使用 TO_DATE 正确地插入了日期。我的讲师花了一个小时和我一起研究为什么这不起作用但无济于事。

它不断返回错误 - ORA-00932:不一致的数据类型:预期的 CHAR 得到了 NUMBER

我也尝试定义日期格式,但仍然返回相同的错误

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;

有任何想法吗?

非常感谢,非常感谢任何批评,我是一名学生,在我的第一步,大约 9 天前,我根本没有 SQL 知识。

4

2 回答 2

3

你能试试我用 NVL 函数替换了你的合并吗

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
于 2011-03-03T21:14:22.003 回答
2

Phil,根据我能想到的任何“top”定义,您使用 ROWNUM 的查询不会获得前 10%。

一个例子是:

SELECT * FROM (
  SELECT solicitor_made, solicitor_id
        ,NTILE(10) OVER (ORDER BY solicitor_made DESC) decile
  FROM rich_solicitors
) WHERE decile = 1;

编辑

刚刚注意到您的视图定义中有一个 ORDER BY 。在这种情况下,您的 ROWNUM 方法将起作用。通常我建议不要将 ORDER BY 放在视图定义中。

于 2011-03-04T01:48:17.313 回答