Please help me with comparing two rows values.

  • Hi Team, Can you please help with below query.

    I need to compare two consecutive rows (if BEGDA of second row is 1 day greater than ENDDA of first row then I need to pick First row BEGDA and 2nd row ENDDA)

    Input Table Data:

    PERNRSUBTYBEGDAENDDA

    1010429001/1/20081/15/2015

    10104210001/1/200812/31/2008

    10104210001/1/200912/31/2009

    10104220001/1/20081/15/2008

    10104220001/16/200812/31/2008

    10104220001/1/200912/31/2009

    10104230001/1/200812/31/2008

    10104230001/1/200912/31/2009

    10104230001/5/201012/31/9999

    101042DDPP5/16/200712/31/2007

    101042DDPP2/16/20075/15/2007

    101042MAPP2/1/200712/31/2007

    101042VISI3/1/200712/31/2007

    Output should be like this:

    PERNRSUBTYBEGDAENDDA

    1010429001/1/20081/15/2015

    10104210001/1/200812/31/2009

    10104220001/1/200812/31/2009

    10104230001/1/200812/31/2009

    10104230001/5/201012/31/9999

    101042DDPP2/16/200712/31/2007

    101042MAPP2/1/200712/31/2007

    101042VISI3/1/200712/31/2007

  • ramesh_gattu (10/30/2015)


    Hi Team, Can you please help with below query.

    I need to compare two consecutive rows (if BEGDA of second row is 1 day greater than ENDDA of first row then I need to pick First row BEGDA and 2nd row ENDDA)

    Input Table Data:

    PERNRSUBTYBEGDAENDDA

    1010429001/1/20081/15/2015

    10104210001/1/200812/31/2008

    10104210001/1/200912/31/2009

    10104220001/1/20081/15/2008

    10104220001/16/200812/31/2008

    10104220001/1/200912/31/2009

    10104230001/1/200812/31/2008

    10104230001/1/200912/31/2009

    10104230001/5/201012/31/9999

    101042DDPP5/16/200712/31/2007

    101042DDPP2/16/20075/15/2007

    101042MAPP2/1/200712/31/2007

    101042VISI3/1/200712/31/2007

    Output should be like this:

    PERNRSUBTYBEGDAENDDA

    1010429001/1/20081/15/2015

    10104210001/1/200812/31/2009

    10104220001/1/200812/31/2009

    10104230001/1/200812/31/2009

    10104230001/5/201012/31/9999

    101042DDPP2/16/200712/31/2007

    101042MAPP2/1/200712/31/2007

    101042VISI3/1/200712/31/2007

    Hi and welcome to the forums. As luck would have it this is a pretty common requirement. As such Jeff Moden has an article which explains how to do this exact type of thing. http://www.sqlservercentral.com/articles/T-SQL/71550/[/url]

    If you are unable to figure this out we can help but we require more information. Please see the first link in my signature for best practices when posting questions.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Try this...

    SELECT

    PERNR,SUBTY,MIN(BEGDA) AS BEGDA,MAX(ENDDA) AS ENDDA

    FROM table_name

    GROUP BY PERNR,SUBTY

  • SolveSQL (11/5/2015)


    Try this...

    SELECT

    PERNR,SUBTY,MIN(BEGDA) AS BEGDA,MAX(ENDDA) AS ENDDA

    FROM table_name

    GROUP BY PERNR,SUBTY

    That is an excellent try but it is not quite what they are after. They want groups of contiguous dates for each of those groups which your code won't do. 😀

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

Viewing 4 posts - 1 through 3 (of 3 total)

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