Generate a calculation between records same table...

Last post 09-27-2008 10:36 AM by preethi. 1 replies.
Page 1 of 1 (2 items)
Active Topics My Discussions Unanswered Sort Posts: Previous Next
  • 09-26-2008 3:23 PM

    • redondoj
    • Not Ranked
    • Joined on 03-11-2008
    • Maracay, Venezuela
    • Posts 1

    Generate a calculation between records same table...

     Good Forum.

    I hope you all will find it very well. Sorry for this inconvenience but I need if I could give idea of how to make the following query in a simple and easy, or that method could be applied to the construction and generation of the same in a SELECT in SQL Server 2005:

    TABLE: TBL_POINTCONTROL
    IdTBL_POINTCONTROL INT IDENTITY (1,1) NOT NULL,
    CODE CHAR (5) NOT NULL,
    NAME nvarchar (80) NOT NULL,
    MILE SMALLINT NOT NULL

    Each record that is generated in this table indicates a point control in "x" region of a site "and". The kilometers specifies that the record identifies the point control automotive as such.

    Now, the problem is that I need to create a query predefined in the database WHERE I make the difference (subtraction) of the number of miles from the record that there are 2 to register 1, and expressing how much is the distance between those two points, so to speak.

    Example data for the table:

    IdTBL_PUNTOCONTROL     CODE      NAME               MILES
    1                                    AAA       ENCRUCIJADA      10
    2                                    BBB       PALO NEGRO       25
    3                                    CCC       TAPA TAPA          35
    4                                    DDD      SANTA CLARA       60

    What I want to me as a result of the query is as follows:

    CODE     NAME                CODE           NAME                 DISTANCE
    BBB      PALO NEGRO        AAA           ENCRUCIJADA          15
    CCC      TAPA TAPA           BBB           PALO NEGRO           10
    DDD      SANTA CLARA       CCC          TAPA TAPA               25

    As I think that really is something simple but not give encounter as a solution to this topic. Could someone give me a light solution of the matter.

    Thanks,;).

    Regards,

    Jose Redondo

    Jose Redondo
    Data Warehouse Architech
    Filed under:
  • 09-27-2008 10:36 AM In reply to

    • preethi
    • Top 100 Contributor
    • Joined on 07-01-2003
    • Sri Lanka
    • Posts 169

    Re: Generate a calculation between records same table...

    Try This:
    SELECT A.Code, A.Name, B.Code, B.Name, (B.Mile-A.Mile) As Distance

    FROM TBL_POINTCONTROL A

    INNER JOIN

    TBL_POINTCONTROL B

    ON A.TBL_POINTCONTROL = B.TBL_POINTCONTROL +1

    Cheers,
    Preethiviraj Kulasingham
    MCITP:DBA
Page 1 of 1 (2 items)
Active Topics   My Discussions    Unanswered Posts


© 2000 - 2007 vDerivatives Limited All Rights Reserved.