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

Imitating procedural language using SQL - response (2) by dnoeth

$
0
0

 
Hi Abin,
you can probably do that as OLAP functions allow access to the "previous" record, e.g.

   min(Action)
   over (partition by customer
         order by entry_date
         rows between 1 preceding and 1 preceding) as prev_action

and you use it in a CASE:

   case
      when prev_Action = 'WENT-LST' and from <> to then 'Y'
      when Action = 'WENT-LST' then 'N'

There's only one condition which is more complex: 

Flag is Y because one of previous record has Action as WENT-LST and From <> To and previous record has flag   Y

The first part is:

max(Action)
             over (partition by customer
             order by entry_date
             rows between unbounded preceding and 1 preceding)  = 'WENT-LST' and from <> to then 'Y' else 'N' end

but for the "previous record has flag   Y" you need to check if there's another algorithm, because the flag is based on the result of an OLAP function (kind of recursion). 
 
 

If OLAP doesn't work you can apply the cursor logic within a WITH RECURSIVE, this will be easier but depending on the number of rows per customer it might be much slower than OLAP. But still much faster than a cursor which is processed sequentially on a single node while WITH is running in parallel.

 

If you could show the cursor logic i might help further.

 

Dieter

 

 


Viewing all articles
Browse latest Browse all 14773

Trending Articles



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