-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2.1.sql
More file actions
200 lines (185 loc) · 6 KB
/
2.1.sql
File metadata and controls
200 lines (185 loc) · 6 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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
-- Adoptions matched with themselves
-- Show adopters who adopted 2 animals in 1 day.
SELECT A1.Adopter_Email,
A1.Adoption_Date,
A1.Name AS First_Animal_Name,
A1.Species AS First_Animal_Species,
A2.Name AS Second_Animal_Name,
A2.Species AS Second_Animal_Species
FROM Adoptions AS A1
INNER JOIN
Adoptions AS A2
ON A1.Adopter_Email = A2.Adopter_Email
AND
A1.Adoption_Date = A2.Adoption_Date
ORDER BY A1.Adopter_Email,
A1.Adoption_Date;
-- Adoptions no longer matched with themselves,
-- but we still get 2 rows for each adoption of 2 animals on the same day.
SELECT A1.Adopter_Email,
A1.Adoption_Date,
A1.Name AS First_Animal_Name,
A1.Species AS First_Animal_Species,
A2.Name AS Second_Animal_Name,
A2.Species AS Second_Animal_Species
FROM Adoptions AS A1
INNER JOIN
Adoptions AS A2
ON A1.Adopter_Email = A2.Adopter_Email
AND
A1.Adoption_Date = A2.Adoption_Date
AND
A1.Name <> A2.Name
ORDER BY A1.Adopter_Email,
A1.Adoption_Date;
-- Now we get only 1 row for each 'double' adoption, but we're not done yet.
SELECT A1.Adopter_Email,
A1.Adoption_Date,
A1.Name AS First_Animal_Name,
A1.Species AS First_Animal_Species,
A2.Name AS Second_Animal_Name,
A2.Species AS Second_Animal_Species
FROM Adoptions AS A1
INNER JOIN
Adoptions AS A2
ON A1.Adopter_Email = A2.Adopter_Email
AND
A1.Adoption_Date = A2.Adoption_Date
AND
A1.Name > A2.Name
ORDER BY A1.Adopter_Email,
A1.Adoption_Date;
-- Add an animal with the same name but of a difference species
INSERT INTO Animals (Name, Species, Primary_Color, Implant_Chip_ID, Breed, Gender, Birth_Date, Pattern, Admission_Date)
VALUES ('Duplicate', 'Dog', 'Black', NEWID(), NULL, 'M', '20171001', 'Solid', '20171101'),
('Duplicate', 'Rabbit', 'Black', NEWID(), NULL, 'M', '20171001', 'Solid', '20171101');
-- and both adopted on the same day, by the same person.
INSERT INTO Adoptions (Name, Species, Adopter_Email, Adoption_Date, Adoption_Fee)
VALUES ('Duplicate', 'Dog', 'alan.cook@hotmail.com', '20181201', 40),
('Duplicate', 'Rabbit', 'alan.cook@hotmail.com', '20181201', 40)
-- Try to execute again, will they show up?
SELECT A1.Adopter_Email,
A1.Adoption_Date,
A1.Name AS First_Animal_Name,
A1.Species AS First_Animal_Species,
A2.Name AS Second_Animal_Name,
A2.Species AS Second_Animal_Species
FROM Adoptions AS A1
INNER JOIN
Adoptions AS A2
ON A1.Adopter_Email = A2.Adopter_Email
AND
A1.Adoption_Date = A2.Adoption_Date
AND
A1.Name > A2.Name
ORDER BY A1.Adopter_Email,
A1.Adoption_Date;
-- You might have been tempted to do the following...
SELECT A1.Adopter_Email,
A1.Adoption_Date,
A1.Name AS First_Animal_Name,
A1.Species AS First_Animal_Species,
A2.Name AS Second_Animal_Name,
A2.Species AS Second_Animal_Species
FROM Adoptions AS A1
INNER JOIN
Adoptions AS A2
ON A1.Adopter_Email = A2.Adopter_Email
AND
A1.Adoption_Date = A2.Adoption_Date
AND
(A1.Name > A2.Name OR A1.Species <> A2.Species)
ORDER BY A1.Adopter_Email,
A1.Adoption_Date;
-- And then you might have been tempted to do...
SELECT A1.Adopter_Email,
A1.Adoption_Date,
A1.Name AS First_Animal_Name,
A1.Species AS First_Animal_Species,
A2.Name AS Second_Animal_Name,
A2.Species AS Second_Animal_Species
FROM Adoptions AS A1
INNER JOIN
Adoptions AS A2
ON A1.Adopter_Email = A2.Adopter_Email
AND
A1.Adoption_Date = A2.Adoption_Date
AND
(A1.Name > A2.Name OR A1.Species > A2.Species)
ORDER BY A1.Adopter_Email,
A1.Adoption_Date;
-- Spell out all 3 possible conditions
SELECT A1.Adopter_Email,
A1.Adoption_Date,
A1.Name AS First_Animal_Name,
A1.Species AS Firs_Animal_Species,
A2.Name AS Second_Animal_Name,
A2.Species AS Second_Animal_Species
FROM Adoptions AS A1
INNER JOIN
Adoptions AS A2
ON A1.Adopter_Email = A2.Adopter_Email
AND
A1.Adoption_Date = A2.Adoption_Date
AND ( (A1.Name = A2.Name AND A1.Species > A2.Species)
OR
(A1.Name > A2.Name AND A1. Species = A2.Species)
OR
(A1.Name > A2.Name AND A1.Species > A2.Species)
)
-- (Alvin Pillay 28/9/2020) The above code can be reduced to the following:
-- AND ( (A1.Name = A2.Name AND A1.Species > A2.Species)
-- OR
-- (A1.Name > A2.Name)
-- )
--
-- This makes logical sense because we use (A1.Name > A2.Name) to get rid of the "repeating groups" and then for the cases where two animals
-- have the same name but different species, we get rid of those with (A1.Name = A2.Name AND A1.Species > A2.Species).
ORDER BY A1.Adopter_Email,
A1.Adoption_Date;
-- For the last predicate, we can't use 2 > as there is no guarantee both name and species will be in that order.
-- We must use one of them as <>, doesn't matter which one...
-- A1.Name > A2.Name AND A1.Species <> A2.Species
SELECT A1.Adopter_Email,
A1.Adoption_Date,
A1.Name AS First_Animal_Name,
A1.Species AS Firs_Animal_Species,
A2.Name AS Second_Animal_Name,
A2.Species AS Second_Animal_Species
FROM Adoptions AS A1
INNER JOIN
Adoptions AS A2
ON A1.Adopter_Email = A2.Adopter_Email
AND
A1.Adoption_Date = A2.Adoption_Date
AND ( (A1.Name = A2.Name AND A1.Species > A2.Species)
OR
(A1.Name > A2.Name AND A1. Species = A2.Species)
OR
(A1.Name > A2.Name AND A1.Species <> A2.Species)
)
ORDER BY A1.Adopter_Email,
A1.Adoption_Date;
-- Alternatively, A1.Name <> A2.Name AND A1.Species > A2.Species
SELECT A1.Adopter_Email,
A1.Adoption_Date,
A1.Name AS First_Animal_Name,
A1.Species AS Firs_Animal_Species,
A2.Name AS Second_Animal_Name,
A2.Species AS Second_Animal_Species
FROM Adoptions AS A1
INNER JOIN
Adoptions AS A2
ON A1.Adopter_Email = A2.Adopter_Email
AND A1.Adoption_Date = A2.Adoption_Date
AND ( (A1.Name = A2.Name AND A1.Species > A2.Species)
OR
(A1.Name > A2.Name AND A1. Species = A2.Species)
OR
(A1.Name <> A2.Name AND A1.Species > A2.Species)
)
ORDER BY A1.Adopter_Email,
A1.Adoption_Date;
-- Cleanup
DELETE FROM Adoptions WHERE Name = 'Duplicate';
DELETE FROM Animals WHERE Name = 'Duplicate';