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.
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.
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.
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.
Thanks Shawn it works.