我的这段SQL代码,执行了1个小时,没有报错,但是最终结果是“空”,求助。
(1)这段代码应该怎么优化?
(2)这段代码为什么执行完是空?
with sanlian as (
select uuid, att_user, intrst_user, inten_user, car_owner, top1att, top3att, top5att, hour_prf, level_prf, series_prf, spec_prf, brand_prf, place_prf, price_prf, leads_num, leads_series
from AA表
where uuid in(select distinct uuid from AA表) and substr(dt,1,7) = '2018-10'
),
fulltable as(
select uuid, age_age as age,
sex_sex as sex,
car_loan as car_loan,
mortgage as mortgage,
er_value as customer_value,
du_level as edu_level,
ge_name as stage,
occupation as occupation,
tal_status as marital_status,
illing as loan_willing,
city as city,
concen as concen,
interval_day as interval_day
from BB表
where seri = '1'
),
atttime as(
select b.uuid, a.att_time
from(
( select uuid, cookie from CC表) b
join
(select cookie, sum(att_time) as att_time
from DD表
where substr(dt,1,7) = '2018-10' cookie <> '0'
group by cookie
) a
on a. cookie = b.cookie
)
),
inten as (
select b.uuid, a.ordernum
from(
(
select phone, sum(ordernum) as ordernum
from EE表
where substr(dt,1,7) = '2018-10' and series_id = '1'
group by phone
) a
join
(
select uuid, phone
from FF表
)b
on a.phone = b.phone
)
),
fangwen as(
select uuid, y_id, y_name, sum(y_ratio) as y_ratio, sum(n_ratio) as n_ratio
from GG表
where substr(dt,1,7) = '2018-10' and series_id = '1'
group by uuid, y_id, y_name
)
select *
from sanlian as a
left join atttime as e
on a.uuid = e.uuid
full join intention as b
on a.uuid = b.uuid
left join fangwen as f
on a.uuid = f.uuid
join fulltable as g
on a.uuid = g.uuid
最后*是35个来自a\b\e\f\g的变量。
参考GPT和自己的思路:
针对你的问题,我可以提供以下回答:
(1)对于代码的优化,首先建议对代码进行排查和调试,检查表之间的关联关系和查询条件是否正确,以及是否有数据丢失、重复等问题。其次,可以考虑对查询语句进行简化,避免不必要的嵌套和重复操作。另外也可以对表进行合并或者使用索引等方式来优化代码执行效率。
(2)造成结果为空的原因可能有很多,需要具体排查。可能是原始数据表中没有符合查询条件的数据,也可能是查询结果被误归为NULL或者空值,还有可能是查询结果存在错误但是没有被报错。建议根据具体情况进行调试和修改代码。