You are on page 1of 5

1 /*SnootyCat Database queries*/

2 /*
3 --NOTE: All ID's are just for demonstration comparation... They are not really required for the business.
For an nice layout rows output, the below query can be used. Using SET will insert a new column with
any given name (e.g. 'No') and all the records will be numbered from 1 to N. This is available only for
current session.
4 ==================
5 SET @No=0;
6 SELECT
7 @No:=@No+1 AS No
8 =================
9 */
10
11
12 /*=====================Q1============================*/
13 /*
14 --Total price for the sold tickest along with the Festival Name, Client Company and Person of Contact,
total no of the tickes sold and price per ticket. This query is for all festivals but it can be adjusted for
specific festival or criteria using WHERE clause.
15 */
16 SELECT
17 festival.id_festival AS ID,
18 festival.festival_name AS `Festival Name`,
19 festival_type.ft_name AS `Festival Type`,
20 festival.festival_date AS Date,
21 client.client_company AS `Client Company`,
22 CONCAT(client.client_name, ' ', client.client_surname) AS `Person of Contact`,
23 ticket_sale.ticket_sold AS `No of Tickets`,
24 CONCAT('£ ', FORMAT(ticket_sale.ticket_price, 2)) AS `Ticket Price`,
25 CONCAT('£ ', FORMAT(ticket_sale.ticket_sold * ticket_sale.ticket_price, 2)) AS `Total
Amount`
26 FROM ticket_sale
27 INNER JOIN festival
28 ON ticket_sale.id_festival = festival.id_festival
29 INNER JOIN client
30 ON festival.id_client = client.id_client
31 INNER JOIN festival_type
32 ON festival.id_festival_type = festival_type.id_festival_type
33 /*WHERE festival.id_festival = 1*/
34 GROUP BY festival.id_festival;
35
36
37 /*=====================Q2============================*/
38 /*
39 --Return the performing bands for a given festival along with their entry time, end time and time on stage
ordered by time_entry
40 */
41
42 SELECT
43 f.id_festival AS ID,
44 f.festival_name AS `Festival Name`,
45 f.festival_date AS Date,
46 band.band_name AS `Band Name`,
47 band.band_genre AS Genre,
48 DATE_FORMAT(festival_performers.fp_time_entry, '%H:%i') AS `Entry Time`,
49 DATE_FORMAT(festival_performers.fp_time_end, '%H:%i') AS `End Time`,
50 DATE_FORMAT(festival_performers.fp_stage_time, '%H h %i min') AS `Time On Stage`,
51 festival_performers.fp_note AS Notes
52 FROM festival_performers
53 INNER JOIN festival f
54 ON festival_performers.id_festival = f.id_festival
55 INNER JOIN band
56 ON festival_performers.id_band = band.id_band
57 WHERE f.id_festival = 1
58 GROUP BY festival_performers.fp_time_entry;
59
60
61 /*=====================Q3============================*/
62 /*
63 --Select staff tasks for a given festival where the task wasn't completed.
64 */
65
66 SELECT
67 f.id_festival AS ID,
68 f.festival_name AS `Festival Name`,
69 f.festival_date AS Date,
70 CONCAT(staff.staff_name, ' ', staff.staff_surname) AS `Staff Name`,
71 staff_role.staff_role_name AS Role,
72 staff_task.task_name AS `Task Name`,
73 staff_task.task_date AS `Assigned Date`,
74 DATE_FORMAT(staff_task.task_time, '%H : %i') AS `Assigned Time`,
75 staff_task.task_completed AS Completed,
76 staff_task.task_details AS Details
77 FROM staff_task
78 INNER JOIN festival f
79 ON staff_task.id_festival = f.id_festival
80 INNER JOIN staff
81 ON staff_task.id_staff = staff.id_staff
82 INNER JOIN staff_role
83 ON staff.id_staff_role = staff_role.id_staff_role
84 WHERE f.id_festival = 1
85 AND staff_task.task_completed = 'NO' \G
86
87 /*NOTE: Use \G for vertical display if the rows are too long and break the layout*/
88
89
90 /*=====================Q4============================*/
91 /*
92 -- A list of al vendors (partenrs) with the name, address, contact details, service(s) they provice and price
per service, sorted ASC on service name.
93 */
94
95 SELECT DISTINCT
96 partner.id_partner AS ID,
97 partner_group.partner_group_name AS `Group`,
98 CONCAT(partner.partner_company, ' ', partner.partner_name, ' ', partner.partner_surname)
AS Partner,
99 CONCAT(partner.partner_address1, ' ', partner.partner_address2, ' ', partner.partner_city, ' ',
partner.partner_postcode, ' ') AS Address,
100 CONCAT(partner.partner_email, ' | ', partner.partner_phone) AS Contact,
101 service.service_name AS Service,
102 CONCAT('£ ', FORMAT(service.service_price, 2)) AS Price,
103 service.service_description AS `Service Description`
104 FROM service
105 INNER JOIN partner
106 ON service.id_partner = partner.id_partner
107 INNER JOIN partner_group
108 ON partner.id_partner_group = partner_group.id_partner_group
109 ORDER BY Service;
110
111
112 /*=====================Q5 - Buildup====================*/
113 /*
114 --All payment for a given festival ID (IN/OUT)
115 */
116
117 SELECT
118 festival.id_festival AS ID,
119 festival.festival_name AS `Festival Name`,
120 festival.festival_budget AS `Festival Budget`,
121 payment.pmt_amount AS Payment,
122 payment.pmt_type AS `Payment Type`
123 FROM payment
124 INNER JOIN festival
125 ON payment.id_festival = festival.id_festival
126 WHERE festival.id_festival = 1
127 ORDER BY `Payment Type`;
128
129 /*
130 --Total payment OUT for a given festival
131 */
132
133 SELECT
134 festival.id_festival AS ID,
135 festival.festival_name AS `Festival Name`,
136 payment.pmt_type AS `Payment Type`,
137 payment.pmt_amount AS Amount
138 FROM payment
139 INNER JOIN festival
140 ON payment.id_festival = festival.id_festival
141 /* WHERE payment.pmt_type LIKE '%OUT'; */
142 WHERE festival.id_festival = 1
143 AND payment.pmt_type LIKE '%OUT';
144
145 /*
146 --Total Payment IN for a given festival
147 */
148
149 SELECT
150 festival.id_festival AS ID,
151 festival.festival_name AS `Festival Name`,
152 payment.pmt_type AS `Payment Type`,
153 payment.pmt_amount AS Amount
154 FROM payment
155 INNER JOIN festival
156 ON payment.id_festival = festival.id_festival
157 WHERE festival.id_festival = 1
158 AND payment.pmt_type LIKE '%IN';
159
160 /*
161 --Total IN/OUT payments for all festivals (or given festival using WHERE clause).
162 */
163
164 SELECT
165 f.id_festival AS `ID`,
166 f.festival_name AS `Festival Name`,
167 f.festival_date AS Date,
168 SUM(CASE WHEN p.pmt_type = 'Payment IN' THEN p.pmt_amount ELSE 0 END) AS `TOTAL IN
`,
169 SUM(CASE WHEN p.pmt_type = 'Payment OUT' THEN p.pmt_amount ELSE 0 END) AS `TOTAL
OUT`
170 FROM festival f
171 INNER JOIN payment p
172 ON p.id_festival = f.id_festival
173 /* WHERE f.id_festival = 1 */
174 GROUP BY f.id_festival;
175
176 /*=====================Q5====================*/
177 /*
178 --Total profit and loses for all festivals (or a given festival) calculating after this formula [BUDGET +
PAYMENT IN - PAYMENT OUT = TOTAL PROFIT]
179 */
180
181 SELECT
182 f.id_festival AS ID,
183 f.festival_name AS `Festival Name`,
184 f.festival_date AS Date,
185 CONCAT('£ ', FORMAT(f.festival_budget, 2)) AS Budget,
186 CONCAT('£ ', FORMAT(SUM(CASE WHEN p.pmt_type = 'Payment IN' THEN p.pmt_amount ELSE
0 END), 2)) AS `TOTAL IN`,
187 CONCAT('£ ', FORMAT(SUM(CASE WHEN p.pmt_type = 'Payment OUT' THEN p.pmt_amount
ELSE 0 END), 2)) AS `TOTAL OUT`,
188 CONCAT('£ ', FORMAT(f.festival_budget
189 + ROUND(SUM(CASE WHEN p.pmt_type = 'Payment IN' THEN p.pmt_amount ELSE 0 END), 2)
190 - ROUND(SUM(CASE WHEN p.pmt_type = 'Payment OUT' THEN p.pmt_amount ELSE 0 END), 2
), 2)) AS PROFIT
191 FROM festival f
192 INNER JOIN payment p
193 ON p.id_festival = f.id_festival
194 /*WHERE f.id_festival = 1*/
195 /*WHERE f.festival_date LIKE '2014%' */
196 /*WHERE f.festival_date < '2015-01-01' */
197 GROUP BY f.id_festival;
198
199 /*=====================Q6====================*/
200 /*
201 -- Select all services for a given festival, along with the company (partener) provinding that service,
service name, price and pitch location at the festival
202 */
203
204 SELECT
205 festival.id_festival AS ID,
206 festival.festival_name AS `Festival Name`,
207 festival.festival_date AS Date,
208 partner.partner_company AS `Company Name`,
209 CONCAT(partner.partner_name, ' ', partner.partner_surname) AS `Person of Contact`,
210 CONCAT(partner.partner_phone, ' | ', partner_email) AS `Phone & eMail`,
211 partner_group.partner_group_name AS `Services Group`,
212 service.service_name AS Service,
213 CONCAT('£ ', FORMAT(service.service_price, 2)) AS Price,
214 festival_details.pitch_location AS Pitch
215 FROM service
216 INNER JOIN partner
217 ON service.id_partner = partner.id_partner
218 INNER JOIN partner_group
219 ON partner.id_partner_group = partner_group.id_partner_group
220 INNER JOIN festival_details
221 ON festival_details.id_service = service.id_service
222 AND festival_details.id_partner = partner.id_partner
223 INNER JOIN festival
224 ON festival_details.id_festival = festival.id_festival
225 WHERE festival.id_festival = 1
226 ORDER BY ID;
227
228 /*=====================Q7====================*/
229 /*
230 --Select venue(s) with a capacity >= to 3000 places and a renting price <= £20.0000;
231 */
232
233 SET @No = 0;
234 SELECT
235 @No := @No + 1 AS No,
236 venue.id_venue AS ID,
237 venue.venue_name AS `Venue Name`,
238 venue.venue_city AS `Venue City`,
239 venue.venue_capacity AS `Venue Capacity`,
240 CONCAT('£ ', FORMAT(venue.venue_rent_price, 2)) AS `Venue Rent Price`,
241 partner.partner_company AS Company,
242 CONCAT(partner.partner_name, ' ', partner.partner_surname) AS `Person of Contact`,
243 CONCAT(partner.partner_email, ' | ', partner.partner_phone) AS `Contact Details`
244 FROM landowner
245 INNER JOIN partner
246 ON landowner.id_partner = partner.id_partner
247 INNER JOIN venue
248 ON venue.id_landowner = landowner.id_landowner
249 WHERE venue.venue_capacity >= 3000
250 AND venue.venue_rent_price <= 20000;

You might also like