Looking for a set based solution to this data

  • I hope a better description is in my 3rd post below.

  • chrismat (9/10/2012)


    I've always heard anything can be done in set-based operation without using a cursor, but I can't think of how for this scenario.

    A SELECT query returns four columns from tables, and the fifth column (NewSibling) needs calculated. Here is an example of the output desired:

    OldSponsor, OldSibling, NewSponsor, NewSibling

    177632,0,176896,5

    177632,1,176896,6

    177632,2,176896,7

    171988,0,176896,8

    171988,1,176896,9

    179067,0,176897,55

    179067,1,176897,56

    179067,2,176897,57

    179068,0,176897,58

    179068,1,176897,59

    The NewSponsor currently has some MAX(Sibling); 176896 is currently 4, 176897 is currently 54.

    Rows in same NewSponsor group must start current MAX(Sibling)+1 and increment to account for the others being selected.

    Thanks for any advice.

    Based on the data above, what are the expected results.

  • I hope a better description is in my 3rd post below.

  • declare @activity table (

    activity int,

    sponsor int,

    sibling int);

    insert into @activity (activity,sponsor,sibling)

    select 0,4,0 union

    select 1,4,1 union

    select 2,4,2 union

    select 3,2,0 union

    select 4,2,1 union

    select 5,6,0 ;

    select *

    from @activity;

    --notice for each group of sponsor,

    --the sibling increments 1 for each record;

    --ie max(sibling) for sponsor=4 would return 2

    --get the sponsor and sibling for activities not set to sponsor 4,

    --start the NewSibling at max(sibling)+1 for group sponsor=4,

    --and increment each one in the resultset

    select activity

    , sponsor 'OldSponsor'

    , sibling 'OldSibling'

    , 4 'NewSponsor'

    , '?' 'NewSibling' --first result should be 3, second 4, etc based on max(sibling)+x for group sponsor=4

    from @activity

    where sponsor <> 4

    group by activity, sponsor, sibling

    --how do I increment NewSibling based at Max(Sibling)+1 for the group sponsor=4?

  • What are the expected results based on your data?


    Kingston Dhasian

    How to post data/code on a forum to get the best help - Jeff Moden
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

  • Bit of a guess.

    SELECT activity, oldSponser, oldSibling, newSponser, start+rn AS newSibling

    FROM (SELECT activity, sponsor, sibling, 4, ROW_NUMBER() OVER(ORDER BY activity)

    FROM @activity

    WHERE sponsor <> 4

    GROUP BY activity, sponsor, sibling) a(activity,oldSponser,oldSibling,newSponser,rn)

    CROSS APPLY (SELECT MAX(sibling)

    FROM @activity

    WHERE sponsor = 4) b(start);

    In case I'm wrong and this is a running total problem, check out this awesome article by Jeff Moden --> http://www.sqlservercentral.com/articles/T-SQL/68467/%5B/url%5D


    Forever trying to learn
    My blog - http://www.cadavre.co.uk/
    For better, quicker answers on T-SQL questions, click on the following...http://www.sqlservercentral.com/articles/Best+Practices/61537/
    For better, quicker answers on SQL Server performance related questions, click on the following...http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • Another guess:

    select activity

    , a.sponsor 'OldSponsor'

    , sibling 'OldSibling'

    , 4 'NewSponsor'

    --, '?' 'NewSibling' --first result should be 3, second 4, etc based on max(sibling)+x for group sponsor=4

    , ( SELECT MAX(sibling)

    FROM @activity

    WHERE sponsor = 4

    GROUP BY sponsor) +

    ROW_NUMBER() OVER (ORDER BY activity) 'NewSibling'

    from (

    SELECT activity, sponsor, sibling

    FROM @activity

    where sponsor <> 4

    group by activity, sponsor, sibling) a


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • Kingston Dhasian (9/11/2012)


    What are the expected results based on your data?

    If you copy/paste my post into SSMS and run it, the last active result set contains some question marks. My expected results are the question marks need to be incrementing numbers; the number is based on MAX(Sibling)+1 for the group Sponsor=4; in this example they would start at 3, but in general it could start at any number depending on what the max(sibling) returned for a given group.

  • chrismat (9/11/2012)


    Kingston Dhasian (9/11/2012)


    What are the expected results based on your data?

    If you copy/paste my post into SSMS and run it, the last active result set contains some question marks. My expected results are the question marks need to be incrementing numbers; the number is based on MAX(Sibling)+1 for the group Sponsor=4; in this example they would start at 3, but in general it could start at any number depending on what the max(sibling) returned for a given group.

    The question is, based on the sample data, what should the expected output look like. This means we would like you to play the computer and manually generate the result set you are expecting the code to actually generate.

  • Thanks everyone.

Viewing 10 posts - 1 through 9 (of 9 total)

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