0

how can I get previous row data from same column?

I have data like this:

A       B              C
1    20181111800   20181111800
2    20181111809   20181111809
3    20181140545   20181140545
4    20181111817   20181140545
5    20181111826   20181140545

Column C should be calculated like this:

First row = same as first row in A column

Second rows excel formula looks like this: =IF(C1<A2;A2;C1)

Third rows excel formula looks like this: =IF(C2<A3;A3;C2)

And so on for rest rows..

How can I change this formula into sql query?

  • 3
    It looks like you want us to write some code for you. While many users are willing to produce code for a coder in distress, they usually only help when the poster has already tried to solve the problem on their own. A good way to demonstrate this effort is to include the code you've written so far, example input (if there is any), the expected output, and the output you actually get (console output, tracebacks, etc.). The more detail you provide, the more answers you are likely to receive. Check the [FAQ] and [ask]. – hellow Apr 25 '18 at 12:35

1 Answers1

0

It looks like you want a cumulative maximum:

select a, b, max(b) over (order by a)
from t;

This is an ANSI-standard window function, available in most databases.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786