File tree Expand file tree Collapse file tree 1 file changed +62
-0
lines changed Expand file tree Collapse file tree 1 file changed +62
-0
lines changed Original file line number Diff line number Diff line change
1
+ This is an example of a LATERAL for a correlated top-k query.
2
+
3
+ Find the top 2 temperature observations for each country.
4
+
5
+ It is related to the ONCE algorithm in
6
+ [ "deep injection" discussion document] ( https://github.com/w3c/sparql-query/blob/main/discussion/Defining_the_DEEP_INJECTION_approach_for_EXISTS.md )
7
+ because it operates on each outer partial result.
8
+
9
+ ```
10
+ PREFIX : <http://example/>
11
+
12
+ :A :location "Country A" ;
13
+ :temperature 23 ;
14
+ :temperature 25 ;
15
+ :temperature 30 ;
16
+ :temperature 18 ;
17
+ .
18
+
19
+ :B :location "Country B" ;
20
+ :temperature 13 ;
21
+ :temperature 15 ;
22
+ .
23
+
24
+ :C :location "Country C" ;
25
+ :temperature 5 ;
26
+ .
27
+
28
+ :D :location "Country D" ;
29
+ .
30
+ ```
31
+
32
+ Query:
33
+
34
+ ```
35
+ PREFIX : <http://example/>
36
+
37
+ ## Two highest temperatures for each location
38
+ SELECT * {
39
+ ?x :location ?label .
40
+ LATERAL {
41
+ SELECT * {
42
+ ?x :temperature ?temp
43
+ }
44
+ ORDER BY DESC(?temp)
45
+ LIMIT 2
46
+ }
47
+ } GROUP BY ?x
48
+ ```
49
+
50
+ Results:
51
+
52
+ ```
53
+ ---------------------------
54
+ | x | label | temp |
55
+ ===========================
56
+ | :A | "Country A" | 30 |
57
+ | :A | "Country A" | 25 |
58
+ | :B | "Country B" | 15 |
59
+ | :B | "Country B" | 13 |
60
+ | :C | "Country C" | 5 |
61
+ ---------------------------
62
+ ```
You can’t perform that action at this time.
0 commit comments