[ www.netezza.com ]
4 Replies Last post: Mar 4, 2010 1:00 AM by caro  
arunsun   1 posts since
Jan 6, 2010
Currently Being Moderated

Jan 6, 2010 6:35 AM

Equivalent of Moving Difference(MDIFF) in Netezza

Has anyone come across a need to have MDIFF function in Netezza?

This function calculates the Moving difference in any other database (like Teradata).

 

Thanks in advance.

Shawn Fox   520 posts since
Aug 15, 2006
Currently Being Moderated
1. Jan 6, 2010 1:00 PM in response to: arunsun
Re: Equivalent of Moving Difference(MDIFF) in Netezza

Actually I am pretty sure 'any other' database, means only in Teradata.  MDIFF is a teradata specific function.  You can use the standard LAG function to do the same thing on any database which supports the SQL-99 standards.

 

select c1- LAG(c1,1) over (order by c2)

from <table>

 

That would return the current row C1 - prior row C1 (ordered by c2)

 

LAG(c1,2) over (order by c2) would return 2 rows prior, etc

 

You can also partition the data so that you only get values for a specifc data element, for example

 

select

     customer_id,

     purchase_amt,

     LAG(purchase_amt,1) over (partition by customer_id order by purchase_date)

from

     sales;

 

There are many of these analytic functions available.  SUM, AVG, COUNT, MIN, MAX, LAG, LEAD, ROW_NUMBER, RANK to name a few.  Here is a good writeup on using analytic functions in SQL.  It is written for Oracle but most of these functions will work exactly as described on pretty much any database which supports SQL-99.

 

http://orafaq.com/node/55

caro   25 posts since
Aug 3, 2009
Currently Being Moderated
2. Mar 3, 2010 12:06 AM in response to: Shawn Fox
Re: Equivalent of Moving Difference(MDIFF) in Netezza

hi all,

 

 

I have had the same problem of converting mdiff to netezza, and found that lag doesnt really give me  the solution.

 

 

in teradata, mdiff is used to find the difference between records while lag only shows the records with a lag, which is not the correct solution.

 

 

we could instead use sum function which is also works on any database which supports the SQL-99 standards.

 

here is an example:

 

 

select promo_week_id,

          week_nbr,

          mdiff(week_nbr,2,promo_week_id)

from database.table

 

 

select promo_week_id,

          week_nbr,

          week_nbr-SUM(week_nbr) OVER (ORDER BY promo_week_id ROWS BETWEEN 2 PRECEDING AND 2 PRECEDING)

from database.table.

 

 

If the same could be achieved by lag i would like to know  the solution for the same.

 

 

Thanks.

Shawn Fox   520 posts since
Aug 15, 2006
Currently Being Moderated
3. Mar 3, 2010 8:07 AM in response to: caro
Re: Equivalent of Moving Difference(MDIFF) in Netezza

From the prior post:

 

select c1- LAG(c1,1) over (order by c2)

from <table>

 

Or in your specific case:

 

select promo_week_id,

          week_nbr,

          week_nbr-LAG(week_nbr,2) OVER (ORDER BY promo_week_id)

from database..table

Using SUM is interesting in a Rube Goldberg kind of way, but LAG is much easier to use for this purpose.

caro   25 posts since
Aug 3, 2009
Currently Being Moderated
4. Mar 4, 2010 1:00 AM in response to: Shawn Fox
Re: Equivalent of Moving Difference(MDIFF) in Netezza

Thanks Shawn it works.

More Like This

  • Retrieving data ...