【问题标题】:how to count number of lines with jointure in Talend on Oracle如何在 Oracle 上的 Talend 中计算带关节的行数
【发布时间】:2016-08-29 17:52:57
【问题描述】:

我有 3 张桌子

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

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

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

我想做一份按Supplier 计算订单数量的工作,对于last_week,last_two_weeks 和Talend

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

我这样做的原因是我的查询花费了很多时间

【问题讨论】:

  • 所以您的查询有效,您只是想知道为什么它效率低下?
  • 我不知道如何用 Talend 制作它:/

标签: mysql oracle count talend jointable


【解决方案1】:

您需要在 supplierorder 之间进行连接才能获取供应商名称。我展示了一个内连接,但如果您需要所有供应商(即使是那些在order 表中没有订单的供应商),您可以将其更改为左外连接。

除此之外,您应该只需要阅读order 表一次并获取您需要的所有信息。您的查询不止一次通过(请阅读 EXPLAIN PLAN 以了解您的查询),这可能是它耗时过长的原因。

注意:sysdate 有一个时间组件(也许date_order 值也有);您编写查询的方式可能会或可能不会完全按照您的要求执行。您可能需要用trunc() 包围sysdate

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
;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多