-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperations.sql
197 lines (172 loc) · 5.37 KB
/
operations.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
197
--SQL to aggregate and analyze feedback by rating, product, or region.
--1. Aggregate Feedback by Rating
SELECT
rating,
COUNT(*) AS feedback_count,
AVG(rating) AS average_rating
FROM
Feedback
GROUP BY
rating
ORDER BY
feedback_count DESC;
--2. Aggregate Feedback by Product
SELECT
p.name,
COUNT(f.feedback_id) AS feedback_count,
AVG(f.rating) AS average_rating
FROM
Feedback f
JOIN
Product p ON f.product_id = p.product_id
GROUP BY
p.name
ORDER BY
feedback_count DESC;
--3. Aggregate Feedback by Region
SELECT
r.region_name,
(SELECT COUNT(*)
FROM Feedback f
JOIN Customer c ON f.customer_id = c.customer_id
WHERE c.region_id = r.region_id) AS feedback_count,
(SELECT AVG(f.rating)
FROM Feedback f
JOIN Customer c ON f.customer_id = c.customer_id
WHERE c.region_id = r.region_id) AS average_rating
FROM
Region r
ORDER BY
feedback_count DESC;
--4. Aggregate Feedback by Product and Region
SELECT
p.name AS product_name,
r.region_name,
(SELECT COUNT(*)
FROM Feedback f
JOIN Customer c ON f.customer_id = c.customer_id
WHERE f.product_id = p.product_id
AND c.region_id = r.region_id) AS feedback_count,
(SELECT AVG(f.rating)
FROM Feedback f
JOIN Customer c ON f.customer_id = c.customer_id
WHERE f.product_id = p.product_id
AND c.region_id = r.region_id) AS average_rating
FROM
Product p
JOIN
Region r ON r.region_id BETWEEN 1 AND 10
ORDER BY
feedback_count DESC;
--5. Find Most Common Complaints (By Rating)
SELECT
f.rating,
DBMS_LOB.SUBSTR(f.comments, 4000) AS comment_text,
COUNT(DBMS_LOB.SUBSTR(f.comments, 4000)) AS complaint_count
FROM
Feedback f
GROUP BY
f.rating, DBMS_LOB.SUBSTR(f.comments, 4000)
ORDER BY
complaint_count DESC;
--6. Find Common Complaints in a Specific Region
SELECT
f.rating,
DBMS_LOB.SUBSTR(f.comments, 4000) AS comment_text,
COUNT(DBMS_LOB.SUBSTR(f.comments, 4000)) AS complaint_count
FROM
Feedback f
JOIN
Customer c ON f.customer_id = c.customer_id
JOIN
Region r ON c.region_id = r.region_id
WHERE
r.region_name = 'North Region'
GROUP BY
f.rating, DBMS_LOB.SUBSTR(f.comments, 4000)
ORDER BY
complaint_count DESC;
--after insert trigger on the feedback table ***********************************
CREATE OR REPLACE TRIGGER feedback_insert_trigger
AFTER INSERT ON Feedback
FOR EACH ROW
BEGIN
INSERT INTO Audit_Log (customer_id, action_type, action_timestamp, modified_by, old_value, new_value)
VALUES (
:new.customer_id,
'INSERT',
SYSDATE,
'admin',
NULL,
'New Feedback: ' || :new.rating || ' - ' || :new.comments
);
END;
--before update trigger on the feedback table
CREATE OR REPLACE TRIGGER feedback_update_trigger
BEFORE UPDATE ON Feedback
FOR EACH ROW
BEGIN
INSERT INTO Audit_Log (customer_id, action_type, action_timestamp, modified_by, old_value, new_value)
VALUES (
:new.customer_id,
'UPDATE',
SYSDATE,
'admin',
'Old Feedback: ' || :old.rating || ' - ' || :old.comments,
'Updated Feedback: ' || :new.rating || ' - ' || :new.comments
);
END;
update feedback set rating=3 where feedback_id = 1;
select * from feedback;
--BEFORE delete trigger on the feedback table
CREATE OR REPLACE TRIGGER feedback_delete_trigger
BEFORE DELETE ON Feedback
FOR EACH ROW
BEGIN
INSERT INTO Audit_Log (customer_id, action_type, action_timestamp, modified_by, old_value, new_value)
VALUES (
:OLD.customer_id,
'DELETE',
SYSDATE,
'admin',
'Deleted Feedback: ' || :OLD.rating || ' - ' || :OLD.comments,
NULL
);
END;
--PL/SQL functions to identify the most common complaints.
CREATE OR REPLACE PROCEDURE get_most_common_complaint(
p_product_id IN INT DEFAULT NULL,
p_common_complaint OUT VARCHAR2
) IS
BEGIN
p_common_complaint := 'No complaints found';
SELECT comments
INTO p_common_complaint
FROM (
SELECT comments, COUNT(*) AS comment_count
FROM Feedback
WHERE (p_product_id IS NULL OR product_id = p_product_id)
GROUP BY comments
ORDER BY comment_count DESC
)
WHERE ROWNUM = 1;
EXCEPTION
WHEN NO_DATA_FOUND THEN
p_common_complaint := 'No complaints found';
WHEN OTHERS THEN
p_common_complaint := 'An error occurred';
END get_most_common_complaint;
set serveroutput on
DECLARE
v_complaint VARCHAR2(255);
BEGIN
get_most_common_complaint(p_product_id => 101, p_common_complaint => v_complaint);
DBMS_OUTPUT.PUT_LINE('Most common complaint for product 101: ' || v_complaint);
END;
set serveroutput on
DECLARE
v_complaint VARCHAR2(255);
BEGIN
get_most_common_complaint(p_common_complaint => v_complaint);
DBMS_OUTPUT.PUT_LINE('Most common complaint: ' || v_complaint);
END;