Quantcast
Channel: Teradata Forums - Database
Viewing all articles
Browse latest Browse all 14773

Query Rewriting. - forum topic by barani_sachin

$
0
0

Hi All,
I have two versions(One is old which is being rewritten using some new standards) of code both claiming to accomplish the same thing; with the subset of data we tested both seems to give the same results. If you guys see any issues in the new code, pelase let me know.
 
Old Code:

SELECT  A.Ten_Dig_Val,A.SUP_Ten_Dig_Val
FROM    Rel_Detail A,
(SELECT Ten_Dig_Val,MAX(Rel_Detail)AS EFF_DT
FROM    Rel_Detail
WHERE   Ten_Dig_Val IN (SELECT Ten_Dig_Val FROM Ten_Dig_Val_list)
GROUP BY Ten_Dig_Val) B
WHERE   A.Ten_Dig_Val=B.Ten_Dig_Val AND
A.HIER_Rel_Detail = B.EFF_DT AND
Lvl_Nbr=(SELECT MIN(Lvl_Nbr) FROM Rel_Detail WHERE A.Ten_Dig_Val=Ten_Dig_Val AND HIER_Rel_Detail = B.EFF_DT)

 
New Approach:

SELECT  A.Ten_Dig_Val,A.SUP_Ten_Dig_Val
FROM    Rel_Detail A
inner join
(
SELECT Temp1.Ten_Dig_Val,MAX(HIER_Rel_Detail)AS EFF_DT
FROM    Rel_Detail Temp1
inner join Ten_Dig_Val_list Temp2
on Temp1.Ten_Dig_Val = Temp2.Ten_Dig_Val
GROUP BY Temp1.Ten_Dig_Val
) B
on   A.Ten_Dig_Val=B.Ten_Dig_Val
AND A.HIER_Rel_Detail = B.EFF_DT 

inner join temp3 t3
on t3.Lvl_Nbr = A.Lvl_Nbr
and t3.Ten_Dig_Val = A.Ten_Dig_Val
AND t3.HIER_Rel_Detail = B.EFF_DT

create volatile table temp3 
(Ten_Dig_Val BIGINT, Lvl_Nbr BYTEINT, HIER_Rel_Detail DATE FORMAT 'YYYY-MM-DD')on commit preserve rows;

insert into temp3
SELECT Ten_Dig_Val,Lvl_Nbr,HIER_Rel_Detail FROM Rel_Detail
qualify row_number() over(partition by Ten_Dig_Val,HIER_Rel_Detail order by Lvl_Nbr)=1

collect stats on temp3 column (Ten_Dig_Val);
collect stats on temp3 column (Lvl_Nbr);
collect stats on temp3 column (HIER_Rel_Detail);

 
Thanks in advance :)

Forums: 

Viewing all articles
Browse latest Browse all 14773

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>