Skip to content

Instantly share code, notes, and snippets.

@Scriddie
Last active May 2, 2021 18:29
Show Gist options
  • Save Scriddie/522dae62c66ca4b9ff37555efd8775a9 to your computer and use it in GitHub Desktop.
Save Scriddie/522dae62c66ca4b9ff37555efd8775a9 to your computer and use it in GitHub Desktop.
determinant
# Calculate determinant of X * X
select @determinant := sum(product * product_sign)
from (
select (xtx_1.value * xtx_2.value * xtx_3.value)
product
# we need to weight the row_numbers according to the inverse of their natural order
# to estimate degree of permutation
, power(-1, dense_rank() over(
order by 3 * xtx_1.row_num + 2 * xtx_2.row_num + 1 * xtx_3.row_num) - 1)
product_sign
from XtX xtx_1
join XtX xtx_2
on xtx_1.row_num != xtx_2.row_num
join XtX xtx_3
on xtx_1.row_num != xtx_3.row_num
and xtx_2.row_num != xtx_3.row_num
where xtx_1.col_num = 1
and xtx_2.col_num = 2
and xtx_3.col_num = 3
order by 3 * xtx_1.row_num + 2 * xtx_2.row_num + 1 * xtx_1.row_num
) determinant_components;
@joeccollins
Copy link

select @Determinant := sum(product * product_sign)


This line does not work in MS SSMS --- is there another way to phrase this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment