July 31, 2013 at 6:31 pm
Hello All....
I was just thinking about a query I need to write and know how to do it with cursors but would like to try and make it happen using a set based approach. Not sure if it can be done.
Here are my requirements:
I need to group the following into a pool.
step 1 - Get all House Ids
step 2 - For each house id, get all people linked directly to that house
step 3 - for each of those people , get any additional house ids linked to them
step 4 - for each of those additional house ids, get any additional people linked to them
e.g. result
Pool 1 =
House Id | people Id
1 | 1
1 | 2
2 | 2
3 | 2
3 | 4
I hope I have made sense, I can see how easy it would be to code using cursors but an not sure of the set based approach.
Thoughts??
Cheers
July 31, 2013 at 8:01 pm
Sounds like you have what is called an adjacency list with people/houses and you may be able to resolve this with a recursive CTE (or perhaps not).
Two questions:
1. Do you need to stop at step 4 or resolve all the way through to the end?
2. What happens when the same house ID comes up for more than one person? In other words, do you need to ignore duplications on each additional resolution step?
If the answer is Yes to ignoring duplicates, you may need to do this with a set-based loop.
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
July 31, 2013 at 8:21 pm
Thanks for your response...
I suppose technically I could continue all the way to the end but at this stage I want to stop at step 4 (which should be the end anyway)
And yes, I do not want duplicates, so the House | Person combination is unique, but as with my example, a house with multiple persons is perfectly valid.
Do you have some example TSql I could look at/play with?
Cheers
July 31, 2013 at 8:35 pm
If you could give us some sample data in table form it would be a lot easier to help you.
I could be totally off-base, but my first thought is to use multiple CTEs. I'm not sure that recursion is even necessary. Without the tables or data this is just pseduo-code so run with it.
WITH
cteHouses AS --step 1 - Get all House Ids
(SELECT * FROM HouseTable),
cteResidents AS --step 2 - For each house id, get all people linked directly to that house
(SELECT *
FROM cteHouses h
INNER JOIN ResidentsTable r
ON h.houseID = r.HouseID),
cteHouseXref AS --step 3 - for each of those people , get any additional house ids linked to them
(SELECT *
FROM cteHouses h1
INNER JOIN ResidentsTable r1
ON h1.houseID = r1.HouseID),
cteResidentXref AS --step 4 - for each of those additional house ids, get any additional people
(SELECT *
FROM cteHouseXref h2
INNER JOIN ResidentsTable r2
ON h2.houseID = r2.HouseID)
SELECT
*
FROM
cteHouses h3
INNER JOIN
cteResidents r3
ON h3.houseID = r3.HouseID
INNER JOIN
cteHouseXref r4
ON h4.houseID = r4.HouseID
INNER JOIN
cteResidentXref r5
ON h5.houseID = r5.HouseID
July 31, 2013 at 11:03 pm
Steven Willis (7/31/2013)
If you could give us some sample data in table form it would be a lot easier to help you.I could be totally off-base, but my first thought is to use multiple CTEs. I'm not sure that recursion is even necessary. Without the tables or data this is just pseduo-code so run with it.
Thanks for responding but I am not quite sure I get where you are going.
The source data I have is simply a table like the following and I have created a very small bit of data:
CREATE TABLE HouseResident
(
HouseId INT,
ResidentId INT
)
INSERT INTO HouseResident
VALUES(194290,2300365)
INSERT INTO HouseResident
VALUES(194291,2300365)
INSERT INTO HouseResident
VALUES(194291,2300847)
INSERT INTO HouseResident
VALUES(192099,2300847)
INSERT INTO HouseResident
VALUES(192099,2300914)
The result set I need to insert into a table and it should look like the following:
PoolId | HouseId | ResidentId
1 | 194290 | 2300365
1 | 194291 | 2300365
1 | 194291 | 2300874
1 | 192099 | 2300874
1 | 192099 | 2300914
Basically there is a heap of data that I need to pool together like above.
Hope I have made sense?
EDIT > I have just discovered that I need to go more than 4 levels deep.
So I would need a procedure that would "continue to the end" if you get me.
Cheers
August 1, 2013 at 4:33 am
Your sample data looks good. But the results set is simply the sample data directly SELECTed out of the table you provided.
You need to give us consumable sample data (DDL and INSERTs like you did) but also you need to tell us how that data should look after transformation.
I'm not sure I can help you much more today as I'm catching a long flight in the next few hours so will be out of pocket for about 18 hours.
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
August 1, 2013 at 7:36 am
This works but it's quite expensive to run:
WITH Pass1 AS (
SELECT
PoolId = DENSE_RANK() OVER(ORDER BY ResidentId),
HouseId, ResidentId
FROM #HouseResident
),
Pass2 AS (
SELECT
PoolId = MIN(PoolId) OVER (PARTITION BY HouseId),
HouseId, ResidentId
FROM Pass1
),
Pass3 AS (
SELECT
PoolId = MIN(PoolId) OVER (PARTITION BY ResidentId),
HouseId, ResidentId
FROM Pass2
),
Pass4 AS (
SELECT
PoolId = MIN(PoolId) OVER (PARTITION BY HouseId),
HouseId, ResidentId
FROM Pass3
)
SELECT
PoolId = MIN(PoolId) OVER (PARTITION BY ResidentId),
HouseId,
ResidentId
FROM Pass4;
Also, you'd have to add more blocks until no changes are found, in order to "reach the end". Nesting CTE's like this can get very expensive after 3 or 4 blocks. It might be quicker to do the same thing using updates.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
August 1, 2013 at 5:19 pm
Thanks All...
I will have a play with all suggested responses and see what I can come up with.
Cheers
August 1, 2013 at 7:57 pm
Hi All...
Ok, the attached file is where I have got to - can anyone make my code better please?
Is there any way I am able to avoid loops and use a set based approach in its entirity?
For some reason I was unable to add the code to my post, kept getting errors from the web server
Cheers
August 1, 2013 at 8:28 pm
Still would like to see some test data with expected results.
This sounds very similar to a problem I worked out not too long ago. And for that I had a pretty fast solution. I'd just prefer to get it right the first time.
Waiting for my connecting flight to Port Moresby in Brisbane at the moment. I'll check back later.
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
August 1, 2013 at 9:58 pm
dwain.c (8/1/2013)
Still would like to see some test data with expected results.This sounds very similar to a problem I worked out not too long ago. And for that I had a pretty fast solution. I'd just prefer to get it right the first time.
Waiting for my connecting flight to Port Moresby in Brisbane at the moment. I'll check back later.
Thanks bud, I will try to get some test data up for a couple of Pools asap.
Cheers
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply