2016-08-29 60 views
0

在Talend與法定的繼承權的行數我有3個表如何指望甲骨文

supplier(id_supp, name, adress, ...) 

Customer(id_cust, name, adress, ...) 

Order(id_order, ref_cust, ref_supp, date_order...) 

我想要通過Supplier計算的訂單數,LAST_WEEK工作,與Talend

last_two_weeks
select 
supp.name, 
(
    select 
     count(*) 
    from 
     order 
    where 
     date_order between sysdate-7 and sysdate 
     nd ref_supp=id_supp 
) as week_1, 
(
    select 
     count(*) 
    from 
     order 
    where 
     date_order between sysdate-14 and sysdate-7 
     nd ref_supp=id_supp 
) as week_2 
from supplier supp 

爲我這樣做是什麼resaon,是我的查詢走上了多少時間

+1

因此,您的查詢工作,你只是想知道爲什麼它效率低下? –

+0

我不知道如何使它與Talend:/ –

回答

0

你需要之間的連接0和order以獲得供應商名稱。我顯示一個內部聯接,但是如果您需要所有供應商(即使在order表中沒有訂單的供應商),也可以將其更改爲左外部聯接。

除此之外,你只需要閱讀一次order表,並獲得所有你需要的信息。您的查詢不止一次傳遞(請參閱您的查詢的EXPLAIN PLAN),這可能是因爲它需要太長時間。

注意:sysdate有一個時間分量組件(也許date_order值也是);您編寫查詢的方式可能會或可能不會完全按照您希望的方式進行。您可能必須圍繞sysdatetrunc()

select s.name, 
     count(case when o.date_order between sysdate - 7 and sysdate  then 1 end) 
                       as week_1, 
     count(case when o.date_order between sysdate - 14 and sysdate - 7 then 1 end) 
                       as week_2 
from supplier s inner join order o 
     on s.id_supp = o.ref_supp 
; 
+0

thx查詢 –