如何联接两个表?它还包括月份

时间:2020-08-03 01:09:15

标签: oracle oracle11g

当我连接前三个表(ACTIVATIONS,customer,agent_dtl)时,我试图在这里连接4个表,我得到4000行计数,但是如果我尝试连接第四个表(postpaid_summary),我得到的不只是10万行。为什么呢?

我认为当月TO_CHAR(TRUNC(a.packag_start_date,'MONTH'),'MON-YYYY')出现问题,如何以分钟(TIME_DAY_KEY)获得4000行?

SELECT
    a.act_actdevice,
    a.act_phone_no,
    a.bi_account_id,
    a.packag_start_date,
    TO_CHAR(TRUNC(a.packag_start_date, 'MONTH'), 'MON-YYYY') AS PACKAG_START_DATE_MONTHYEAR,
    a.retailer_name,
    a.retailer_type,
    a.dms_id as "DSR/BPR_ID",
    a.dsr_name as "DSR/BPR_NAME",
    a.agent_type,
    a.distributor_id,
    a.distributor_name,
    a.SALES_DISTRICT,
    a.profileid,
    s.district,
    s.province,
    c.identification_number,
    c.account_type,
    c.account_status,
    c.activation_date,
    c.permanent_disconnection_date,
    c.temporary_disconnection_date,
    c.status_change_date,
    c.credit_limit,
    c.average_monthly_bill_amount,
    c.primary_packag_start__date,
    c.package_code,
    c.sales_channel,
    c.site_id,
    c.district_name,
    c.usage_arpu,
    c.bill_to_contact_name,
    min(p.TIME_DAY_KEY) as first_consumption_date 
FROM
    ACTIVATIONS a 
    left JOIN customer c on TO_CHAR(a.act_phone_no) = c.msisdn_voice 
    left JOIN agent_dtl s ON a.dms_id = s.agent_id 
    JOIN postpaid_summary p on a.act_phone_no = p.MSISDN 
where
    a.packag_start_date BETWEEN TO_DATE('2020-01-01 00:00:00', 'YYYY-MM-DD HH24:MI:SS') and TO_DATE('2020-05-31 23:59:59', 'YYYY-MM-DD HH24:MI:SS') 
group by
    a.act_actdevice,
    a.act_phone_no,
    a.bi_account_id,
    a.packag_start_date,
    TO_CHAR(TRUNC(a.packag_start_date, 'MONTH'), 'MON-YYYY'),
    a.retailer_name,
    a.retailer_type,
    a.dms_id,
    a.dsr_name,
    a.agent_type,
    a.distributor_id,
    a.distributor_name,
    a.SALES_DISTRICT,
    a.profileid,
    s.district,
    s.province,
    c.identification_number,
    c.account_type,
    c.account_status,
    c.activation_date,
    c.permanent_disconnection_date,
    c.temporary_disconnection_date,
    c.status_change_date,
    c.credit_limit,
    c.average_monthly_bill_amount,
    c.primary_packag_start__date,
    c.package_code,
    c.sales_channel,
    c.site_id,
    c.district_name,
    c.usage_arpu,
    c.bill_to_contact_name,
    p.TIME_DAY_KEY

1 个答案:

答案 0 :(得分:2)

从GROUP BY子句中删除TIME_DAY_KEY应该可以解决此问题。

关于聚合的要点是,必须按投影中的所有列进行分组,我们正在聚合的列除外。您正在TIME_DAY_KEY上使用MIN()聚合函数,因此不要将其包含在GROUP BY子句中。