-
Notifications
You must be signed in to change notification settings - Fork 0
/
self_join.clj
175 lines (156 loc) · 6.3 KB
/
self_join.clj
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
(ns seaquell.zoo.self-join
(:refer-clojure
:exclude [alter distinct drop group-by into set update partition-by when])
(:require [seaquell.core :refer :all]))
;; The following queries are solutions (as of 7/21/2020) to the
;; tutorial at http://sqlzoo.net/wiki/Self_join
;; Each query is named for the question it answers.
;;
;; To try them out, open a repl and issue the commands below.
;; Submitting the resulting SQL into the tutorial should produce
;; correct answers.
(comment
(use 'seaquell.core)
(use 'seaquell.zoo.self-join)
(println (select$ q1)))
(def q1 (select [[count :*]] (from :stops)))
(def q2 (select :id (from :stops) (where {:name "Craiglockhart"})))
(def q3
(select [:id :name]
(from :stops :route)
(where {:id :stop, :company "LRT", :num "4"})))
(def q3-b
(select [:id :name]
(from :route (join :stops (on {:id :stop})))
(where {:company "LRT", :num "4"})))
(def q4 (select [:company :num [count :*]]
(from :route)
(where '(or [= stop 149] [= stop 53]))
(group-by :company :num)
(having [= [count :*] 2])))
(def q5 (select [:a.company :a.num :a.stop :b.stop]
(from :route (as :a)
(join :route (as :b) (on {:a.company :b.company
:a.num :b.num })))
(where {:a.stop 53, :b.stop 149})))
(def q6
(select
[:a.company :a.num :stopa.name :stopb.name]
(from :route (as :a)
(join :route (as :b) (on {:a.company :b.company :a.num :b.num }))
(join :stops (as :stopa) (on {:a.stop :stopa.id}))
(join :stops (as :stopb) (on {:b.stop :stopb.id})))
(where {:stopa.name "Craiglockhart", :stopb.name "London Road"})))
(def q7
(select-distinct
[:R1.company :R1.num]
(from :route (as :R1)
(comma-join :route (as :R2)))
(where {:R1.num :R2.num
:R1.company :R2.company
:R1.stop 115
:R2.stop 137})))
(def q7-b
(select-distinct
[:R1.company :R1.num]
(from :route (as :R1)
(join :route (as :R2) (on {:R1.num :R2.num
:R1.company :R2.company })))
(where {:R1.stop 115
:R2.stop 137})))
(def q8
(select [:R1.company :R1.num]
(from :route :as :R1
(comma-join :route :as :R2)
(comma-join :stops :as :S1)
(comma-join :stops :as :S2))
(where {:R1.num :R2.num, :R1.company :R2.company
:R1.stop :S1.id, :R2.stop :S2.id
:S1.name "Craiglockhart"
:S2.name "Tollcross"})))
(def q8-b
(select [:R1.company :R1.num]
(from :route :as :R1
(join :route :as :R2 (on {:R1.num :R2.num
:R1.company :R2.company}))
(join :stops :as :S1 (on {:R1.stop :S1.id}))
(join :stops :as :S2 (on {:R2.stop :S2.id})))
(where {:S1.name "Craiglockhart"
:S2.name "Tollcross"})))
(def q9
(select [:S2.name :R2.company :R2.num]
(from :stops :as :S1
(comma-join :stops :as :S2)
(comma-join :route :as :R1)
(comma-join :route :as :R2))
(where {:S1.name "Craiglockhart"
:S1.id :R1.stop
:R1.company :R2.company, :R1.num :R2.num
:R2.stop :S2.id})))
(def q9-b
(select [:S2.name :R2.company :R2.num]
(from :route :as :R1
(join :route :as :R2 (on {:R1.company :R2.company
:R1.num :R2.num }))
(join :stops :as :S1 (on {:S1.id :R1.stop }))
(join :stops :as :S2 (on {:S2.id :R2.stop})))
(where {:S1.name "Craiglockhart"})))
(def q10
"NOTE: for this query, `q10-b`, and `q10-c` use the MySQL engine and set
the following option: `SET SQL_BIG_SELECTS=1;`. This was not necessary in
earlier versions of SQLZoo."
(select
[:ra.num :ra.company :sb.name :rd.num :rd.company]
(from :route (as :ra)
(join :stops (as :sa) (on {:ra.stop :sa.id}))
(join :route (as :rb) (using :num :company))
(join :stops (as :sb) (on {:rb.stop :sb.id}))
(join :route (as :rc))
(join :stops (as :sc) (on {:rc.stop :sc.id}))
(join :route (as :rd) (on {:rc.num :rd.num, :rc.company :rd.company}))
(join :stops (as :sd) (on {:rd.stop :sd.id})))
(where {:sa.name "Craiglockhart", :sd.name "Lochend", :sb.name :sc.name})
(order-by :ra.company :ra.num :sb.name :rd.company :rd.num)))
(def q10-b
(select
[:q1.num :q1.company :q1.name :q2.num :q2.company]
(from
(select
[:x.num :x.company :b.name]
(from :route (as :x)
(join :stops (as :a) (on {:x.stop :a.id}))
(join :route (as :y) (on {:x.num :y.num, :x.company :y.company}))
(join :stops (as :b) (on {:y.stop :b.id})))
(where {:a.name "Craiglockhart"}))
(as :q1)
(join
(select
[:x.num :x.company :a.name]
(from :route (as :x)
(join :stops (as :a) (on {:x.stop :a.id}))
(join :route (as :y) (on {:x.num :y.num, :x.company :y.company}))
(join :stops (as :b) (on {:y.stop :b.id})))
(where {:b.name "Lochend"}))
(as :q2)
(on {:q1.name :q2.name})))
(order-by :q1.company :q1.num :q1.name :q2.company :q2.num)))
(def q10-c
(with
:dest[:company :num :pos :name] :as
(select [:company :num :pos :name]
(from :route (join :stops (on {:stop :id})))),
:leg[:company :num :p1 :s1 :p2 :s2] :as
(select
[:a.company :a.num :a.pos :a.name :b.pos :b.name]
(from :dest (as :a)
(join :dest (as :b) (on {:a.company :b.company
:a.num :b.num
:a.pos [not= :b.pos]})))),
:leg1[:company :num :p1 :s1 :p2 :s2] :as
(select :* (from :leg) (where {:s1 "Craiglockhart"}))
:leg2[:company :num :p1 :s1 :p2 :s2] :as
(select :* (from :leg) (where {:s2 "Lochend"}))
(select
[:leg1.num :leg1.company :leg1.s2 (as :name) :leg2.num :leg2.company]
(from :leg1 (join :leg2 (on {:leg1.s2 :leg2.s1})))
(order-by :leg1.company :leg1.num :name :leg2.company :leg2.num))))