简体   繁体   English

关联子查询,Oracle SQL

[英]Correlated Subquery, oracle sql

I am working on my homework and I couldn't figure it out this problem. 我正在做家庭作业,但无法弄清楚这个问题。 Can anyone help me please? 谁能帮我吗? This Oracle Sql 这个Oracle Sql

Display each invoice amount that is higher than the vendor's average invoice amount. 显示高于供应商平均发票金额的每个发票金额。 You must use a correlated subquery. 您必须使用相关的子查询。

I tried but I am not that close. 我试过了,但距离还不很近。 Here my code is: 这是我的代码:

    SELECT vendor_id, invoice_number, invoice_total
FROM ap.invoices
WHERE invoice_total  >  (SELECT AVG(invoice_total) 
                     FROM ap.invoices
                     where invoice_id=invoice_id
                     )
ORDER BY vendor_id; 

Result table should match this image : https://i.stack.imgur.com/w9D6i.png 结果表应与此图片匹配: https : //i.stack.imgur.com/w9D6i.png

Use an alias for the outer query table, and correlate on the vendor_id field: 对外部查询表使用别名,并在vendor_id字段上进行vendor_id

SELECT vendor_id, invoice_number, invoice_total
FROM ap.invoices i
WHERE invoice_total  >  (
    SELECT AVG(invoice_total) 
    FROM ap.invoices v
    where v.vendor_id = i.vendor_id
)
ORDER BY vendor_id;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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