SQL Matrix Manipulations Two random 3x3 (N = 3) matrices have been provided in t
ID: 3790834 • Letter: S
Question
SQL Matrix Manipulations
Two random 3x3 (N = 3) matrices have been provided in tables A and B, having the following schema:
i INT: Row index
j INT: Column index
val INT: Cell value
We want the elementwise multiply of matrices A and B. That is, (A·B)ij = AijBij for all i, j. Write a single SQL query that performs this operation (in the same format as Aoutput tuples should be of format (i, j, val) where i is row, j is column, and the output is ordered by row then column index).
For example:
select*from A;
0|0|7
0|1|5
0|2|8
1|0|10
1|1|7
1|2|7
2|0|2
2|1|0
2|2|5
select* from B;
0|0|9
0|1|6
0|2|10
1|0|7
1|1|6
1|2|9
2|0|1
2|1|1
2|2|7
should result :
i j val
0 0 63
0 1 30
0 2 80
1 0 70
1 1 42
1 2 63
2 0 2
2 1 0
2 2 35
Explanation / Answer
CREATE TABLE a ( row_num INT, col_num INT, value INT, PRIMARY KEY(row_num, col_num) ); CREATE TABLE b ( row_num INT, col_num INT, value INT, PRIMARY KEY(row_num, col_num) );
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.