The product of col1 and col2 directly saves values in table 2 - plsql

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>

Related

How to aggregate two columns while creating a new column that counts the aggregation?

I have this data
col1 col2
A 1
A 2
A 1
A 2
A 2
B 1
B 2
B 1
B 2
B 1
And I need to aggregate by col1 and col2, while creating col3, that counts:
col1 col2 col3
A 1 2
A 2 3
B 1 3
B 2 2
I tried aggregate function, but it requires me to use one of the existing columns to apply the function to and I couldn't add the third column.
I tried using unique across the two columns, but then I don't get col3.
We may use count
library(dplyr)
df1 %>%
count(col1, col2, name = 'col3')

How to use 2 columns in another table to filter simultaneously in dplyr

I am trying to do something of this sort. I have 2 tables that would look like this:
Table 1
col1
col2
col3
One
1
A
Three
3
C
Table 2
col1
col2
col3
One
1
A
Two
2
B
Three
3
C
Then I would run this code to filter the 2 table.
final_table <- table_2 %>%
filter(col1 %in% table_1$col1 &
col2 %in% table_1$col2)
My understanding was it looked at 'table_1$col1' and 'table_1$col2' where they matched and filtered table_2 whenever the rows have those 2 columns matching.
The result would be:
Table 2
col1
col2
col3
One
1
A
Three
3
C
I guess the question is, is this a correct way of thinking?
We may do an inner_join
library(dplyr)
inner_join(table_1, table_2)

R drop adjacent columns based on a column value

I have a df say
Df1
Col1 col2 Col3 col 4
Match 1 1 4
Match 1 1 4
Match 1 1 4
Now I want to check if any col contains string "match" if so then I need to delete col1 and its adjacent columns col2 and col2 only and still retain column 4
Any help would be appreciated
So the output should like
Df1
Col4
4
4
4

Updating a column based on conditions

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)

sqlite select all rows where some columns are in set of possible values

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

Resources