Calculation in new column

  • Hi,

    I don't know how to calculate minus in this case.

    In third column (dif_) are results. Formula is (from excel...there is easy)

    give me distincion 2 row - 1 row, 3 row - 2 row, 4 row- 3 row....etc

    But in sql I don't know how to do it.

    Just imagine that you have all columns except dif_ and how to inser new column in order to get results.

    Will you help me? How to get result in new calculated column? How to calculate minus?

    Nr Report dif_ Report Date 03

    1 238120600 12.14.2008 12:00AM

    2 238720430 12.13.2008 12:00AM

    3 2391501419 12.10.2008 12:00AM

    4 240569430 12.11.2008 12:00AM

    5 2409999112 12.12.2008 12:00AM

    6 250111743 12.15.2008 12:00AM

    7 25085410559 12.16.2008 12:00AM

    8 261413 12.17.2008 12:00AM

  • In Excel turns, referencing horizontally is easy, vertically requires a little bit more work.

    First you need a column or columns to unique ly order the rows. In your case it looks like the [Nr] column will serve this purpose. Then you will need to join your table against itself in order to line it up to the following row. If there are no gaps in [Nr] then this is easy:

    Select Nr

    , (next.Report - curr.Report) as dif_

    , [Report Date 03]

    From ImportedXL as curr

    Left Join ImportedXL as next

    ON next.Nr = curr.Nr + 1

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply