SQL查询中的复杂计算

发布于 2024-10-20 11:42:53 字数 1897 浏览 4 评论 0原文

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

我正在使用 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 得到数字

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

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 知识。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自此以后,行同陌路 2024-10-27 11:42:53

你能尝试一下吗?我已经用 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

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
三生殊途 2024-10-27 11:42:53

Phil,您使用 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 放在视图定义中。

Phil, your query using ROWNUM will not get the top 10% by any definition of "top" that I can think of.

An example that will is:

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

EDIT

Just noticed you have an ORDER BY in your view definition. In which case your ROWNUM method will work. Normally I recommend against putting ORDER BY in a view definition.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文