Results 1 to 6 of 6

Thread: [RESOLVED] Compare rows of a record set @ sp (SQL Server)

  1. #1

    Thread Starter
    Hyperactive Member Chathura's Avatar
    Join Date
    Nov 2005
    Location
    Sri Lanka
    Posts
    345

    Resolved [RESOLVED] Compare rows of a record set @ sp (SQL Server)

    Hi,
    I'm using ms sqlserver 2000. I want to write a stored procedure and it must do following
    1. Select certain records from a table using a where clause
    2. compare two fields of two records (say F1 of row 1 with F2 of row2)
    3. update another column (say F3 of row2)
    4. Use a loop to continue these 2 & 3 steps

    Please help me to do this. Any suggesion will be highly appreciated.
    If the post is heplful, Please Rate it
    Chathura Wijekoon

  2. #2
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Compare rows of a record set @ sp (SQL Server)

    Well - that sounds like a need for a cursor loop in the SPROC - which of course most people try to avoid.

    Show us some sample data - what would row 1 and row 2 actually look like?

    Show us enough rows of data so we can see this F1 and F2 relationship.

    Is it only two pairs of rows that "compare"?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  3. #3

    Thread Starter
    Hyperactive Member Chathura's Avatar
    Join Date
    Nov 2005
    Location
    Sri Lanka
    Posts
    345

    Re: Compare rows of a record set @ sp (SQL Server)

    Quote Originally Posted by szlamany

    Show us enough rows of data so we can see this F1 and F2 relationship.
    Thanks szlamany for your reply.
    Show us some sample data - what would row 1 and row 2 actually look like?
    See these rows
    =========Col1================Col2==============Col3
    Row1 ==12/12/2007 08:00:00 ==12/12/2007 10:00:00 ===0
    Row2 ==12/12/2007 11:00:00 ==12/12/2007 13:00:00 ===1(11:00:00-10:00:00)
    Row3 ==12/12/2007 15:00:00 ==12/12/2007 16:00:00 ===2
    Row4 ==12/12/2007 16:00:00 ==12/12/2007 17:00:00 ===0

    Is it only two pairs of rows that "compare"?
    No. I have more than two rows.
    **Col3 is the column which should be updated after comparing Row2 Col1 and Row1Col2 with the difference of the two values.
    Last edited by Chathura; Mar 2nd, 2007 at 03:34 AM.
    If the post is heplful, Please Rate it
    Chathura Wijekoon

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Compare rows of a record set @ sp (SQL Server)

    Here's a way to do it without storing col3 at all...

    sql Code:
    1. Set NoCount On
    2. -- Temp table to fool with
    3. Create Table #TempTbl (Col1 datetime, Col2 datetime)
    4.  
    5. -- Your data
    6. Insert into #TempTbl values('12/12/2007 08:00:00','12/12/2007 10:00:00')
    7. Insert into #TempTbl values('12/12/2007 11:00:00','12/12/2007 13:00:00')
    8. Insert into #TempTbl values('12/12/2007 15:00:00','12/12/2007 16:00:00')
    9. Insert into #TempTbl values('12/12/2007 16:00:00','12/12/2007 17:00:00')
    10.  
    11. -- A quick Select to show the data
    12. Select * From #TempTbl
    13.  
    14. -- Now here's a Select that joins back to the table itself
    15. -- and joins to the prior row!
    16. Select * From #TempTbl T1
    17.    Left Join #TempTbl T2 on T2.Col2=(Select Max(T3.Col2) From #TempTbl T3
    18.                         Where T3.Col2<T1.Col2)
    19.  
    20. -- And now your final output - all without ever storing the Col3 value
    21. -- instead calc'ing it at query time
    22.  
    23. Select T1.Col1,T1.Col2,IsNull(DateDiff(hh,T2.Col2,T1.Col1),0) From #TempTbl T1
    24.    Left Join #TempTbl T2 on T2.Col2=(Select Max(T3.Col2) From #TempTbl T3
    25.                         Where T3.Col2<T1.Col2)
    26.  
    27.  
    28. Drop Table #TempTbl

    sql Code:
    1. Col1                     Col2                                                  
    2. ------------------------ ------------------------------------------------------
    3. 2007-12-12 08:00:00.000  2007-12-12 10:00:00.000
    4. 2007-12-12 11:00:00.000  2007-12-12 13:00:00.000
    5. 2007-12-12 15:00:00.000  2007-12-12 16:00:00.000
    6. 2007-12-12 16:00:00.000  2007-12-12 17:00:00.000
    7.  
    8. Col1                     Col2                     Col1                     Col2                                                  
    9. ------------------------ -----------------------  ------------------------ ------------------------------------------------------
    10. 2007-12-12 08:00:00.000  2007-12-12 10:00:00.000  NULL                     NULL
    11. 2007-12-12 11:00:00.000  2007-12-12 13:00:00.000  2007-12-12 08:00:00.000  2007-12-12 10:00:00.000
    12. 2007-12-12 15:00:00.000  2007-12-12 16:00:00.000  2007-12-12 11:00:00.000  2007-12-12 13:00:00.000
    13. 2007-12-12 16:00:00.000  2007-12-12 17:00:00.000  2007-12-12 15:00:00.000  2007-12-12 16:00:00.000
    14.  
    15. Col1                     Col2                      
    16. ------------------------ ------------------------ -----------
    17. 2007-12-12 08:00:00.000  2007-12-12 10:00:00.000  0
    18. 2007-12-12 11:00:00.000  2007-12-12 13:00:00.000  1
    19. 2007-12-12 15:00:00.000  2007-12-12 16:00:00.000  2
    20. 2007-12-12 16:00:00.000  2007-12-12 17:00:00.000  0

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  5. #5

    Thread Starter
    Hyperactive Member Chathura's Avatar
    Join Date
    Nov 2005
    Location
    Sri Lanka
    Posts
    345

    Re: [RESOLVED] Compare rows of a record set @ sp (SQL Server)

    Thanks a lot "szlamany". I got through with the problem with your help. Thanks again. your help must be appreciate more than once because I couldn't find any others help other than you.
    If the post is heplful, Please Rate it
    Chathura Wijekoon

  6. #6
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: [RESOLVED] Compare rows of a record set @ sp (SQL Server)

    I am glad it worked for you.

    I enjoy the challenges that people post here - using SQL to solve a problem with "set-based" logic is always a great thing to do!

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width