Solved

equivalent syntax for lag or lead

Posted on 2013-10-31
5
710 Views
Last Modified: 2013-10-31
Hello

Using sql server 2008 and want to know the sql syntax equivalent for lag and lead.

Regards
0
Comment
Question by:PHIL Sawyer
  • 2
  • 2
5 Comments
 
LVL 10

Expert Comment

by:jmanishbabu
ID: 39613646
WITH shifted_timeseries AS (
    SELECT event_time,
           value,
           LAG(value) OVER (ORDER BY event_time) AS lagged_value
        FROM timeseries
)
SELECT event_time AS change_time, value AS new_value
FROM shifted_timeseries
    WHERE value != lagged_value;

Check this below link

http://blog.sqlauthority.com/2011/11/15/sql-server-introduction-to-lead-and-lag-analytic-functions-introduced-in-sql-server-2012/
0
 

Author Comment

by:PHIL Sawyer
ID: 39613705
SQL SERVER 2008 does not support LAG LEAD functions? - or am I missing something
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39613731
often row_number() can be used to generate a method to join tables so that the effect of lead/lag.

do you have a specific scenario to share?

(& yes lead/lag does not exist in sql 2008)
0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 total points
ID: 39613745
here's an example (simplified from a previous question) hopefully it will make sense:
;WITH
  CTE AS (
        SELECT
               TransNo
             , TransType
             , TransDate
             , Prod
             , Qty
             , Cost
             , row_number() over (partition BY TransNo ORDER BY TransDate ASC) AS rn
        FROM PurchTransactions
      )
SELECT
      t.TransNo
    , t.TransType
    , t.TransDate
    , t.Prod
    , t.Qty
    , t.Cost
    , t.rn
    , CASE WHEN nxt.TransDate <> t.TransDate THEN nxt.rn ELSE NULL END AS diff_date
    , CASE WHEN nxt.qty       <> t.qty       THEN nxt.rn ELSE NULL END AS diff_qtys
    , CASE WHEN nxt.cost      <> t.cost      THEN nxt.rn ELSE NULL END AS diff_cost
FROM CTE AS t
LEFT JOIN CTE AS nxt ON t.TransNo = nxt.TransNo
                    AND (t.rn + 1) = nxt.rn --<< similar to lead

Open in new window

the row_number gives us a way to perform a self-join so that (here) the "next" record is joined and hence calculations performed in a row mimic the effect of lead
0
 

Author Comment

by:PHIL Sawyer
ID: 39613856
The rownum was the clue and that logic has worked for me  - Thanks
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question