I don't see what's wrong with allowing matching since the question was "all possible combinations". <- EDIT: I believe it was the original question in the thread Steve referenced.
However, if it needs to be no matching between columns, then there are definitely plenty of ways to do it. CTEs could be used, but I foresee a nested monstrosity depending on how many columns you have. Also, it depends on if your original table is going to be one column which every number has to be matched with all the other numbers or not.
I did code my original for 2 columns only, because that's what I do at work. Two columns, every possible combination. So, the columns have to increase based on the number of values you have to work with?
I'll see what I can come up with. In the meantime, here's a nested CTE for 3 values which discounts matching.
DECLARE @MaxTally int;
SELECT @MaxTally = Max(ID) FROM dbo.#Combinations;
WITH FirstSet AS
(SELECT c.ID AS ID1, t.N AS ID2
FROM dbo.#Combinations c
CROSS JOIN CreditDBA_Admin.dbo.Tally t
WHERE t.N <= @MaxTally AND t.N <> c.ID),
SecondSet AS
(SELECT fs.ID1, fs.ID2, t.N AS ID3
FROM FirstSet fs
CROSS JOIN CreditDBA_Admin.dbo.Tally t
WHERE t.N <= @MaxTally AND t.N <> fs.ID1 AND t.N <> fs.ID2)
SELECT * from SecondSet
Order by ID1;
I put in the non-repeat bit because of the way Steve wrote his original Where clause. I just moved it into the Joins.
- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread
"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Yeah, that was my first thought Gus, and I found they have the exact same execution plans. I tried it from the other direction, a straight cross join then excepting out the matchers, and the plans got even worse.
From a performance perspective, I think that's probably the best way (Where or ON clause), if cumbersome to write. I'm still trying to figure out a 'pretty' way that doesn't turn into a mess.
Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.
For better assistance in answering your questions[/url] | Forum Netiquette
For index/tuning help, follow these directions.[/url] |Tally Tables[/url]
Twitter: @AnyWayDBA
GSquared (5/18/2011)
I put in the non-repeat bit because of the way Steve wrote his original Where clause. I just moved it into the Joins.
Gus: If this concept is expanded to conver the numbers 1 to 5 (and 5 columns), the query would look a little more complicated than the current one. I guess, Jeff is looking for a less complex query (complex = several WHERE or JOIN conditions).
LutzM (5/18/2011)
GSquared (5/18/2011)
I put in the non-repeat bit because of the way Steve wrote his original Where clause. I just moved it into the Joins.Gus: If this concept is expanded to conver the numbers 1 to 5 (and 5 columns), the query would look a little more complicated than the current one. I guess, Jeff is looking for a less complex query (complex = several WHERE or JOIN conditions).
Jeff or Steve?
LutzM (5/18/2011)
GSquared (5/18/2011)
I put in the non-repeat bit because of the way Steve wrote his original Where clause. I just moved it into the Joins.Gus: If this concept is expanded to conver the numbers 1 to 5 (and 5 columns), the query would look a little more complicated than the current one. I guess, Jeff is looking for a less complex query (complex = several WHERE or JOIN conditions).
You can actually build the thing dynamically through a string concatenator to use as many copies of the Values table as you want, adding as many columns as you need, without having to build/see the ugly code that happens when you get, say, 20 columns added. It's also less likely to accidentally leave out a join condition (skip from 17 to 19 for example), than coding it by hand.
But that's ugly in its own way, of course.
- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread
"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Brandie Tarvin (5/18/2011)
LutzM (5/18/2011)
GSquared (5/18/2011)
I put in the non-repeat bit because of the way Steve wrote his original Where clause. I just moved it into the Joins.Gus: If this concept is expanded to conver the numbers 1 to 5 (and 5 columns), the query would look a little more complicated than the current one. I guess, Jeff is looking for a less complex query (complex = several WHERE or JOIN conditions).
Jeff or Steve?
Jeff. No. Steve. AAAAAAAHHHHHHHH
Viewing 15 posts - 1 through 15 (of 24 total)
You must be logged in to reply to this topic. Login to reply