1
SELECT 
    A.row_number, B.column_number, SUM(A.value*B.value)
FROM 
    A, B
WHERE 
    A.column_number = B.row_number
GROUP BY 
    A.row_number, B.column_number

This snippet outputs the multiplication of two matrices where each matrix is a table with the form:

matrix (row_number, column_number, value)

Could you possibly explain what happens when the code gets evaluated? If you could explain step by step it would be appreciated.

Thanks

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
rikonor
  • 409
  • 1
  • 4
  • 15

1 Answers1

2

You use the SQL SELECT SUM function to return A.row_number, B.column_number values and the matrix multiplication SUM(A.value*B.value) (for the associated row and column).

FROM tables A and B.

WHERE the number of columns in A is equal to the number of rows in B (because the product AB is defined only if this is true)

Because you have listed A.row_number, B.column_number in your SQL SELECT statement that is not encapsulated in the SQL SUM function, you must use the SQL GROUP BY clause. So you list them in the SQL GROUP BY section.

You might also want to read on the concept of matrix multiplication.

etr
  • 1,252
  • 2
  • 8
  • 15