In Oracle, repeatedly assign values from a set to column - oracle

I have a table in Oracle that has a column that I would like to assign a value to from a set of possible values. I like to assign the values in order of the set, repeatedly, for the entire table.
For example:
If the set of values is {1, 2, 3}. I'd like to assign the values in this pattern until the last row is reached:
rowNum someCol valueCol
1 this 1
2 is 2
3 some 3
4 other 1
5 column 2
6 in 3
7 the 1
8 table 2
I can't figure out how to do this with a traditional update statement. Anone that could help with this problem?

Use Modulo to achieve desire result
UPDATE TableName
SET valueCol= CASE WHEN rowNum % 3 == 1 then 1
WHEN rowNum % 3 == 2 then 2
WHEN rowNum % 3 == 0 then 3
END

update tablename
set valuecol = case mod(rownum, 3) when 0 then 3 else mod(rownum, 3) end
;

Related

execute immediate update doesn't put value PL SQL

Im trying to update values in PL SQL. The problem is that my dbms_output.put_line is working fine, everything is writen good, but execute immediate doesn't work. The funny thing is, that it works once (or even two) but now it doesn't work. I don't know how to resolve this problem.
Program that i write is inserting some random values into table that is created from values that are input veriables. code can be little to long to be throwed here, i put the most important part of code
for looper in 1..y loop
execute immediate 'insert into GAME ' || s_string || ' values '
|| rowGenerator(v_low=>1, v_high=>6, v_x=>x);
end loop;
start_x_point:= round (dbms_random.value( high=>x, low=>1));
start_y_point:= round (dbms_random.value( high=>y, low=>1));
dbms_output.put_line(start_x_point || ' ' || start_y_point);
s_temp:= ' set x' || start_x_point || ' = 0 where ROWNUM = '|| start_y_point;
dbms_output.put_line(s_temp);
execute immediate 'update GAME ' || s_temp;
s_string conteining bracket with columns,
rowGenerator insert random values into table,
start_x_point and start_y_point is the place where i want to put 0 value
and the problem is that it doesn't works. I don't know other way to do that.
that is how my table looks like after puting random values, its a matrix with values
x1 x2 x3 x4 x5 x6 x7 x8
1 2 5 3 2 4 1 1 3
2 2 3 2 5 4 5 3 3
3 4 3 4 2 4 2 6 3
4 2 1 1 4 3 2 4 1
5 1 5 2 5 3 3 5 4
6 1 5 5 1 1 3 5 3
7 5 3 3 4 4 3 1 3
8 5 4 5 6 3 4 3 2
It appears that your problem is ROWNUM = .... It'll work only when START_Y_POINT = 1, and won't work when it is not.
What to do? Don't use ROWNUM; UPDATE values that depend on something else (some ID value, perhaps?).
Thanks to #Littlefoot i got answer!
i just need to put something like that
select * from (select * from game
where rownum <=start_y_point
order by rownum desc)
where rownum=1;
and i getting the row that i want!

SAS grouping algorithm

I have the following mock up table
#n a b group
1 1 1 1
2 1 2 1
3 2 2 1
4 2 3 1
5 3 4 2
6 3 5 2
7 4 5 2
I am using SAS for this problem. In column group, the rows that are interconnected through a and b are grouped. I will try to explain why these rows are in the same group
row 1 to 2 are in group 2 since they both have a = 1
row 3 is in group 2 since b = 2 in row 2 and 3 and row 2 is in group 1
row 3 and 4 are in group 1 since a = 2 in both rows and row 3 is in group 1
The overall logic is that if a row x contains the same value of a or b as row y, row x also belongs to the same group as y is a part of.
Following the same logic, row 5,6 and 7 are in group 2.
Is there any way to make an algorithm to find these groups?
Case I:
Grouping defined as to be item linkage within contiguous rows.
Use the LAG function to examine both variables prior values. Increase the group value if both have changed. For example
group + ( a ne lag(a) and b ne lag(b) );
Case II:
Grouping determined from pair item slot value linkages over all data.
From grouping pairs by either key
General statement of problem:
-----------------------------
Given: P = p{i} = (p{i,1},p{i,2}), a set of pairs (key1, key2).
Find: The distinct groups, G = g{x}, of P,
such that each pair p in a group g has this property:
key1 matches key1 of any other pair in g.
-or-
key2 matches key2 of any other pair in g.
Demonstrates
… an iterative way using hashes.
Two hashes maintain the groupId assigned to each key value.
Two additional hashes are used to maintain group mapping paths.
When the data can be passed without causing a mapping, then the groups have
been fully determined.
A final pass is done, at which point the groupIds are assigned to each
pair and the data is output to a table.

Oracle - Assign count value for a column based on another column in select query

Consider, I have the following in a select query:
ID Flag
5 Y
5 Y
5 N
6 Y
6 Y
6 Y
6 N
I should be adding a new column count in the same select which counts the number of 'Y' records for the ID and assigns it to all. (Eg: ID=5 has 3 records. All of them should be assigned the count value as '2').
Output required in select query:
ID Flag count
5 Y 2
5 Y 2
5 N 2
6 Y 3
6 Y 3
6 Y 3
6 N 3
Use a window function:
select id,
flag,
count(case when flag = 'Y' then 1 end) over (partition by id) as "count"
from the_table
order by id;
The case expression will return null for flags with N and thus they will be ignored by the count() function

How to sum incrementaly in Oracle 8?

I need to do an incremental sum in Oracle.
My situation is the following:
RecordID Value
1 1
2 2
3 5
4 10
And I need to get something like this:
RecordID Sum_incremental
1 (1)
2 (1 + 2)
3 (1 + 2 + 5)
4 (1 + 2 + 5 + 10)
The clues: self join and group by.
The solution:
select a.recordid, sum(b.value) sum_incremental from mytable a, mytable b
where b.recordid <= a.recordid group by a.recordid
select recordid,
sum(value) over (order by recordid)
from some_data

How do I compare rows in an Oracle Table?

I have a table that's like this
rank continuationofrow
1 row
2 row
3 row
4 row
4 row
4 row
I'm trying to identify the previous rows rank number within an Oracle statement. Any help is greatly appreciated. I've searched the internet and haven't found much.
You must have another column that establishes the order of the rows with the same rank, otherwise the concept of "previous row" is meaningless. Let's suppose you do:
seq rank continuationofrow
1 1 row
2 2 row
3 3 row
4 4 row
5 4 row
6 4 row
No you can use an analytic function:
select seq, rank, continuationofrow, lag(rank) over (order by seq) as prev_rank
from mytable;
seq rank continuationofrow prev_rank
1 1 row
2 2 row 1
3 3 row 2
4 4 row 3
5 4 row 4
6 4 row 4
select
...
lag(rank, 1) over (order by ordering-columns)
from
..

Resources