Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
jackv
Master Smack Fu Yak Hacker
2179 Posts |
Posted - 2004-09-08 : 16:41:49
|
| I have a problem, which I can explain but am struggling to translate intoSQL syntax.myTable has 3 columns, with some example valuesID NAME NUMBER_ITEMS1 john 42 tim 33 john 24 tim 15 roger 66 roger 7Essentially, I am trying to derive every possible combination between NAMEand NUMBER_ITEMS , but as a set.The set being "john,tim,roger".which conceptually become the columnse,g JOHN TIM ROGERcombination1 4 3 6combination 2 4 1 6combination3 4 3 7combination4 4 1 7Ultimately, I just need them as a coma delimited string. But , mostimportantly I need to be able to get past STEP 1ThanksJack |
|
|
DustinMichaels
Constraint Violating Yak Guru
464 Posts |
Posted - 2004-09-08 : 16:59:09
|
| I have no idea how you could do this with a single sql statement.Is there anyway you can get all of NAMES and NUMBER_ITEMS from the database and once this is done do processing in your application get to combination? This may be your only hope.Dustin Michaels |
 |
|
|
MichaelP
Jedi Yak
2489 Posts |
Posted - 2004-09-08 : 17:02:59
|
Well, here's about half the solution:DECLARE @MyTable TABLE (FirstName VARCHAR(20), Number_Items INT)INSERT INTO @MyTable(FirstName, Number_Items) VALUES('John', 4)INSERT INTO @MyTable(FirstName, Number_Items) VALUES('John', 2)INSERT INTO @MyTable(FirstName, Number_Items) VALUES('Tim', 3)INSERT INTO @MyTable(FirstName, Number_Items) VALUES('Tim', 1)INSERT INTO @MyTable(FirstName, Number_Items) VALUES('Roger', 6)INSERT INTO @MyTable(FirstName, Number_Items) VALUES('Roger', 7)SELECT a.FirstName, a.Number_Items, b.Number_Items, c.Number_ItemsFROM @MyTable a CROSS JOIN @MyTable b CROSS JOIN @MyTable cWHERE a.FirstName <> b.FirstName AND a.FirstName <> c.FirstName AND b.FirstName <> c.FirstName AND b.Number_Items <> c.Number_ItemsORDER BY a.FirstNameWhere is Dr. Cross Join?Michael<Yoda>Use the Search page you must. Find the answer you will.</Yoda> |
 |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2004-09-08 : 17:07:59
|
Why couldn't you do:DECLARE @MyTable TABLE (FirstName VARCHAR(20), Number_Items INT)INSERT INTO @MyTable(FirstName, Number_Items) VALUES('John', 4)INSERT INTO @MyTable(FirstName, Number_Items) VALUES('John', 2)INSERT INTO @MyTable(FirstName, Number_Items) VALUES('Tim', 3)INSERT INTO @MyTable(FirstName, Number_Items) VALUES('Tim', 1)INSERT INTO @MyTable(FirstName, Number_Items) VALUES('Roger', 6)INSERT INTO @MyTable(FirstName, Number_Items) VALUES('Roger', 7)Select *From (Select John = Number_Items From @myTable Where firstName = 'John') A,(Select Tim = Number_Items From @myTable Where firstName = 'Tim') B,(Select Roger = Number_Items From @myTable Where firstName = 'Roger') CCorey |
 |
|
|
MichaelP
Jedi Yak
2489 Posts |
Posted - 2004-09-08 : 17:34:49
|
| Nice! Something tells me that it's not a fixed number of people. I bet you could dynamic cross tab that out and get the solution though.Michael<Yoda>Use the Search page you must. Find the answer you will.</Yoda> |
 |
|
|
jackv
Master Smack Fu Yak Hacker
2179 Posts |
Posted - 2004-09-09 : 05:11:16
|
| You are right, in practise , it is not a fixed amount of people.What do you mean by "dynamic cross tab" ? |
 |
|
|
AndrewMurphy
Master Smack Fu Yak Hacker
2916 Posts |
Posted - 2004-09-09 : 07:46:04
|
| searching gives below....amongst many others!!...http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=6216 |
 |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2004-09-09 : 07:50:07
|
| [code]Create Table MyTable (FirstName VARCHAR(20), Number_Items INT)INSERT INTO MyTable(FirstName, Number_Items) VALUES('John', 4)INSERT INTO MyTable(FirstName, Number_Items) VALUES('John', 2)INSERT INTO MyTable(FirstName, Number_Items) VALUES('Tim', 3)INSERT INTO MyTable(FirstName, Number_Items) VALUES('Tim', 1)INSERT INTO MyTable(FirstName, Number_Items) VALUES('Roger', 6)INSERT INTO MyTable(FirstName, Number_Items) VALUES('Roger', 7)INSERT INTO MyTable(FirstName, Number_Items) VALUES('Kevin', 2)INSERT INTO MyTable(FirstName, Number_Items) VALUES('Kevin', 5)Declare @str varchar(2000), @pattern varchar(200), @count intSet @str = ''Set @pattern = '(Select [name] = Number_Items From myTable Where firstName = [name]) S[count], 'Set @count = 1Select @str = @str + replace(replace(@pattern,'[name]',''''+Firstname+''''),'[count]',@count), @count = @count + 1From (Select distinct FirstName From MyTable) ASelect @str = 'Select * From ' + left(@str,len(@str)-1)Exec(@str)Drop Table MyTable[/code]Corey |
 |
|
|
|
|
|
|
|