How to compare two different columns in two different rows.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gabe

    How to compare two different columns in two different rows.

    I'm completely new to SQL and this puzzle is over my head. Any advice
    would be greatly appreciated.

    Table: Visits
    Columns:
    RecNo VisitNo EnterDate
    LeaveDate
    M1111232 V11112869 2008-09-23 10:25:00.000 2008-09-23 10:31:00.000
    M1111232 V11112943 2008-10-24 15:56:00.000 NULL
    M1111233 V11112877 2008-09-23 15:53:00.000 NULL

    I have a table as described above. It contains several thousand rows
    ordered by RecNo by VisitNo.

    VisitNo is the key.

    If there is only one row for a RecNo, I want to bypass it.

    If there is more than one row per RecNo AND
    If the row immediately below the current row has the same RecNo as the
    first row
    I want to compare the Leave Date of the 1st row with the Enter Date of
    the 2nd row and calculate the diff. in # of days.

    If the number of days is greater than 3, I want to list the two date
    values from above and the RecNo. in one row.

    How do I do this?

    Please help. I am completely befuddled. Thanks, Gabe
Working...