-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsizzler-oql.sql
198 lines (174 loc) · 6.12 KB
/
sizzler-oql.sql
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
-- Bare extents --
branches
saladBars
computerMachines
timeAttendanceMachines
employees
waiters
kitchenPorters
chefs
cashiers
kitchenManagers
deliveryMen
employeeWagePayments
branchManagers
clockInOuts
cashierMachines
customerPaxes
customerDeliveries
orders
workTimes
memberCustomers
customerRewardRedemption
memberLevelRefs
redeemableRewards
billings
billingOnSite
billingDelivery
cashierBillingHandlings
inventoryInboundOrders
cashTransactions
creditTransactions
giftVoucherTransactions
giftVoucherRefs
giftVouchers
orderItems
seasonRefs
menuAvailability
menuRefs
servingRefs
menuServingRefs
foodItemRefs
servingFoodItemRef
foodItemIngredientRefs
foodIngredientRefs
menuServingCustomizations
saladBarServings
saladBarRefills
-- 0: List all branches that are normally operational
DEFINE OperatingBranches AS
SELECT b
FROM b IN branches
WHERE b.status = 'normally operational';
-- 1: Get branches that is located in a given province name
DEFINE BranchesInProvince(provinceName) AS
SELECT b
FROM b IN OperatingBranches
WHERE b.address.province = provinceName;
-- 2: List every branch's name, province as well as telephone number
SELECT struct(name: b.name, province: b.address.province, telephoneNumbers: b.telephoneNumber)
FROM b IN OperatingBranches;
-- 3: List customers who registered themself close to a given branch name.
DEFINE CustomersWhoLocatedNearBranch(branchName) AS
SELECT [DISTINCT] b.locatedNear
FROM b IN OperatingBranches
WHERE b.name = branchName;
-- 4: Get every inventory order made by a given branch
DEFINE InventoryOrdersOfBranch(branchName) AS
SELECT [DISTINCT] b.manages
FROM b IN OperatingBranches
WHERE b.name = branchName;
-- 5: Get all servings of the salad bar in a given branch
DEFINE SaladServingOfBranch(branchName) AS
SELECT [DISTINCT] b.offersSaladBar.leadsTo
FROM b IN branches
WHERE b.name = branchName;
-- 6: List all employees who work for a given branch
DEFINED EmployeesWhoWorkForBranch(branchName) AS
SELECT [DISTINCT] b.operatedBy
FROM b IN branches
WHERE b.name = branchName;
-- 7: List employees who has an age lower than 18 years old
SELECT e
FROM e IN employees
WHERE (EXTRACT(YEAR FROM now()) - EXTRACT(YEAR FROM e.birthdate)) < 18;
-- 8: List employees who work over 3 days per week
SELECT e
FROM e IN employees
WHERE COUNT(e.workTimes) > 3;
-- 9: List employees who get paid more than the overall wage payment average
SELECT e
FROM e IN employees
WHERE e.float * 30 > (SELECT AVG(wagePaymentAmount) FROM e IN employees);
-- 10: List all employees who carry out salad bar refills along with the food item involved
SELECT e.carriesOut
FROM e IN employees
SELECT struct(employee: x.e, foodItems: x.e.carriesOut.refersTo)
FROM x IN (
SELECT e
FROM e IN employees
WHERE e.carriesOut IS NOT NULL
);
-- 11: List all waiters who are fluent not only in Thai and English but also Chinese
SELECT w
FROM w IN waiters
WHERE 'Thai' IN w.languageFluency AND 'English' IN w.languageFluency AND 'Chinese' IN w.languageFluency;
-- 12: For each chef, list cooking roles that is in the highest priority along with the information of the respective chef.
SELECT struct( c.employeeId,
c.firstname,
c.surname,
highPriorityCookingRoles: (
SELECT role
FROM role IN c.cookingRole
WHERE role.priority = 'high'))
FROM c in chefs;
-- 13: Identify the list of billing handled by a given Cashier employee Id
DEFINE BillingHandledBy(employeeId) AS
SELECT h.BillingOnSite
FROM h in (
SELECT c.participates
FROM c in cashiers
WHERE c.employeeId = employeeId
);
-- 14: Find the average amount of payment that is handled by each employee
SELECT struct(
cashierId,
averageAmountOfHandledPayments: AVG(
SELECT SUM((oi.perUnitPrice + oi.perUnitTakeHomeFee - oi.perUnitDiscount) * oi.quantity)
FROM oi IN (SELECT o.includes
FROM o in (SELECT p.cbh.needed.orders FROM p IN partition))
)
)
FROM cbh IN cashierBillingHandlings
GROUP BY cashierId: cbh.participatedBy.employeeId;
-- 15: Count the number of issued Gift Vouchers from each Gift Voucher type
SELECT giftVoucherRefId, totalNumberOfIssuedVouchers: COUNT(partition)
FROM g IN giftVouchers
GROUP BY giftVoucherRefId: g.refersTo.giftVoucherRefId;
-- 16: For each Order, count the number of order items as well as the price of each
SELECT orderId,
totalPrice: SUM(SELECT (p.oi.perUnitPrice + p.oi.perUnitTakeHomeFee - p.oi.perUnitDiscount) * p.oi.quantity FROM p IN partition),
totalItemsInOrder: COUNT(partition)
FROM oi IN orderItems
GROUP BY orderId: oi.includedIn.orderId;
-- 17: Select food menus that are currently available to customers a given time and branchId.
DEFINE AvailableMenus(branchId, givenTime) AS
SELECT m
FROM m IN menuRefs
WHERE m.isActive IS TRUE AND
EXISTS(
SELECT ma
FROM ma IN m.dependsOnAvailability
WHERE EXTRACT(DAY_OF_WEEK FROM givenTime) = ma.dayOfWeek
AND EXTRACT(TIME FROM givenTime) IS BETWEEN ma.timeRangeStart AND ma.timeRangeEnd)
AND EXISTS(
SELECT sr
FROM sr IN n.dependsOnSeason
WHERE givenTime IS BETWEEN sr.dateStart AND sr.dateEnd
);
-- 18: For each menu, identify the quantity and the price that are in each order item.
SELECT m.referredBy,
totalSaleWorth: SUM(SELECT (p.oi.perUnitPrice + p.oi.perUnitTakeHomeFee - p.oi.perUnitDiscount) * p.oi.quantity FROM p IN partition),
totalSaleQuantity: SUM(SELECT p.oi.quantity FROM p IN partition)
FROM oi in orderItems
GROUP BY menuRefId: oi.refersTo.menuRefId;
-- 19: From all inventory inbound orders, identify the number of orders and total price of each food ingredient.
SELECT foodIngredientRefId, totalOrders: COUNT(partition), totalPrices: SUM(iioi.quantity * iioi.pricePerUnit)
FROM iioi IN (
SELECT iio.items
FROM iio IN inventoryInboundOrders
)
GROUP BY foodIngredientRefId: iioi.involves.long;
-- 20: List billings that belong each branch along with handling employee as well
SELECT branchId: cbh.involves.deployedBy.branchId, cbh.needed, cbh.participatedBy
FROM cbh IN cashierBillingHandlings;