Skip to content

Instantly share code, notes, and snippets.

@craigmbooth
Created May 18, 2013 16:13
Show Gist options
  • Select an option

  • Save craigmbooth/5604946 to your computer and use it in GitHub Desktop.

Select an option

Save craigmbooth/5604946 to your computer and use it in GitHub Desktop.
Sparse matrix multiply in SQL
-- Join columns to rows, group by rows and columns, then filter to get the cell you want.
SELECT A.row_num, B.col_num, SUM(A.value*B.value)
FROM A,B
WHERE A.col_num=B.row_num
GROUP BY A.row_num, B.col_num;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment