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.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Matriz SQL

Author  Topic 

jalejandro0211
Starting Member

9 Posts

Posted - 2006-05-22 : 15:56:52
Hi, I have the following problem

In a DB exist this inf:
(I can use many tables... and/or many columns)

-->
1 2 3 4
5 6 7 8
9 a b c
d e f g
<--

In need write a select sentence that move in "circle" all info, for example

5 1 2 3
9 a 6 4
d b 7 8
e f g c

to 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
Go to Top of Page

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

Go to Top of Page

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
Go to Top of Page

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

Go to Top of Page

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 on
set ansi_warnings off

if object_id('tempdb..#vals') > 0
drop table #vals
create table #vals (initPos char(1), dispVal char(1)) --display value can be anything


if object_id('dbo.fnCircleOffset') > 0
drop function dbo.fnCircleOffset
go
create function dbo.fnCircleOffset(@initPos char(1), @offset int)
returns char(1)
as
begin
-- 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 @AdjPos
end
go

insert #vals (initPos, dispVal)
select '1','1' union
select '2','2' union
select '3','3' union
select '4','4' union
select '5','5' union
select '6','6' union
select '7','7' union
select '8','8' union
select '9','9' union
select 'a','a' union
select 'b','b' union
select 'c','c' union
select 'd','d' union
select 'e','e' union
select 'f','f' union
select 'g','g'



declare @offset int
set @offset = -2
while @offset < 3
begin
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 + 1
end


output:
Offet is: -2
c1 c2 c3 c4
---- ---- ---- ----
3 4 8 c
2 b a g
1 7 6 f
5 9 d e

Offet is: -1
c1 c2 c3 c4
---- ---- ---- ----
2 3 4 8
1 7 b c
5 6 a g
9 d e f

Offet is: 0
c1 c2 c3 c4
---- ---- ---- ----
1 2 3 4
5 6 7 8
9 a b c
d e f g

Offet is: 1
c1 c2 c3 c4
---- ---- ---- ----
5 1 2 3
9 a 6 4
d b 7 8
e f g c

Offet is: 2
c1 c2 c3 c4
---- ---- ---- ----
9 5 1 2
d b a 3
e 7 6 4
f g c 8


Be One with the Optimizer
TG
Go to Top of Page

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.
Go to Top of Page

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
Go to Top of Page

jalejandro0211
Starting Member

9 Posts

Posted - 2006-05-23 : 08:33:02
Tks TG 4 ur help, excelent work, I will try...



Go to Top of Page

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...





Go to Top of Page

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 Optimizer
TG
Go to Top of Page

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

--data
declare @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'

--calculation
update @t set a = b.a, b = b.b, c = b.c, d = b.d
from @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 d
from ( 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.c
group by a.r) b on a.r = b.r

select a, b, c, d from @t order by r

--results
a b c d
---- ---- ---- ----
5 1 2 3
9 a 6 4
d b 7 8
e f g c

or

a b c d
---- ---- ---- ----
2 3 4 8
1 7 b c
5 6 a g
9 d e f


Ryan Randall
www.monsoonmalabar.com London-based IT consultancy

Solutions are easy. Understanding the problem, now, that's the hard part.
Go to Top of Page

jalejandro0211
Starting Member

9 Posts

Posted - 2006-05-24 : 10:09:38
RyanRandall u a the Best!!!!

Tks 4 ur help
Go to Top of Page
   

- Advertisement -