-
Notifications
You must be signed in to change notification settings - Fork 9.1k
Expand file tree
/
Copy pathdemo2-using-the-match-clause.sql
More file actions
77 lines (67 loc) · 1.82 KB
/
demo2-using-the-match-clause.sql
File metadata and controls
77 lines (67 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
------------------------------------------------------------------------
-- Event: SQL Saturday #675 Parma, November 18 2017 -
-- http://www.sqlsaturday.com/675/EventHome.aspx -
-- Session: SQL Server 2017 Graph Database -
-- Demo: Demo1: Using the MATCH clause -
-- Author: Sergio Govoni -
-- Notes: -- -
------------------------------------------------------------------------
USE [WideWorldImporters];
GO
SELECT * FROM Edges.Friends;
-- List of all guys that speak finnish with friends
-- Pattern: Node > Relationship > Node
SELECT
P1.FullName
,P1.[Language]
,Friends_Number = COUNT(*)
FROM
Nodes.Person AS P1
,Edges.Friends AS Friends
,Nodes.Person AS P2
WHERE
MATCH(P1-(Friends)->P2)
AND (P1.[Language] = 'Finnish')
GROUP BY
P1.FullName, P1.[Language]
ORDER BY
Friends_Number DESC, P1.[Language];
GO
-- List of the top 5 people who have friends that speak Greek
-- in the first and second connections
SELECT
TOP 5
P1.FullName
,P1.[Language]
,GreekFriends = COUNT(*)
FROM
Nodes.Person AS P1
,Edges.Friends AS F1
,Nodes.Person AS P2
,Edges.Friends AS F2
,Nodes.Person AS P3
WHERE
MATCH(P1-(F1)-> P2-(F2)-> P3)
AND ((P2.[Language] = 'Greek') OR (P3.[Language] = 'Greek'))
GROUP BY
P1.FullName, P1.[Language]
ORDER BY
GreekFriends DESC, P1.[Language];
GO
-- People who have common friends that speak Croatian
SELECT
P1.FullName
,P2.FullName
,P2.[Language]
--,P3.FullName
FROM
Nodes.Person AS P1
,Edges.Friends AS F1
,Nodes.Person AS P2
,Edges.Friends AS F2
,Nodes.Person AS P3
WHERE
MATCH(P1-(F1)-> P2 <-(F2)-P3)
AND (P2.[Language] = 'Croatian')
AND (P1.$node_id <> P3.$node_id);
GO