| Author |
Topic |
|
jalejandro0211
Starting Member
9 Posts |
Posted - 2006-05-22 : 15:56:52
|
| Hi, I have the following problemIn a DB exist this inf:(I can use many tables... and/or many columns)-->1 2 3 45 6 7 89 a b c d e f g <--In need write a select sentence that move in "circle" all info, for example5 1 2 39 a 6 4d b 7 8e f g cto right side and left side...pls help.... (not with store procedure) |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2006-05-22 : 16:19:48
|
| Homework question?CODO ERGO SUM |
 |
|
|
jalejandro0211
Starting Member
9 Posts |
Posted - 2006-05-22 : 16:28:39
|
office Question quote: Originally posted by Michael Valentine Jones Homework question?CODO ERGO SUM
|
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2006-05-22 : 16:36:35
|
quote: Originally posted by jalejandro0211 office Question quote: Originally posted by Michael Valentine Jones Homework question?CODO ERGO SUM
OK, why are you trying to do this? What is the business problem you are trying to solve?Why do you have a requirement to not use a stored procedure?CODO ERGO SUM |
 |
|
|
jalejandro0211
Starting Member
9 Posts |
Posted - 2006-05-22 : 16:48:27
|
I need a visual aplication to transforme some data, (my ofice bussines is contability), the program objects I don't have, only the .ini file where existe the sentence that poor program execute, I need modify that sentense to return a values to work in the program later... that transform, must be with a select.... I can't modify the original program... only the external select...quote: Originally posted by Michael Valentine Jones
quote: Originally posted by jalejandro0211 office Question quote: Originally posted by Michael Valentine Jones Homework question?CODO ERGO SUM
OK, why are you trying to do this? What is the business problem you are trying to solve?Why do you have a requirement to not use a stored procedure?CODO ERGO SUM
|
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2006-05-22 : 18:21:46
|
Truely a weird question and useless problem...just the thing I like. How about this:set nocount onset ansi_warnings offif object_id('tempdb..#vals') > 0 drop table #valscreate table #vals (initPos char(1), dispVal char(1)) --display value can be anythingif object_id('dbo.fnCircleOffset') > 0 drop function dbo.fnCircleOffsetgocreate function dbo.fnCircleOffset(@initPos char(1), @offset int)returns char(1)asbegin-- declare @initPos char(1), @offset int-- select @initPos = 'a', @offset = 1 declare @AdjPos char(1) ,@outer varchar(103) ,@inner varchar(50) ,@outeroffset int ,@inneroffset int ,@idx int select @outer = '12348cgfed9512348cgfed9512348cgfed95' ,@inner = '67ba67ba67ba' ,@outeroffset = @offset%12 ,@inneroffset = @offset%4 if charindex(@initPos, @outer) > 0 begin set @idx = charindex(@initPos, @outer, 12) + @outeroffset set @AdjPos = substring(@outer, @idx, 1) end else if charindex(@initPos, @inner) > 0 begin set @idx = charindex(@initPos, @inner, 4) + @inneroffset set @AdjPos = substring(@inner, @idx, 1) end--select @idx, @AdjPos, @inneroffset return @AdjPosendgoinsert #vals (initPos, dispVal)select '1','1' unionselect '2','2' unionselect '3','3' unionselect '4','4' unionselect '5','5' unionselect '6','6' unionselect '7','7' unionselect '8','8' unionselect '9','9' unionselect 'a','a' unionselect 'b','b' unionselect 'c','c' unionselect 'd','d' unionselect 'e','e' unionselect 'f','f' unionselect 'g','g'declare @offset intset @offset = -2while @offset < 3begin print 'Offet is: ' + convert(varchar, @offset) select max(c1) c1 ,max(c2) c2 ,max(c3) c3 ,max(c4) c4 from ( select case when dbo.fnCircleOffset(initPos, @offset) in ('1','5','9','d') then dispVal end as c1 ,case when dbo.fnCircleOffset(initPos, @offset) in ('2','6','a','e') then dispVal end as c2 ,case when dbo.fnCircleOffset(initPos, @offset) in ('3','7','b','f') then dispVal end as c3 ,case when dbo.fnCircleOffset(initPos, @offset) in ('4','8','c','g') then dispVal end as c4 ,case when dbo.fnCircleOffset(initPos, @offset) in ('1','2','3','4') then 1 when dbo.fnCircleOffset(initPos, @offset) in ('5','6','7','8') then 2 when dbo.fnCircleOffset(initPos, @offset) in ('9','a','b','c') then 3 when dbo.fnCircleOffset(initPos, @offset) in ('d','e','f','g') then 4 end as r from #vals ) a group by r set @offset = @offset + 1endoutput:Offet is: -2c1 c2 c3 c4 ---- ---- ---- ---- 3 4 8 c2 b a g1 7 6 f5 9 d eOffet is: -1c1 c2 c3 c4 ---- ---- ---- ---- 2 3 4 81 7 b c5 6 a g9 d e fOffet is: 0c1 c2 c3 c4 ---- ---- ---- ---- 1 2 3 45 6 7 89 a b cd e f gOffet is: 1c1 c2 c3 c4 ---- ---- ---- ---- 5 1 2 39 a 6 4d b 7 8e f g cOffet is: 2c1 c2 c3 c4 ---- ---- ---- ---- 9 5 1 2d b a 3e 7 6 4f g c 8Be One with the OptimizerTG |
 |
