0% found this document useful (0 votes)
15 views

CREATE DATABASE IF NOT EXISTS operational

Uploaded by

farhashaikh.3535
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

CREATE DATABASE IF NOT EXISTS operational

Uploaded by

farhashaikh.3535
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Operation Analytics and

Investigating Metric Spike

Name : Shaikh Farhatunnisha

Project Submitted to : Trainity

CASE 1:
CREATE DATABASE IF NOT EXISTS operational_analytics;

USE operational_analytics;

CREATE TABLE IF NOT EXISTS job_data (

ds DATE,

job_id INT,

actor_id INT,

event VARCHAR(50),

language VARCHAR(50),

time_spent INT,

org CHAR(1)

);

INSERT INTO job_data (ds, job_id, actor_id, event, language, time_spent,


org) VALUES

('2020-11-30', 21, 1001, 'skip', 'English', 15, 'A'),

('2020-11-30', 22, 1006, 'transfer', 'Arabic', 25, 'B'),

('2020-11-29', 23, 1003, 'decision', 'Persian', 20, 'C'),

('2020-11-28', 23, 1005, 'transfer', 'Persian', 22, 'D'),

('2020-11-28', 25, 1002, 'decision', 'Hindi', 11, 'B'),

('2020-11-27', 11, 1007, 'decision', 'French', 104, 'D'),

('2020-11-26', 23, 1004, 'skip', 'Persian', 56, 'A'),

('2020-11-25', 20, 1003, 'transfer', 'Italian', 45, 'C');

SELECT * FROM job_data;


Jobs Reviewed Over Time
Objective: Calculate the number of jobs reviewed per hour for
each day in November 2020.
SELECT
DATE(ds) AS review_date,
HOUR(ds) AS review_hour,
COUNT(job_id) AS jobs_reviewed
FROM
job_data
WHERE
DATE(ds) BETWEEN '2020-11-01' AND '2020-11-30'
GROUP BY
review_date, review_hour
ORDER BY
review_date, review_hour;
Explanation: This query extracts the date and hour from the
ds column and counts the number of job reviews (job_id) for
each hour in November 2020. It groups by review_date and
review_hour to provide hourly job counts.

2. Throughput Analysis
Objective: Calculate the 7-day rolling average of throughput
(number of events per second).
SELECT
ds,
AVG(event_count / 86400) OVER (ORDER BY ds ROWS
BETWEEN 6 PRECEDING AND CURRENT ROW) AS
rolling_throughput
FROM (
SELECT
DATE(ds) AS ds,
COUNT(event) AS event_count
FROM
job_data
GROUP BY
DATE(ds)
) AS daily_events
ORDER BY ds;
Explanation:
 The inner query counts the total events per day.
 The outer query calculates a 7-day rolling average
throughput, where event_count / 86400 represents events
per second (assuming each day has 86400 seconds).
3. Language Share Analysis
Objective: Calculate the percentage share of each language in
the last 30 days.
SELECT
language,
ROUND((COUNT(language) * 100.0) / (SELECT COUNT(*)
FROM job_data WHERE DATE(ds) >= DATE_SUB(CURDATE(),
INTERVAL 30 DAY)), 2) AS language_share_percentage
FROM
job_data
WHERE
DATE(ds) >= DATE_SUB(CURDATE(), INTERVAL 30 DAY)
GROUP BY
language
ORDER BY
language_share_percentage DESC;
Explanation: This query calculates the percentage of each
language's usage over the past 30 days by dividing the count
of each language by the total count of events in that period.
4. Duplicate Rows Detection
Objective: Identify duplicate rows in the data.
SELECT
ds, job_id, actor_id, event, language, time_spent, org,
COUNT(*) AS duplicate_count
FROM
job_data
GROUP BY
ds, job_id, actor_id, event, language, time_spent, org
HAVING
duplicate_count > 1;
Explanation: This query groups the data by all columns and
uses HAVING to show only rows with duplicate entries (where
the count is more than 1).
CASE 2:
CREATE DATABASE IF NOT EXISTS invesgtigatingspikes;

-- Switch to the newly created database


USE invesgtigatingspikes;

-- Create the Users table


CREATE TABLE users (
user_id INT PRIMARY KEY,
created_at TIMESTAMP
);

-- Create the Events table


CREATE TABLE events (
event_id INT PRIMARY KEY,
user_id INT,
occurred_at TIMESTAMP,
event_type VARCHAR(50),
event_name VARCHAR(100),
device VARCHAR(50),
FOREIGN KEY (user_id) REFERENCES users(user_id)
);

-- Create the Email_Events table


CREATE TABLE email_events (
email_event_id INT PRIMARY KEY,
user_id INT,
occurred_at TIMESTAMP,
action VARCHAR(50),
FOREIGN KEY (user_id) REFERENCES users(user_id)
);

a. Find Users with Events


SELECT u.user_id, u.created_at, e.event_type,
e.event_name, e.occurred_at
FROM users u
JOIN events e ON u.user_id = e.user_id
ORDER BY e.occurred_at;
b. Email Open Actions per User
SELECT u.user_id, ee.action, ee.occurred_at
FROM users u
JOIN email_events ee ON u.user_id = ee.user_id
WHERE ee.action = 'Open'
ORDER BY ee.occurred_at;
c. Find Users Who Have Both Events and Email Actions
SELECT DISTINCT u.user_id
FROM users u
JOIN events e ON u.user_id = e.user_id
JOIN email_events ee ON u.user_id = ee.user_id;

Data Aggregation
SELECT user_id, COUNT(*) AS event_count
FROM events
GROUP BY user_id
ORDER BY event_count DESC;

You might also like