Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Looking for a set based solution to this data Expand / Collapse
Author
Message
Posted Monday, September 10, 2012 7:10 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, October 26, 2012 3:17 PM
Points: 22, Visits: 591
I hope a better description is in my 3rd post below.
Post #1357082
Posted Monday, September 10, 2012 7:24 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 8:48 PM
Points: 23,015, Visits: 31,536
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.



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)
Post #1357083
Posted Monday, September 10, 2012 10:14 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, October 26, 2012 3:17 PM
Points: 22, Visits: 591
I hope a better description is in my 3rd post below.
Post #1357126
Posted Monday, September 10, 2012 10:55 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Sunday, July 20, 2014 11:55 AM
Points: 1,945, Visits: 2,860
You gave us no DDL, no specs and no desired results. You talk about a "current row", but there is no temporal or ordering in the data.

Would you like to try again and follow the basic rules of netiquette?


Books in Celko Series for Morgan-Kaufmann Publishing
Analytics and OLAP in SQL
Data and Databases: Concepts in Practice
Data, Measurements and Standards in SQL
SQL for Smarties
SQL Programming Style
SQL Puzzles and Answers
Thinking in Sets
Trees and Hierarchies in SQL
Post #1357134
Posted Monday, September 10, 2012 11:27 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, October 26, 2012 3:17 PM
Points: 22, Visits: 591
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?
Post #1357140
Posted Tuesday, September 11, 2012 1:12 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Wednesday, July 16, 2014 8:33 AM
Points: 2,616, Visits: 4,720
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/
Post #1357165
Posted Tuesday, September 11, 2012 1:35 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Thursday, July 24, 2014 2:26 PM
Points: 2,422, Visits: 7,437
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/



Not a DBA, just 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


LinkedIn | Blog coming soon (for sufficiently large values of "soon" )!
Post #1357174
Posted Tuesday, September 11, 2012 2:37 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 9:44 PM
Points: 3,610, Visits: 5,223
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!
Post #1357193
Posted Tuesday, September 11, 2012 9:52 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, October 26, 2012 3:17 PM
Points: 22, Visits: 591
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.
Post #1357543
Posted Tuesday, September 11, 2012 9:55 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 8:48 PM
Points: 23,015, Visits: 31,536
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.



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)
Post #1357547
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse