SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Looking for a set based solution to this data


Looking for a set based solution to this data

Author
Message
chrismat
chrismat
Valued Member
Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)

Group: General Forum Members
Points: 50 Visits: 593
I hope a better description is in my 3rd post below.
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (52K reputation)SSC Guru (52K reputation)SSC Guru (52K reputation)SSC Guru (52K reputation)SSC Guru (52K reputation)SSC Guru (52K reputation)SSC Guru (52K reputation)SSC Guru (52K reputation)

Group: General Forum Members
Points: 52752 Visits: 38684
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.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
chrismat
chrismat
Valued Member
Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)

Group: General Forum Members
Points: 50 Visits: 593
I hope a better description is in my 3rd post below.
chrismat
chrismat
Valued Member
Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)

Group: General Forum Members
Points: 50 Visits: 593
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?
Kingston Dhasian
Kingston Dhasian
SSCarpal Tunnel
SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)

Group: General Forum Members
Points: 4259 Visits: 5244
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/
Cadavre
Cadavre
SSCarpal Tunnel
SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)

Group: General Forum Members
Points: 4990 Visits: 8483
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/


Forever trying to learn

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/



If you litter your database queries with nolock query hints, are you aware of the side effects?
Try reading a few of these links...

(*) Missing rows with nolock
(*) Allocation order scans with nolock
(*) Consistency issues with nolock
(*) Transient Corruption Errors in SQL Server error log caused by nolock
(*) Dirty reads, read errors, reading rows twice and missing rows with nolock


Craig Wilkinson - Software Engineer
LinkedIn
dwain.c
dwain.c
SSCrazy Eights
SSCrazy Eights (9.8K reputation)SSCrazy Eights (9.8K reputation)SSCrazy Eights (9.8K reputation)SSCrazy Eights (9.8K reputation)SSCrazy Eights (9.8K reputation)SSCrazy Eights (9.8K reputation)SSCrazy Eights (9.8K reputation)SSCrazy Eights (9.8K reputation)

Group: General Forum Members
Points: 9807 Visits: 6431
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!

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?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
chrismat
chrismat
Valued Member
Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)Valued Member (50 reputation)

Group: General Forum Members
Points: 50 Visits: 593
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.
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (52K reputation)SSC Guru (52K reputation)SSC Guru (52K reputation)SSC Guru (52K reputation)SSC Guru (52K reputation)SSC Guru (52K reputation)SSC Guru (52K reputation)SSC Guru (52K reputation)

Group: General Forum Members
Points: 52752 Visits: 38684
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.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search