I have a table like:
ID ID2 Name
1 1
2 1
3 2
4 2
5 2
6 3
7 3
8 3
I want to Update the Name column with Values like Name1, name 2 and so on.
This will be based on condition if there are two similar values in ID2 column, for example, the first two rows, then the Name column to be updated with values Name1 and Name2 respectively.Next rows would be Name1, Name2, Name3 respectively and so on. Can someone help me with the logic for this?
ROW_NUMBER with a partition on the ID2 column, ordered by the ID column, should generate the sequences you want. Try this update query:
UPDATE yourTable t1
SET Name = (SELECT 'Name' || t.rn FROM
(
SELECT t2.ID, t2.ID2,
ROW_NUMBER() OVER (PARTITION BY t2.ID2 ORDER BY t2.ID) rn
FROM yourTable t2
WHERE t1.ID = t2.ID AND t1.ID2 = t2.ID2
) t)
Related
table1 has 2 columns and i want to store the product values of column 1 and column 2 values from table 1 to table 2. (the product of col 1 and col 2 directly saves values in table 2)
I'M THINKING TO ADD ONE MORE COLUMN IN TABLE 1 AND PASTE THE PRODUCT VALUES IN COL3 AND THEN PUT THE COLUMN VALUES(CLONE) IN TABLE2
Do you need table 2 ? With virtual columns you can just do the calculation automatically in the table1.
koen>CREATE TABLE some_test_table (
2 col1 NUMBER,
3 col2 NUMBER,
4 col3 NUMBER GENERATED ALWAYS AS ( col1 * col2 ) VIRTUAL
5* );
Table SOME_TEST_TABLE created.
koen>INSERT INTO some_test_table(col1, col2) VALUES (5,9);
1 row inserted.
koen>select * from some_test_table;
COL1 COL2 COL3
_______ _______ _______
5 9 45
koen>
I have two tables: Table1 and Table2
x
Value
1
35904
2
40500
x
Value
1
25535
2
23235
How would I multiply the value of x = 1 in Table1 by the value of x = 2 in Table2?
SQLite UPDATE Statement
I have two tables:
Table1
ID Num
1
2
3
4
5
Table2
ID
1
1
2
2
2
3
3
4
4
4
5
I need to UPDATE the Num field in Table1 with occurences of the ID field in Table2 i.e. based on previous:
Table1
ID Num
1 2
2 3
3 2
4 3
5 1
If i run this SQLite statement:
SELECT COUNT(t2.ID) FROM Table1 t1,Table2 t2 WHERE t1.ID=t2.ID GROUP BY t2.ID;
i have the correct table but when i try to UPDATE with that statement:
UPDATE Table1
SET Num=(SELECT COUNT(t2.ID) FROM Table1 t1,Table2 t2 WHERE t1.ID=t2.ID GROUP BY t2.ID);
i have nonsense output.Any ideas?
You must use a correlated subquery to correlate the value returned by the subquery with the current row in the outer query.
This means that you must not use Table1 again in the subquery, but instead refer to the outer table (with the actual name; the UPDATEd table does not support an alias):
UPDATE Table1
SET Num = (SELECT COUNT(t2.ID)
FROM Table2 t2
WHERE Table1.ID=t2.ID
GROUP BY t2.ID);
col0 col1 col2 A B C
0 0 1 1 2 3
0 1 1 1 2 3
0 0 1 1 2 3
0 2 0 1 2 3
0 1 1 1 2 3
Hello, I have a table where some of the columns are col0, col1, col2. First of all I want to find each combination which meet some condition. Let's say for simplicity to find each triple which is more than once in database (group by and having is required in my more complicated real case):
SELECT col0,col1,col2 FROM table GROUP BY col0,col1,col2 HAVING COUNT(*) > 1
Gives me something like:
col0 col1 col2
0 0 1
0 1 1
Now I want to select (actually delete) all such rows which contain one of those col0,col1,col2 combinations.
So something like:
SELECT * FROM table WHERE (col0,col1,col2) IN (...select above...)
But that gives me an error, I guess just one column is allowed for WHERE clause in SQLite. So probably some kind of join has to be used but I struggle with proper syntax and kind of join to be used. How to proceed? Thank you.
The easiest way to select rows with certain combinations to values is to use a join:
SELECT MyTable.*
FROM MyTable
JOIN (SELECT col0, col1, col2
FROM MyTable
GROUP BY col0, col1, col2
HAVING COUNT(*) > 1)
USING (col1, col2, col3)
Joins do not work with DELETE, so you have to use the primary key (or a candidate primary key) in a separate step:
DELETE FROM MyTable
WHERE rowid IN (SELECT MyTable.rowid
FROM MyTable
JOIN (SELECT col0, col1, col2
FROM MyTable
GROUP BY col0, col1, col2
HAVING COUNT(*) > 1)
USING (col1, col2, col3))
The goal is to combine Time_a within 10 min interval that has same ID.
And group the ID.
ID Time_a -> ID
------------ ----------
1 12:10:00 1
1 12:15:00 2
1 12:20:00 2
2 12:25:00
2 12:35:00
2 02:00:00
It became two '2' because time interval between row5 and row6 is more than 10 min.
I was able to combine within 10-min difference, but it doesn't distinguish ID.
select ID
from(
select id, Time_a, min(time) OVER (order by id, time rows between 1 preceding and 1 preceding) prev_t_stamp
from dual
)
where abs(Time_a-prev_t_stamp)>10/1440
If time_a is date then it works as follow. If it is a timestamp you need to cast it to a date otherwise it will not quite work. Here is my test script
drop table test;
create table test (id number, time_a date);
insert into test values (1, to_date('12:10','HH24:MI'));
insert into test values (1, to_date('12:15','HH24:MI'));
insert into test values (1, to_date('12:20','HH24:MI'));
insert into test values (2, to_date('12:25','HH24:MI'));
insert into test values (2, to_date('12:35','HH24:MI'));
insert into test values (2, to_date('14:00','HH24:MI'));
select id from (
select distinct id, case from (
select ID,case when abs(Time_a-prev_t_stamp)>10/1440 then '>' end case
from(
select id, Time_a, min(time_a) OVER (order by id, time_a rows between 1 preceding and 1 preceding) prev_t_stamp
from test
)
)
) order by 1
This results into:
table TEST dropped.
table TEST created.
1 rows inserted.
1 rows inserted.
1 rows inserted.
1 rows inserted.
1 rows inserted.
1 rows inserted.
ID
--
1
2
2