|
|
MichaelP
Jedi Yak
2489 Posts |
Posted - 2006-05-22 : 18:26:21
|
| Wow, well done TG!<Yoda>Use the Search page you must. Find the answer you will. Cursors, path to the Dark Side they are. Avoid them, you must. Use Order By NewID() to get a random record you will.</Yoda>Opinions expressed in this post are not necessarily those of TeleVox Software, inc. All information is provided "AS IS" with no warranties and confers no rights. |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2006-05-22 : 22:25:26
|
Nice work TG.Now you just need to extend it to be able to rotate a matrix of arbitrary width and length. Just kidding, but I think that is what the poster actually wanted.Once you figure that out, maybe you could give some thought to rotating values through three (or more) dimensions. CODO ERGO SUM |
 |
|
|
jalejandro0211
Starting Member
9 Posts |
Posted - 2006-05-23 : 08:33:02
|
Tks TG 4 ur help, excelent work, I will try... |
 |
|
|
jalejandro0211
Starting Member
9 Posts |
Posted - 2006-05-23 : 09:05:56
|
TG the values are not constant, they change in every table...Today can be 1 2 3 4 5 6 7 8 ...Tomorrow... a b c d 1 3 g p 4 r t 4...etc...quote: Originally posted by jalejandro0211 Tks TG 4 ur help, excelent work, I will try...
|
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2006-05-23 : 09:35:33
|
quote: Originally posted by jalejandro0211 TG the values are not constant, they change in every table...Today can be 1 2 3 4 5 6 7 8 ...Tomorrow... a b c d 1 3 g p 4 r t 4...etc...
[/quote]If the size is always 4X4 then the above could work. The InitPos column needs to be 1-g, but the DispVal can be anything.Be One with the OptimizerTG |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-05-23 : 12:31:56
|
Hi all,Just like TG, I can't resist these pointless problems Here's my effort... I too haven't yet extended it to rotate a matrix of arbitrary width and length. Maybe later Note that, since the data is in a de-normalised format, I thought the easiest method was to normalise, do the transposition, and then re-de-normalise. I've put it all together in one update to make it harder to understand  --datadeclare @t table (r int identity(1, 1), a char(1), b char(1), c char(1), d char(1))insert @t select '1', '2', '3', '4'union all select '5', '6', '7', '8'union all select '9', 'a', 'b', 'c'union all select 'd', 'e', 'f', 'g'--calculationupdate @t set a = b.a, b = b.b, c = b.c, d = b.dfrom @t a inner join (select a.r, max(case when a.c = 1 then c.v else null end) as a, max(case when a.c = 2 then c.v else null end) as b, max(case when a.c = 3 then c.v else null end) as c, max(case when a.c = 4 then c.v else null end) as dfrom ( select r, 1 as c, a as v from @t union all select r, 2, b from @t union all select r, 3, c from @t union all select r, 4, d from @t) a--rem out one of the following 2 rows to rotate clockwise or anti-clockwise inner join ( select 1 as r, 1 as c, 1 as r1, 2 as c1-- inner join ( select 1 as r1, 1 as c1, 1 as r, 2 as c union all select 1, 2, 1, 3 union all select 1, 3, 1, 4 union all select 1, 4, 2, 4 union all select 2, 1, 1, 1 union all select 2, 2, 2, 3 union all select 2, 3, 3, 3 union all select 2, 4, 3, 4 union all select 3, 1, 2, 1 union all select 3, 2, 2, 2 union all select 3, 3, 3, 2 union all select 3, 4, 4, 4 union all select 4, 1, 3, 1 union all select 4, 2, 4, 1 union all select 4, 3, 4, 2 union all select 4, 4, 4, 3) b on a.r = b.r1 and a.c = b.c1 inner join ( select r, 1 as c, a as v from @t union all select r, 2, b from @t union all select r, 3, c from @t union all select r, 4, d from @t) c on b.r = c.r and b.c = c.cgroup by a.r) b on a.r = b.rselect a, b, c, d from @t order by r--resultsa b c d ---- ---- ---- ---- 5 1 2 39 a 6 4d b 7 8e f g cora b c d ---- ---- ---- ---- 2 3 4 81 7 b c5 6 a g9 d e f Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
jalejandro0211
Starting Member
9 Posts |
Posted - 2006-05-24 : 10:09:38
|
RyanRandall u a the Best!!!! Tks 4 ur help |
 |
|
|
|
|
|