title: qualify statement with case statements in teradata
body: <p>I am trying to execute below query which has got a lot of case statements in qualify clause.</p>
  
  <pre><code>insert into tab2 b
  select  
        a.id,a.name
      from tab1 a
  where a.country='US'
  qualify(
         row_number() over(partition by a.university_id 
         order by 
         case when a.student_in_computerscience_fl='Y' then 1 else 0 end,
         case when a.student_in_chemistry_fl='Y' then 1 else 0 end,
         case when a.student_in_electronics_fl='Y' or a.student_in_physics_fl='Y' then 1 else 0 end,                 )=1)
  </code></pre>
  
  <p>Will the presence of a qualify with lot of case statements generate performance issues?</p>
  
  <p>or do i have to use a temp table where i will store the rank(rk) as an additional field?
  like </p>
  
  <pre><code>insert into tmptab
  select  
        a.id,a.name,
       row_number() over(partition by a.university_id 
         order by 
         case when a.student_in_computerscience_fl='Y' then 1 else 0 end,
         case when a.student_in_chemistry_fl='Y' then 1 else 0 end,
         case when a.student_in_electronics_fl='Y' or a.student_in_physics_fl='Y' then 1 else 0 end                 ) as rk
      from tab1 a
  where a.country='US'
  </code></pre>
  
  <p>and then to insert the final target table from above mentioned temporary table.</p>
  
  <pre><code>issert into tab2
  (name,id)
  select name,id from tmptab where rk=1
  </code></pre>
  
  <p>Kindly share your opinion.</p>