Home Page for the TeradataForum
 

Archives of the TeradataForum

Message Posted: Thu, 09 Sep 2004 @ 06:33:51 GMT


     
  <Prev Next>   <<First <Prev Next> Last>>  


Subj:   Re: SQL puzzle...
 
From:   Dieter Noeth

Dirk Lerner wrote:

  There are some (defined) rules to solve the problem: In bla2 for every combination (bdf,sai,ek_akt) there will be a genre 0. So if in bla1 genre is NULL then use 0. If genre is something else, but not in bla2.genre available then use 0 as genre.  



There's no way to select only one row, but you can get rid of the extra row:

     sel * from bla1 a
     left join bla2 b
     on a.bdf=b.bdf
     and a.sai=b.sai
     and a.ek_akt=b.ek_akt
     and (a.genre=b.genre or b.genre=0)
     qualify
        rank() over (partition by a.bdf,a.sai,a.ek_akt, a.genre
                     order by b.genre desc) = 1

But rank is a huge overhead, maybe it's better to do an extra join:

     sel a.*,b2.*
     from bla1 a
     left join bla2 b
     on a.bdf=b.bdf
     and a.sai=b.sai
     and a.ek_akt=b.ek_akt
     and a.genre=b.genre
     left join bla2 b2
     on a.bdf=b2.bdf
     and a.sai=b2.sai
     and a.ek_akt=b2.ek_akt
     and coalesce(b.genre,0)=b2.genre

Dieter



     
  <Prev Next>   <<First <Prev Next> Last>>  
 
 
 
 
 
 
 
 
  
  Top Home Privacy Feedback  
 
 
Copyright for the TeradataForum (TDATA-L), Manta BlueSky    
Copyright 2016 - All Rights Reserved    
Last Modified: 15 Jun 2023