如何在沒有子查詢的情況下重寫此查詢?重寫avg而不帶子查詢
select i.invoice_number, i.invoice_total
from invoices i
where i.invoice_total>(select avg(payment_total)
from invoices);
如何在沒有子查詢的情況下重寫此查詢?重寫avg而不帶子查詢
select i.invoice_number, i.invoice_total
from invoices i
where i.invoice_total>(select avg(payment_total)
from invoices);
爲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
。那麼我們只需1
invoice_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
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;
但你仍然可以使用子查詢來解決售後服務這個問題( – 2013-03-28 08:59:59
這是一個在線視圖 – 2013-03-28 09:00:13
是否有可能解決售後服務這個任務沒有任何視圖剛剛加入 – 2013-03-28 09:04:04
這裏有幾種方法可以做到這一點。我不保證你的教授會接受他們。
對於我們的第一選擇,首先要創建一個功能:
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的無意義。
分享和享受。
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;
一個WITh子句仍然是子查詢 – APC 2013-03-29 00:57:51
只有一個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)
謝謝!它是我正在尋找的! – 2013-03-29 08:56:55