-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAi chatbot.sql
310 lines (287 loc) · 11.8 KB
/
Ai chatbot.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
USE AI_Chatbot;
-- Drop tables if they exist
DROP TABLE IF EXISTS Interaction;
DROP TABLE IF EXISTS Intent;
DROP TABLE IF EXISTS Message;
DROP TABLE IF EXISTS Conversation;
DROP TABLE IF EXISTS User;
-- Create User table
CREATE TABLE User (
UserID INT AUTO_INCREMENT PRIMARY KEY,
Username VARCHAR(255),
Email VARCHAR(255),
Password VARCHAR(255),
RegistrationDate DATETIME,
LastLoginDate DATETIME
);
-- Create Conversation table
CREATE TABLE Conversation (
ConversationID INT AUTO_INCREMENT PRIMARY KEY,
UserID INT,
StartTime DATETIME,
EndTime DATETIME,
FOREIGN KEY (UserID) REFERENCES User(UserID)
);
-- Create Message table
CREATE TABLE IF NOT EXISTS Message (
MessageID INT AUTO_INCREMENT PRIMARY KEY,
ConversationID INT,
SenderID INT,
Content TEXT,
FOREIGN KEY (ConversationID) REFERENCES Conversation(ConversationID),
FOREIGN KEY (SenderID) REFERENCES User(UserID)
);
-- Create Intent table
CREATE TABLE IF NOT EXISTS Intent (
IntentID INT AUTO_INCREMENT PRIMARY KEY,
Name VARCHAR(255),
Description TEXT
);
-- Create Interaction table
CREATE TABLE IF NOT EXISTS Interaction (
InteractionID INT AUTO_INCREMENT PRIMARY KEY,
UserID INT,
IntentID INT,
MessageID INT,
ConfidenceLevel FLOAT,
FOREIGN KEY (UserID) REFERENCES User(UserID),
FOREIGN KEY (IntentID) REFERENCES Intent(IntentID),
FOREIGN KEY (MessageID) REFERENCES Message(MessageID)
);
-- Insert sample data into User table with real historical timestamps
INSERT INTO User (Username, Email, Password, RegistrationDate, LastLoginDate)
VALUES
('John Doe', '[email protected]', 'password123', '2023-12-01 08:00:00', '2024-04-20 10:30:00'),
('Jane Smith', '[email protected]', 'securepass', '2023-12-05 10:00:00', '2024-04-22 14:45:00'),
('Alice Johnson', '[email protected]', 'pass1234', '2023-12-10 12:00:00', '2024-04-23 09:15:00'),
('Bob White', '[email protected]', 'password', '2023-12-15 14:00:00', '2024-04-24 11:20:00'),
('Emma Davis', '[email protected]', 'password1234', '2023-12-20 16:00:00', '2024-04-25 08:45:00'),
('James Wilson', '[email protected]', '123456', '2023-12-25 18:00:00', '2024-04-26 10:00:00'),
('Sarah Brown', '[email protected]', 'password321', '2023-12-30 20:00:00', '2024-04-27 12:00:00'),
('Michael Taylor', '[email protected]', 'mypassword', '2024-01-04 22:00:00', '2024-04-28 14:00:00'),
('Laura Clark', '[email protected]', 'password123', '2024-01-09 08:00:00', '2024-04-29 16:00:00'),
('David Martinez', '[email protected]', 'password', '2024-01-14 10:00:00', '2024-04-30 18:00:00'),
('Jennifer Hall', '[email protected]', 'securepass123', '2024-01-19 12:00:00', '2024-05-01 20:00:00'),
('Daniel Thompson', '[email protected]', 'pass123', '2024-01-24 14:00:00', '2024-05-02 22:00:00'),
('Jessica Lee', '[email protected]', 'password123', '2024-01-29 16:00:00', '2024-05-03 08:00:00'),
('Kevin Rodriguez', '[email protected]', 'securepassword', '2024-02-03 18:00:00', '2024-05-04 10:00:00'),
('Amanda Garcia', '[email protected]', 'password321', '2024-02-08 20:00:00', '2024-05-05 12:00:00'),
('Ryan Martinez', '[email protected]', '123456', '2024-02-13 22:00:00', '2024-05-06 14:00:00'),
('Nicole Hernandez', '[email protected]', 'mypassword', '2024-02-18 08:00:00', '2024-05-07 16:00:00'),
('Justin Smith', '[email protected]', 'password1234', '2024-02-23 10:00:00', '2024-05-08 18:00:00'),
('Samantha Johnson', '[email protected]', 'password', '2024-02-28 12:00:00', '2024-05-09 20:00:00'),
('Brandon Davis', '[email protected]', 'password123', '2024-03-04 14:00:00', '2024-05-10 22:00:00'),
('Rachel Wilson', '[email protected]', 'securepass', '2024-03-09 16:00:00', '2024-05-11 08:00:00'),
('Tyler Miller', '[email protected]', 'pass1234', '2024-03-14 18:00:00', '2024-05-12 10:00:00'),
('Lauren Anderson', '[email protected]', 'password', '2024-03-19 20:00:00', '2024-05-13 12:00:00'),
('Andrew Thompson', '[email protected]', 'password1234', '2024-03-24 22:00:00', '2024-05-14 14:00:00'),
('Megan Moore', '[email protected]', '123456', '2024-03-29 08:00:00', '2024-05-15 16:00:00');
select * from User;
-- Insert sample data into Message table
INSERT INTO Message (ConversationID, SenderID, Content)
VALUES
(1, 1, 'Hello, how can I help you?'),
(1, 2, 'Hi there, I have a question about...'),
(2, 1, 'I need assistance with...');
-- Insert sample data into Intent table
INSERT INTO Intent (Name, Description)
VALUES
('Greeting', 'Intent for greeting messages'),
('Inquiry', 'Intent for inquiry messages'),
('Request', 'Intent for request messages');
-- Insert sample data into Interaction table
INSERT INTO Interaction (UserID, IntentID, MessageID, ConfidenceLevel)
VALUES
(1, 1, 1, 0.9),
(2, 2, 2, 0.8),
(3, 3, 3, 0.7);
-- Insert sample data into Conversation table with specified timestamps
INSERT INTO Conversation (UserID, StartTime, EndTime)
VALUES
(1, '2024-01-01 08:00:00', '2024-01-01 08:30:00'),
(2, '2024-01-05 10:00:00', '2024-01-05 11:00:00'),
(3, '2024-01-10 12:00:00', '2024-01-10 12:30:00'),
(4, '2024-01-15 14:00:00', '2024-01-15 14:30:00'),
(5, '2024-01-20 16:00:00', '2024-01-20 16:30:00'),
(6, '2024-01-25 18:00:00', '2024-01-25 18:30:00'),
(7, '2024-01-30 20:00:00', '2024-01-30 20:30:00'),
(8, '2024-02-04 22:00:00', '2024-02-04 22:30:00'),
(9, '2024-02-09 08:00:00', '2024-02-09 08:30:00'),
(10, '2024-02-14 10:00:00', '2024-02-14 10:30:00'),
(11, '2024-02-19 12:00:00', '2024-02-19 12:30:00'),
(12, '2024-02-24 14:00:00', '2024-02-24 14:30:00'),
(13, '2024-02-29 16:00:00', '2024-02-29 16:30:00'),
(14, '2024-03-05 18:00:00', '2024-03-05 18:30:00'),
(15, '2024-03-10 20:00:00', '2024-03-10 20:30:00'),
(16, '2024-03-15 22:00:00', '2024-03-15 22:30:00'),
(17, '2024-03-20 08:00:00', '2024-03-20 08:30:00'),
(18, '2024-03-25 10:00:00', '2024-03-25 10:30:00'),
(19, '2024-03-30 12:00:00', '2024-03-30 12:30:00'),
(20, '2024-04-04 14:00:00', '2024-04-04 14:30:00'),
(21, '2024-04-09 16:00:00', '2024-04-09 16:30:00'),
(22, '2024-04-14 18:00:00', '2024-04-14 18:30:00'),
(23, '2024-04-19 20:00:00', '2024-04-19 20:30:00'),
(24, '2024-04-24 22:00:00', '2024-04-24 22:30:00'),
(25, '2024-04-29 08:00:00', '2024-04-29 08:30:00');
select * from Conversation;
-- Insert sample data into Intent table,
INSERT INTO Intent (Name, Description)
VALUES
('Farewell', 'Intent for farewell messages'),
('Complaint', 'Intent for complaint messages'),
('Praise', 'Intent for praise messages'),
('Feedback', 'Intent for feedback messages'),
('Question', 'Intent for question messages'),
('Confirmation', 'Intent for confirmation messages'),
('Apology', 'Intent for apology messages'),
('Appreciation', 'Intent for appreciation messages'),
('Suggestion', 'Intent for suggestion messages'),
('Offer', 'Intent for offer messages'),
('Warning', 'Intent for warning messages'),
('Acknowledgment', 'Intent for acknowledgment messages'),
('Encouragement', 'Intent for encouragement messages'),
('Request', 'Intent for request messages'),
('Explanation', 'Intent for explanation messages'),
('Reminder', 'Intent for reminder messages'),
('Assurance', 'Intent for assurance messages'),
('Agreement', 'Intent for agreement messages'),
('Disagreement', 'Intent for disagreement messages'),
('Confusion', 'Intent for confusion messages'),
('Clarification', 'Intent for clarification messages'),
('Approval', 'Intent for approval messages'),
('Rejection', 'Intent for rejection messages'),
('Support', 'Intent for support messages');
-- Insert sample data into Interaction table
INSERT INTO Interaction (UserID, IntentID, MessageID, ConfidenceLevel)
VALUES
(1, 1, 1, 0.9),
(2, 2, 2, 0.8),
(3, 3, 3, 0.7),
(4, 4, 4, 0.6),
(5, 5, 5, 0.5),
(6, 6, 6, 0.4),
(7, 7, 7, 0.3),
(8, 8, 8, 0.2),
(9, 9, 9, 0.1),
(10, 10, 10, 0.9),
(11, 11, 11, 0.8),
(12, 12, 12, 0.7),
(13, 13, 13, 0.6),
(14, 14, 14, 0.5),
(15, 15, 15, 0.4),
(16, 16, 16, 0.3),
(17, 17, 17, 0.2),
(18, 18, 18, 0.1),
(19, 19, 19, 0.9),
(20, 20, 20, 0.8),
(21, 21, 21, 0.7),
(22, 22, 22, 0.6),
(23, 23, 23, 0.5),
(24, 24, 24, 0.4),
(25, 25, 25, 0.3);
select * from Interaction;
-- Total number of messages sent by each user Joins
SELECT u.UserID, u.Username, COUNT(m.MessageID) AS TotalMessagesSent
FROM User u
LEFT JOIN Conversation c ON u.UserID = c.UserID
LEFT JOIN Message m ON c.ConversationID = m.ConversationID
GROUP BY u.UserID, u.Username;
-- Total number of conversations started by each user:
SELECT u.UserID, u.Username, COUNT(c.ConversationID) AS TotalConversationsStarted
FROM User u
LEFT JOIN Conversation c ON u.UserID = c.UserID
GROUP BY u.UserID, u.Username;
-- Average confidence level of interactions for each user:
SELECT u.UserID, u.Username, AVG(i.ConfidenceLevel) AS AvgConfidenceLevel
FROM User u
LEFT JOIN Interaction i ON u.UserID = i.UserID
GROUP BY u.UserID, u.Username;
-- Total number of interactions of each type (intent):
SELECT i.Name, COUNT(*) AS TotalInteractions
FROM Intent i
LEFT JOIN Interaction inter ON i.IntentID = inter.IntentID
GROUP BY i.Name;
-- Find the most active users (by total interactions) who joined within the last month:
SELECT u.UserID, u.Username, COUNT(i.InteractionID) AS TotalInteractions
FROM User u
LEFT JOIN Interaction i ON u.UserID = i.UserID
WHERE u.RegistrationDate >= DATE_SUB(NOW(), INTERVAL 1 MONTH)
GROUP BY u.UserID, u.Username
ORDER BY TotalInteractions DESC;
-- Find users who have sent the most messages:
SELECT Username, Email
FROM User
WHERE UserID = (
SELECT UserID
FROM (
SELECT UserID, COUNT(*) AS TotalMessages
FROM Conversation
JOIN Message ON Conversation.ConversationID = Message.ConversationID
GROUP BY UserID
ORDER BY TotalMessages DESC
LIMIT 1
) AS SubQuery
);
-- List conversations started by users who have registered within the last month:
SELECT ConversationID, StartTime
FROM Conversation
WHERE UserID IN (
SELECT UserID
FROM User
WHERE RegistrationDate >= DATE_SUB(NOW(), INTERVAL 1 MONTH)
);
-- Find intents with more than 100 interactions:
SELECT Name
FROM Intent
WHERE IntentID IN (
SELECT IntentID
FROM Interaction
GROUP BY IntentID
HAVING COUNT(*) > 20
);
-- List users who have never started a conversation:
SELECT Username
FROM User
WHERE UserID NOT IN (
SELECT DISTINCT UserID
FROM Conversation
);
-- Find the conversation with the most messages:
SELECT ConversationID
FROM Conversation
WHERE ConversationID = (
SELECT ConversationID
FROM (
SELECT ConversationID, COUNT(*) AS TotalMessages
FROM Message
GROUP BY ConversationID
ORDER BY TotalMessages DESC
LIMIT 1
) AS SubQuery
);
-- Find conversations started by users who have sent the most messages
SELECT c.ConversationID, c.StartTime
FROM Conversation c
INNER JOIN (
SELECT UserID, COUNT(*) AS TotalMessages
FROM Conversation
JOIN Message ON Conversation.ConversationID = Message.ConversationID
GROUP BY UserID
ORDER BY TotalMessages DESC
LIMIT 1
) AS SubQuery ON c.UserID = SubQuery.UserID;
-- Find users who have not interacted with any intents
SELECT u.UserID, u.Username
FROM User u
LEFT JOIN Interaction i ON u.UserID = i.UserID
WHERE i.UserID IS NULL;
-- Find the unique set of users who have either started a conversation or interacted with an intent:
SELECT UserID FROM Conversation
UNION
SELECT UserID FROM Interaction;
-- Join with the Conversation table using the earliest login time
SELECT
UserID,
MIN(LastLoginDate) AS EarliestLoginTime
FROM User
GROUP BY UserID;