Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d36608c

Browse filesBrowse files
committed
added distance calculation samples
1 parent ea3cb2c commit d36608c
Copy full SHA for d36608c

File tree

2 files changed

+85
-1
lines changed
Filter options

2 files changed

+85
-1
lines changed

‎.gitignore

Copy file name to clipboardExpand all lines: .gitignore
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,4 +400,5 @@ FodyWeavers.xsd
400400
# Custom
401401
*.local.txt
402402
*.local.sql
403-
.venv/
403+
.venv/
404+
.env

‎distance-calculations-in-tsql.md

Copy file name to clipboard
+83Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# How to calculate common vectors distances in T-SQL
2+
3+
The sample data used to show how to calculate the common vector distances in T-SQL the following
4+
5+
```sql
6+
declare @v1 nvarchar(max) = '[1,3,-5]';
7+
declare @v2 nvarchar(max) = '[4,-2,-1]';
8+
9+
drop table if exists #v1;
10+
select
11+
cast([key] as int) as [vector_value_id],
12+
cast([value] as float) as [vector_value]
13+
into
14+
#v1
15+
from
16+
openjson(@v1);
17+
18+
drop table if exists #v2;
19+
select
20+
cast([key] as int) as [vector_value_id],
21+
cast([value] as float) as [vector_value]
22+
into
23+
#v2
24+
from
25+
openjson(@v2);
26+
```
27+
28+
## Cosine Distance
29+
30+
The cosine distance can be calculated as follows:
31+
32+
```sql
33+
select
34+
1-SUM(v1.[vector_value] * v2.[vector_value]) /
35+
(
36+
SQRT(SUM(v1.[vector_value] * v1.[vector_value]))
37+
*
38+
SQRT(SUM(v2.[vector_value] * v2.[vector_value]))
39+
) as cosine_distance
40+
from
41+
#v1 as v1
42+
inner join
43+
#v2 as v2 on v1.[vector_value_id] = v2.[vector_value_id]
44+
```
45+
46+
## Dot Product
47+
48+
The dot produce can be calculated as follows:
49+
50+
```sql
51+
select
52+
SUM(v1.[vector_value] * v2.[vector_value]) as dot_product
53+
from
54+
#v1 as v1
55+
inner join
56+
#v2 as v2 on v1.[vector_value_id] = v2.[vector_value_id]
57+
```
58+
59+
## Euclidean Distance
60+
61+
The euclidean distance can be calculated as follows:
62+
63+
```sql
64+
select
65+
SQRT(SUM(POWER(v1.[vector_value] - v2.[vector_value], 2))) as euclidean_distance
66+
from
67+
#v1 as v1
68+
inner join
69+
#v2 as v2 on v1.[vector_value_id] = v2.[vector_value_id]
70+
```
71+
72+
## Manhattan Distance
73+
74+
The manhattan distance can be calculated as follows:
75+
76+
```sql
77+
select
78+
SUM(ABS(v1.[vector_value] - v2.[vector_value])) as manhattan_distance
79+
from
80+
#v1 as v1
81+
inner join
82+
#v2 as v2 on v1.[vector_value_id] = v2.[vector_value_id]
83+
```

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.