问题描述
我不是 sql 专家,因此这个问题。我们使用以下查询,我想了解它是如何处理的。所有查询都使用同一个表,我想知道它是自上而下的方法还是自下而上的方法?如何理解这个查询?
CREATE TABLE <tablename1>
select cr_latest_canumber.CAGID,cr_latest_canumber.GFCID,cast(cr_renewal.FAC_LAST_renewal_DATE as STRING) as FAC_LAST_renewal_DATE,cr_cust_total.CUST_TOTAL_CA_LTM,cr_latest_canumber.ca_number as LATEST_CA_NUMBER,cr_latest_canumber.sponsoring_officer as CA_SPONSORING_OFFICER,cr_latest_canumber.rorc as CA_RORC_PCT,cr_latest_canumber.rorc_bucket as CA_RORC_BUCKET,cr_latest_canumber.ca_segment as CA_CLIENT_SEGMENT from (select S.cagid,S.ca_number,S.sponsoring_officer,S.rorc,S.ca_segment,case
when cast(rorc as decimal(8,2)) <= 10 then "0%-10%"
when cast(rorc as decimal(8,2)) > 10 and cast(rorc as decimal(8,2)) <= 20 then "10%-20%"
when cast(rorc as decimal(8,2)) > 20 and cast(rorc as decimal(8,2)) <= 30 then "20%-30%"
when cast(rorc as decimal(8,2)) > 30 and cast(rorc as decimal(8,2)) <= 40 then "30%-40%"
when cast(rorc as decimal(8,2)) > 40 and cast(rorc as decimal(8,2)) <= 50 then "40%-50%"
when cast(rorc as decimal(8,2)) > 50 then ">50%"
else 'null'
end rorc_bucket
FROM
( select distinct cagid,ca_number,sponsoring_officer,rorc,ca_segment,to_date(from_unixtime(max(ca_booked_date) DIV 1000)),rank() over (partition by cagid order by to_date(from_unixtime(max(ca_booked_date) DIV 1000)) desc) as r
from
<tablename>
where
((ca_type='') or
(ca_type='' and review_type='') or
(ca_type='' and review_type='') or
(ca_type='' and review_type='') or
(ca_type='' and review_type='') or
(ca_type='' and review_type='') or
(ca_type='' and review_type='') )
) S where S.r = 1
) cr_latest_canumber left outer join
(
select to_date(from_unixtime(max(ca_booked_date) DIV 1000)) as FAC_LAST_renewal_DATE,cagid,gfcid from
<tablename>
where
CREDIT_STATUS IN ('','') and
((ca_type='') or
(ca_type='' and review_type='') or
(ca_type='' and review_type='') or
(ca_type='' and review_type='')
) group by cagid,gfcid
) cr_renewal on cr_latest_canumber.cagid = cr_renewal.cagid and cr_latest_canumber.gfcid = cr_renewal.gfcid left outer join
(
select credit.cagid,count(distinct(ca_number)) as CUST_TOTAL_CA_LTM
<tablename> credit,(select report_date_12,report_date from <tablename2> cobdt where row_num=1) cobdt
where
to_date(from_unixtime(ca_creation_date DIV 1000)) between cobdt.report_date_12 and cobdt.report_date
and CREDIT_STATUS NOT IN ('','')
and credit.cagid = credit.gfcid
group by credit.cagid
) cr_cust_total on cr_renewal.cagid=cr_cust_total.cagid;
解决方法
要理解查询,只需拆分嵌套查询并单独运行即可获得清晰的思路 有 3 个子查询,然后左外连接该 3query 以生成新表 查询:1 table1:cr_latest_canumber
select distinct cagid,ca_number,sponsoring_officer,rorc,ca_segment,to_date(from_unixtime(max(ca_booked_date) DIV 1000)),rank() over (partition by cagid order by to_date(from_unixtime(max(ca_booked_date) DIV 1000)) desc) as r
from
<tablename>
where
((ca_type='') or
(ca_type='' and review_type='') or
(ca_type='' and review_type='') or
(ca_type='' and review_type='') or
(ca_type='' and review_type='') or
(ca_type='' and review_type='') or
(ca_type='' and review_type='') )
) S where S.r = 1
查询:2 table2:cr_renewal
select to_date(from_unixtime(max(ca_booked_date) DIV 1000)) as FAC_LAST_RENEWAL_DATE,cagid,gfcid from
<tablename>
where
CREDIT_STATUS IN ('','') and
((ca_type='') or
(ca_type='' and review_type='') or
(ca_type='' and review_type='') or
(ca_type='' and review_type='')
) group by cagid,gfcid
query3:table3:cr_cust_total
select credit.cagid,count(distinct(ca_number)) as CUST_TOTAL_CA_LTM
<tablename> credit,(select report_date_12,report_date from <tablename2> cobdt where row_num=1) cobdt
where
to_date(from_unixtime(ca_creation_date DIV 1000)) between cobdt.report_date_12 and cobdt.report_date
and CREDIT_STATUS NOT IN ('','')
and credit.cagid = credit.gfcid
group by credit.cagid
决赛
CREATE TABLE <tablename1>
select cr_latest_canumber.CAGID,cr_latest_canumber.GFCID,cast(cr_renewal.FAC_LAST_RENEWAL_DATE as STRING) as FAC_LAST_RENEWAL_DATE,cr_cust_total.CUST_TOTAL_CA_LTM,cr_latest_canumber.ca_number as LATEST_CA_NUMBER,cr_latest_canumber.sponsoring_officer as CA_SPONSORING_OFFICER,cr_latest_canumber.rorc as CA_RORC_PCT,cr_latest_canumber.rorc_bucket as CA_RORC_BUCKET,cr_latest_canumber.ca_segment as CA_CLIENT_SEGMENT from (select S.cagid,S.ca_number,S.sponsoring_officer,S.rorc,S.ca_segment,case
when cast(rorc as decimal(8,2)) <= 10 then "0%-10%"
when cast(rorc as decimal(8,2)) > 10 and cast(rorc as decimal(8,2)) <= 20 then "10%-20%"
when cast(rorc as decimal(8,2)) > 20 and cast(rorc as decimal(8,2)) <= 30 then "20%-30%"
when cast(rorc as decimal(8,2)) > 30 and cast(rorc as decimal(8,2)) <= 40 then "30%-40%"
when cast(rorc as decimal(8,2)) > 40 and cast(rorc as decimal(8,2)) <= 50 then "40%-50%"
when cast(rorc as decimal(8,2)) > 50 then ">50%" else 'null'
end rorc_bucket
FROM
(cr_latest_canumber left outer join
cr_renewal on cr_latest_canumber.cagid = cr_renewal.cagid and cr_latest_canumber.gfcid = cr_renewal.gfcid left outer join
cr_cust_total on cr_renewal.cagid=cr_cust_total.cagid);