| Author |
Topic |
|
coagulance
Yak Posting Veteran
78 Posts |
Posted - 2009-12-07 : 05:24:57
|
| Table List ArticlesXvalue YValue ZValue Program#300 400 500 1300 600 200 2400 400 100 3100 200 500 4200 600 200 5Result should retrieveXvalue YValue ZValue 100 200 100200 400 200300 600 500400 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-12-07 : 08:13:42
|
quote: Originally posted by coagulance Table List ArticlesXvalue YValue ZValue Program#300 400 500 1300 600 200 2400 400 100 3100 200 500 4200 600 200 5Result should retrieveXvalue YValue ZValue 100 200 100200 400 200300 600 500400
What is the logic you applied to get this result?MadhivananFailing to plan is Planning to fail |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-12-07 : 08:46:15
|
[code]declare @sample table(x int,y int,z int)insert @sampleselect * from ( -- table with example dataselect 300 as x, 400 as y, 500 as z union allselect 300,600,200 union allselect 400,400,100 union allselect 100,200,500 union allselect 200,600,200)dt--select * from @sample-- solutionselect tablex.x, tabley.y,tablez.z from(selectrow_number() over (order by (select 1)) as rownum, x from(select distinct x from @sample)dt)tablexleft join(selectrow_number() over (order by (select 1)) as rownum, y from(select distinct y from @sample)dt)tableyon tablex.rownum = tabley.rownumleft join(selectrow_number() over (order by (select 1)) as rownum, z from(select distinct z from @sample)dt)tablezon tablez.rownum=tablex.rownum[/code]-- result:[code]x y z100 200 100200 400 200300 600 500400 NULL NULL[/code] No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
coagulance
Yak Posting Veteran
78 Posts |
Posted - 2009-12-09 : 05:06:07
|
| Can we do this without using @sample table ? |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2009-12-09 : 06:37:43
|
| The @sample table is just what webfred used to demonstrate his suggestion. Remember that we don't have a copy of your tables and data. You should replace the @sample table with the name of your own table, and you only need the bit webfred labels 'solution'.BUT, to echo madhivanan's question: What is the logic you applied to get this result?Ryan Randall - Yak of all tradesSolutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
coagulance
Yak Posting Veteran
78 Posts |
Posted - 2009-12-09 : 06:46:42
|
| Thank you for your reply , The logic is to get distict items from the three columns . Please note that there is no relevence to the row to row link here as I am using this as Menu items.IFdeclare @sample table(x int,y int,z int)insert @sampleselect * from ( -- table with example dataselect 300 as x, 400 as y, 500 as z union allselect 300,600,200 union allselect 400,400,100 union allselect 100,200,500 union allselect 200,600,200)dtNEEDS to be replacved by a query that returns x,y,z from a table , I was uanbel to figure out the changes that I need to make in the solutiuon sent by Webfred. Sorry for this dumb requset , i could not figure out. |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2009-12-09 : 07:39:31
|
| I concluded that either webfred is a mindreader or there was a similar post on this somewhere. I searched and found this [url]http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=136929[/url].Is that the only other related post or are there others (on this site or elsewhere)?Ryan Randall - Yak of all tradesSolutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2009-12-09 : 07:43:14
|
Using webfred's suggestion and removing the @sample table, try the following. You'll need to replace 'PutTheNameOfYourTableHere' for the name of your table.select tablex.x, tabley.y,tablez.z from(selectrow_number() over (order by (select 1)) as rownum, x from(select distinct x from PutTheNameOfYourTableHere)dt)tablexleft join(selectrow_number() over (order by (select 1)) as rownum, y from(select distinct y from PutTheNameOfYourTableHere)dt)tableyon tablex.rownum = tabley.rownumleft join(selectrow_number() over (order by (select 1)) as rownum, z from(select distinct z from PutTheNameOfYourTableHere)dt)tablezon tablez.rownum=tablex.rownum Ryan Randall - Yak of all tradesSolutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
coagulance
Yak Posting Veteran
78 Posts |
Posted - 2009-12-09 : 08:36:13
|
| Thank you , But the two posts I have put are different although it seems related ,Now with the solution that Ryan and WebFred havd suggested it was possible to crack the other one.Thanks Ryan , Fred |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-12-10 : 07:51:55
|
glad I could help Greetings from the "mindreader" edit: I havn't seen that other post before. No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2009-12-10 : 08:09:19
|
The logic is a bit more obvious now I look at it again - I guess I'm just too used to "thinking in rows" Still, nice work Ryan Randall - Yak of all tradesSolutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
|