2013-03-28 65 views

回答

1

为you..using只有一个选择万兆+,无笛卡尔自变体加入:)

SQL> select avg(payment_total) 
    2 from invoices; 

AVG(PAYMENT_TOTAL) 
------------------ 
       5.4 

SQL> select invoice_number, invoice_total, payment_total 
    2 from invoices 
    3 model return updated rows 
    4 dimension by (row_number() over (order by 1) rn, 
    5    case when invoice_total > avg(payment_total) over() then 1 else 2 end a) 
    6 measures (invoice_total, invoice_number, payment_total) 
    7 rules (
    8  invoice_number[any, 1] = invoice_number[cv(rn), 1] 
    9 ) 
10 order by 1; 

INVOICE_NUMBER INVOICE_TOTAL PAYMENT_TOTAL 
-------------- ------------- ------------- 
      6    6    1 
      7    7    8 
      8    8    4 
      9    9    7 
      10   10    6 

“返回更新行“..我们只返回我们触摸的行。我们将每行标记为case when invoice_total > avg(payment_total) over() then 1 else 2 end a是否超出平均值。即那些平均超过a的行设为1。那么我们只需1invoice_number[any, 1] = invoice_number[cv(rn), 1](即不要更改任何数据......只是将其更新为自己)痒痒行。

比原始查询:??

SQL> select i.invoice_number, i.invoice_total , i.payment_total 
    2 from invoices i 
    3 where i.invoice_total>(select avg(payment_total) 
    4       from invoices) 
    5 order by 1; 

INVOICE_NUMBER INVOICE_TOTAL PAYMENT_TOTAL 
-------------- ------------- ------------- 
      6    6    1 
      7    7    8 
      8    8    4 
      9    9    7 
      10   10    6 
+0

谢谢!它是我正在寻找的! – 2013-03-29 08:56:55

0
select 
    invoice_number, 
    invoice_total 
from (
    select 
    invoice_number, 
    invoice_total , 
    avg(payment_total) over() avg_payment_total 
    from 
    invoices) 
where 
    invoice_total>avg_payment_total; 
+0

但你仍然可以使用子查询来解决售后服务这个问题( – 2013-03-28 08:59:59

+1

这是一个在线视图 – 2013-03-28 09:00:13

+0

是否有可能解决售后服务这个任务没有任何视图刚刚加入 – 2013-03-28 09:04:04

0

这里有几种方法可以做到这一点。我不保证你的教授会接受他们。

对于我们的第一选择,首先要创建一个功能:

CREATE OR REPLACE FUNCTION AVG_PAYMENT_TOTAL_FUNC RETURN NUMBER IS 
    nAvg_payment_total NUMBER; 
BEGIN 
    SELECT AVG(PAYMENT_TOTAL) 
    INTO nAvg_payment_total 
    FROM INVOICES; 

    RETURN nAvg_payment_total; 
END AVG_PAYMENT_TOTAL_FUNC; 

,那么你使用该功能在查询:

select i.invoice_number, i.invoice_total 
    from invoices i 
    where i.invoice_total > AVG_PAYMENT_TOTAL_FUNC; 

第二种方案是创建一个视图:

CREATE OR REPLACE VIEW AVG_PAYMENT_TOTAL_VIEW AS 
    SELECT AVG(PAYMENT_TOTAL) AS AVG_PAYMENT_TOTAL 
    FROM INVOICES; 

然后您的查询变成

SELECT i.INVOICE_NUMBER, 
     i.INVOICE_TOTAL, 
     t.AVG_PAYMENT_TOTAL 
    FROM INVOICES i 
    CROSS JOIN AVG_PAYMENT_TOTAL_VIEW t; 

缺少这样的东西我看不到一种方法来完成你已经分配的东西。更重要的是,我无法想象任何理由为什么有人会在乎查询中是否有一个或两个SELECT关键字。要求开发人员想出一些古怪/怪异/书呆子的方式来完成上述所有内容,只需一个SELECT关键字就可以完成上述操作,这是浪费时间。有完全合理的方法可以快速而合理地完成这项工作;要求某人解决问题否则既不生产也不高效,因此是IMO的无意义。

分享和享受。

0
with average as (select avg(payment_total) avgtot 
       from invoices) 
select i.invoice_number, i.invoice_total 
    from invoices i 
    , average a 
where i.invoice_total>a.avgtot; 
+0

一个WITh子句仍然是子查询 – APC 2013-03-29 00:57:51

2

只有一个SELECT :-)

select i1.invoice_number, i1.invoice_total 
from invoices i1, invoices i2 
group by i1.invoice_number, i1.invoice_total 
having i1.invoice_total > avg(i2.payment_total)