mirror of
https://github.com/Microsoft/sql-server-samples.git
synced 2025-12-08 14:58:54 +00:00
updated documentation
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
# Implementing Transitive Closure Clustering in SQL Server using CLR UDF
|
||||
SQL Database don't have built-in support for transitive closure clustering, so the only workaround is to implement this algorithm in .Net framework and expose it as T-SQL function.
|
||||
A discussion on the problem, the algorithm and a pure T-SQL based solution can be found here:
|
||||
- [Transitive Closure Clustering with SQL Server, UDA and JSON](https://medium.com/@mauridb/transitive-closure-clustering-with-sql-server-uda-and-json-dade18953fd2)
|
||||
- [T-SQL Puzzle Challenge Grouping Connected Items](http://www.itprotoday.com/microsoft-sql-server/t-sql-puzzle-challenge-grouping-connected-items)
|
||||
|
||||
This code sample demonstrates how to create CLR User-Defined aggregate that implements clustering.
|
||||
|
||||
### Contents
|
||||
@@ -17,7 +21,7 @@ This code sample demonstrates how to create CLR User-Defined aggregate that impl
|
||||
2. **Key features:**
|
||||
- CLR, JSON
|
||||
3. **Programming Language:** .NET C#
|
||||
4. **Author:** Davide Mauri, Jovan Popovic [jovanpop-msft]
|
||||
4. **Author:** [Davide Mauri](https://github.com/yorek), Jovan Popovic [jovanpop-msft]
|
||||
|
||||
<a name=build-functions></a>
|
||||
|
||||
@@ -65,8 +69,9 @@ Once you create the assembly and expose the aggregate, you can use it to cluster
|
||||
declare @edges table(n1 int, n2 int);
|
||||
|
||||
insert into @edges
|
||||
values (1,2),(2,3),(3,4),(4,5),(2,21),(2,22),
|
||||
(7,8),(8,9),(9,10);
|
||||
values
|
||||
(1,2),(2,3),(3,4),(4,5),(2,21),(2,22),
|
||||
(7,8),(8,9),(9,10);
|
||||
|
||||
select TC.CLUSTERING(n1,n2)
|
||||
from @edges;
|
||||
@@ -74,21 +79,21 @@ from @edges;
|
||||
The result will be JSON document that groups the numbers that belong to the same cluster.
|
||||
```javascript
|
||||
{
|
||||
"0":[1,2,3,4,5,21,22],
|
||||
"1":[7,8,9,10]
|
||||
"0":[1,2,3,4,5,21,22],
|
||||
"1":[7,8,9,10]
|
||||
}
|
||||
```
|
||||
You can transform this JSON document into relational formatusing **OPENJSON** function:
|
||||
```
|
||||
select cluster = [key], elements = value
|
||||
from openjson(
|
||||
(select TC.CLUSTERING(n1,n2) from @edges)
|
||||
(select TC.CLUSTERING(n1,n2) from @edges)
|
||||
);
|
||||
```
|
||||
The result of this query is:
|
||||
|
||||
|cluster|elements|
|
||||
|----|---|
|
||||
|---|---|
|
||||
|0|[1,2,3,4,5,21,22]|
|
||||
|1|[7,8,9,10]|
|
||||
|
||||
|
||||
@@ -14,11 +14,9 @@ namespace TransitiveClosure
|
||||
public class Group: IEnumerable<int>
|
||||
{
|
||||
private int? _groupRoot = null;
|
||||
|
||||
|
||||
private Dictionary<int, bool> _group = new Dictionary<int, bool>();
|
||||
|
||||
public Dictionary<int, bool>.KeyCollection Elements => _group.Keys;
|
||||
|
||||
public int Count => _group.Keys.Count;
|
||||
|
||||
public bool ContainsElement(int element)
|
||||
@@ -89,13 +87,12 @@ namespace TransitiveClosure
|
||||
public class GroupSet: IEnumerable<Group>
|
||||
{
|
||||
private List<Group> _groupSet = new List<Group>();
|
||||
// This keeps about the distinct numbers and in which group they are
|
||||
private Dictionary<int, Group> _numbers = new Dictionary<int, Group>();
|
||||
private int _merges = 0;
|
||||
|
||||
public int Groups => _groupSet.Count;
|
||||
|
||||
public int Numbers => _numbers.Count;
|
||||
|
||||
public int Merges => _merges;
|
||||
|
||||
public void Add(Group group)
|
||||
@@ -212,9 +209,7 @@ namespace TransitiveClosure
|
||||
private GroupSet _groupSet;
|
||||
|
||||
public int Groups => _groupSet.Groups;
|
||||
|
||||
public int Numbers => _groupSet.Numbers;
|
||||
|
||||
public int Merges => _groupSet.Merges;
|
||||
|
||||
public void Init()
|
||||
|
||||
Reference in New Issue
Block a user