(assigned_name in works_on is percentage of time employee is assigned to project)
Question:
Find the IDs of employees assigned to a project that is more than 20 hours per week.
My attempt:
I have assumed 24 hours as working hours here. Is my query correct?
Here's the create and insert statements.
CREATE TABLE IF NOT EXISTS employees(
employee_id NUMERIC(9),
first_name VARCHAR(10),
last_name VARCHAR(20),
dept_code CHAR(5),
salary NUMERIC(9, 2),
PRIMARY KEY (employee_id)
);
CREATE TABLE IF NOT EXISTS departments(
code CHAR(5),
name VARCHAR(30),
manager_id NUMERIC(9),
sub_dept_of CHAR(5),
PRIMARY KEY (code)
);
CREATE TABLE IF NOT EXISTS projects(
project_id CHAR(8),
dept_code CHAR(5),
description VARCHAR(200),
start_date DATE,
stop_date DATE,
revenue NUMERIC(12, 2),
PRIMARY KEY (project_id)
);
CREATE TABLE IF NOT EXISTS works_on(
employee_id NUMERIC(9),
project_id CHAR(8),
assigned_time NUMERIC(3, 2),
PRIMARY KEY (employee_id, project_id)
);
INSERT INTO employees
VALUES (1, 'Al', 'Betheleader', 'ADMIN', 70000),
(2, 'Pl', 'Rsquared', 'ACCNT', 40000),
(3, 'Harry', 'Hardware', 'HDWRE', 50000),
(4, 'Sussie', 'Software', 'CNSLT', 60000),
(5, 'Abe', 'Advice', 'CNSLT', 30000),
(6, 'Hardly', 'Aware', NULL, 65000),
(7, 'Bucky', 'Nour', 'ACTNG', 25000);
INSERT INTO departments
VALUES ('ADMIN', 'Administration', 1, NULL),
('ACCNT', 'Accounting', 2, 'ADMIN'),
('HDWRE', 'Hardware', 3, 'CNSLT'),
('CNSLT', 'Consulting', 4, 'ADMIN'),
('ACTNG', 'Bug fixing', 7, 'ADMIN');
INSERT INTO projects
VALUES
('EMPHAPPY', 'ADMIN', 'Employee Moral', '2002-03-14', NULL, 0),
('ADT4MFIA', 'ACCNT', 'Mofia Audit', '2003-07-03', '2003-11-30', 100000),
('ROBOSPSE', 'CNSLT', 'Robotic Spouse', '2002-03-14', NULL, 242000),
('DNLDCLNT', 'CNSLT', 'Download Client', '2005-02-03', NULL, 18150),
('BOBBYFUN', 'ACTNG', 'Bug fixing', '2024-02-14', NULL, 17990);
INSERT INTO works_on
VALUES (2, 'ADT4MFIA', 0.50),
(3, 'ROBOSPSE', 0.75),
(4, 'ROBOSPSE', 0.75),
(5, 'ROBOSPSE', 0.50),
(5, 'ADT4MFIA', 0.60),
(3, 'DNLDCLNT', 0.25);
SELECT employee_id
FROM works_on
WHERE 1.68 * assigned_time > 20;
while you mention that you're looking for person > 20hr per week, you don't give us information about what the "assigned_time" is in reference to. Is that a percentage of each day, week, month? With the values you have now, you can see that none of these individuals is close to a value of '20' if you just add your math to the select statement.
SELECT employee_id, (assigned_time * 1.68) as'worked'
FROM works_on
employee_id worked
2 0.8400
3 0.4200
3 1.2600
4 1.2600
5 1.0080
5 0.8400
I imagine you're looking for the sum of same employee id's working multiple times, which would be more along the lines of
SELECT employee_id, sum(assigned_time * 1.68) as'worked'
FROM works_on
group by employee_id
employee_id worked
2 0.8400
3 1.6800
4 1.2600
5 1.8480
This question is really confusing me. They didn't provide enough details of it. Whatever they have provided I have written below.
job_id: unique identifier of jobs
actor_id: unique identifier of actor
event: decision/skip/transfer
language: language of the content
time_spent: time spent to review the job in seconds
org: organization of the actor
ds: date in the yyyy/mm/dd format. It is stored in the form of text and we use presto to run. no need for date function
CREATE TABLE job_data
(
ds DATE,
job_id INT NOT NULL,
actor_id INT NOT NULL,
event VARCHAR(15) NOT NULL,
language VARCHAR(15) NOT NULL,
time_spent INT NOT NULL,
org CHAR(2)
);
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');
Below is the data. Points to be considered :
What does the event mean? What to consider for reviewing?
And here's the query I've tried:
SELECT ds, COUNT(*)/24 AS no_of_job
FROM job_data
WHERE ds BETWEEN '2020-11-01' AND '2020-11-30'
GROUP BY ds;
Check below approach if it is what you are looking for.
select ds,count(job_id) as jobs_per_day, sum(time_spent)/3600 as hours_spent
from job_data
where ds >='2020-11-01' and ds <='2020-11-30'
group by ds ;
Demo MySQL 5.6: https://www.db-fiddle.com/f/7yUJcuMJPncBBnrExKbzYz/26
Demo MySQL 8.0.26: https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=83e89a2ad2a7e73b7ca990ac36ae4df0
The difference between the demos as #FaNo_FN pointed out in the comments is: in MySQL 8.0.26version it will provide an error if date 2020-11-31 it is used, because there is no 31 Novembre.
Use and condition instead of between , it performs faster.
You need to sum the time_spent for the day.
I need to calculat fin_sum for visit_numbers and industry_code, but if id in other table then i need to exclude records which have id in other table from calculation but, if id is not esixst in table themn it should be included.
I have table with next structure
create temporary table client_transactions_final
(
id int,
fin_amount decimal (6,2),
ind_code varchar(10),
visit_number int
);
insert into client_transactions_final values
(1, 100, 'Ind 1', 2),
(1, 300, 'Ind 2', 3),
(2, 300, 'Ind 3', 4),
(2, 100, 'Ind 1', 2),
(3, 300, 'Ind 2', 3),
(4, 300, 'Ind 3', 5),
(5, 100, 'Ind 1', 2),
(6, 300, 'Ind 2', 5),
(6, 300, 'Ind 3', 4)
create temporary table term_map
(
id int
);
insert into term_map values
(2),
(4);
from this table i am runing select which do sum groyp by visit_numbers and industry code
SELECT visit_number,
case when id in (SELECT id FROM term_map) then
--sum(fin_amount) do not include ids from SELECT id FROM term_map into
else
SUM(fin_amount) end revenue
FROM
client_transactions_final
GROUP BY visit_number , ind_code
Howewer i need to calculat fin_sum for visit_numbers and industry_code, but if id in other table then i need to exclude records which have id in other table from calculation but, if id is not esixst in table themn it should be included. I alreade try difrentaproaches on how to do it but non of it is working any ideas how to do it ?
Try this:-
SELECT visit_number,
SUM(fin_amount) revenue
FROM
client_transactions_final
where id not in (SELECT id FROM term_map)
GROUP BY visit_number , ind_code
How can I get a table of the top 3 rated users per project type per company?
Here is my schema:
contact users
-------------------------- ----------------
id id
Contact_Title Name_Title
Contact_First Name_First
Contact_Middle Name_Middle
Contact_Last Name_Last
Lead_Referral_Source Email
Date_of_Initial_Contact Password
Title User_Roles
Company User_Status
Industry
Address
Address_Street_1
Address_Street_2
Address_City
Address_State
Address_Zip
Address_Country
Phone
Email
Status
Website
LinkedIn_Profile
Background_Info
Sales_Rep
Rating
Project_Type
Project_Description
Proposal_Due_Date
Budget
Deliverables
I am just a beginner when it comes to SQL and am having a hard time with the logic. I haven't been able to make any progress with this question and would love to see how you guys might attempt it and hear some suggestions. Thanks so much for your help :)
I couldn't find a way to do it with a single query, but here is a stored procedure that will create a table in which to store the results, then populate that table with the results you need:
DROP PROCEDURE IF EXISTS Get_Top_3_Rated_Salespeople_Per_Company_and_Project_Type;
DELIMITER //
CREATE PROCEDURE IF NOT EXISTS Get_Top_3_Rated_Salespeople_Per_Company_and_Project_Type()
BEGIN
DECLARE ct int;
DECLARE comboCT int;
DECLARE lt int;
/* Create table where results will be stored */
/* WARNING: Drops existing table first! */
DROP TABLE IF EXISTS `Top_3_Rated_Salespeople_Per_Company_and_Project_Type`;
CREATE TABLE IF NOT EXISTS `Top_3_Rated_Salespeople_Per_Company_and_Project_Type` (
Name_First varchar(30),
Name_Middle varchar(30),
Name_Last varchar(100),
Email varchar(100),
Rating numeric(6,4),
Company varchar(100),
Project_Type varchar(100)
);
/* Create temp table to store distinct Company/Project_Type combinations */
CREATE TEMPORARY TABLE IF NOT EXISTS temp_distinct_combos
SELECT Project_Type, Company FROM contact GROUP BY Project_Type, Company;
SET ct = 0;
SET comboCT = (SELECT COUNT(*) FROM temp_distinct_combos);
/* For each combination in temp table, get top three ratings with associated user data
/* and inset into Top_3_Rated_Salespeople_Per_Company_and_Project_Type */
WHILE ct <= (comboCT + 1) DO
SET lt = 1;
INSERT INTO
`Top_3_Rated_Salespeople_Per_Company_and_Project_Type`
SELECT
u.Name_First,
u.Name_Middle,
u.Name_Last,
u.Email,
c.Rating,
c.Company,
c.Project_Type
FROM
contact as c
LEFT OUTER JOIN
users AS u
ON
u.id = c.Sales_Rep
WHERE
/* Company/Project_Type combination matches current record in temp_distinct_combos */
CONCAT(c.Company, c.Project_Type) = (
SELECT
CONCAT(Company, Project_Type)
FROM
temp_distinct_combos
LIMIT
ct, lt /* this will limit this subquery to returning only one row at a time */
)
ORDER BY
c.Company,
c.Project_Type,
c.Rating DESC
LIMIT 3;
SET ct = ct + 1;
END WHILE;
END //
DELIMITER ;
Code I used to setup test (note: you should be including this stuff when you post your question; most people around won't go this far to help you):
DROP TABLE IF EXISTS contact;
CREATE TABLE IF NOT EXISTS contact (
id int,
Contact_Title varchar(40),
Contact_First varchar(40),
Contact_Middle varchar(40),
Contact_Last varchar(40),
Lead_Referral_Source varchar(40),
Date_of_Initial_Contact varchar(40),
Title varchar(40),
Company varchar(40),
Industry varchar(40),
Address varchar(40),
Address_Street_1 varchar(40),
Address_Street_2 varchar(40),
Address_City varchar(40),
Address_State varchar(40),
Address_Zip varchar(40),
Address_Country varchar(40),
Phone varchar(40),
Email varchar(40),
Status varchar(40),
Website varchar(40),
LinkedIn_Profile varchar(40),
Background_Info varchar(40),
Sales_Rep int,
Rating numeric(6,4),
Project_Type varchar(40),
Project_Description varchar(40),
Proposal_Due_Date varchar(40),
Budget varchar(40),
Deliverables varchar(40)
);
DROP TABLE IF EXISTS users;
CREATE TABLE IF NOT EXISTS users (
id int,
Name_Title varchar(40),
Name_First varchar(40),
Name_Middle varchar(40),
Name_Last varchar(40),
Email varchar(40),
Password varchar(40),
User_Roles varchar(40),
User_Status varchar(40)
);
INSERT INTO
users
(id, Name_First, Name_Last, Email)
VALUES
(1, 'Name_First_1', 'Name_Last_1', 'Email_1'),
(2, 'Name_First_2', 'Name_Last_2', 'Email_2'),
(3, 'Name_First_3', 'Name_Last_3', 'Email_3'),
(4, 'Name_First_4', 'Name_Last_4', 'Email_4'),
(5, 'Name_First_5', 'Name_Last_5', 'Email_5'),
(6, 'Name_First_6', 'Name_Last_6', 'Email_6'),
(7, 'Name_First_7', 'Name_Last_7', 'Email_7'),
(8, 'Name_First_8', 'Name_Last_8', 'Email_8'),
(9, 'Name_First_9', 'Name_Last_9', 'Email_9'),
(10, 'Name_First_10', 'Name_Last_10', 'Email_10'),
(11, 'Name_First_11', 'Name_Last_11', 'Email_11');
INSERT INTO
contact
(id, Contact_First, Contact_Last, Sales_Rep, Rating, Project_Type, Company)
VALUES
(1, 'Contact_First_1', 'Contact_Last_1', 1, 10, 'Type_1', 'Company_1'),
(2, 'Contact_First_2', 'Contact_Last_2', 2, 9.9, 'Type_1', 'Company_1'),
(3, 'Contact_First_3', 'Contact_Last_3', 3, 9.8, 'Type_1', 'Company_1'),
(4, 'Contact_First_4', 'Contact_Last_4', 4, 9.7, 'Type_1', 'Company_1'),
(5, 'Contact_First_5', 'Contact_Last_5', 5, 9.6, 'Type_1', 'Company_1'),
(6, 'Contact_First_6', 'Contact_Last_6', 6, 9.5, 'Type_2', 'Company_1'),
(7, 'Contact_First_7', 'Contact_Last_7', 7, 9.4, 'Type_2', 'Company_1'),
(8, 'Contact_First_8', 'Contact_Last_8', 8, 9.3, 'Type_2', 'Company_1'),
(9, 'Contact_First_9', 'Contact_Last_9', 9, 9.2, 'Type_2', 'Company_1'),
(10, 'Contact_First_10', 'Contact_Last_10', 10, 9.1, 'Type_2', 'Company_1'),
(11, 'Contact_First_11', 'Contact_Last_11', 11, 9, 'Type_2', 'Company_1'),
(12, 'Contact_First_12', 'Contact_Last_12', 1, 10, 'Type_1', 'Company_2'),
(13, 'Contact_First_13', 'Contact_Last_13', 2, 9.9, 'Type_1', 'Company_2'),
(14, 'Contact_First_14', 'Contact_Last_14', 3, 9.8, 'Type_1', 'Company_2'),
(15, 'Contact_First_15', 'Contact_Last_15', 4, 9.7, 'Type_1', 'Company_2'),
(16, 'Contact_First_16', 'Contact_Last_16', 5, 9.6, 'Type_2', 'Company_2'),
(17, 'Contact_First_17', 'Contact_Last_17', 6, 9.5, 'Type_2', 'Company_2'),
(18, 'Contact_First_18', 'Contact_Last_18', 7, 9.4, 'Type_2', 'Company_2'),
(19, 'Contact_First_19', 'Contact_Last_19', 8, 9.3, 'Type_2', 'Company_2'),
(20, 'Contact_First_20', 'Contact_Last_20', 9, 9.2, 'Type_2', 'Company_2'),
(21, 'Contact_First_21', 'Contact_Last_21', 10, 9.1, 'Type_2', 'Company_2'),
(22, 'Contact_First_22', 'Contact_Last_22', 11, 9, 'Type_2', 'Company_2'),
(23, 'Contact_First_23', 'Contact_Last_23', 1, 10, 'Type_1', 'Company_3'),
(24, 'Contact_First_24', 'Contact_Last_24', 2, 9.9, 'Type_1', 'Company_3'),
(25, 'Contact_First_25', 'Contact_Last_25', 3, 9.8, 'Type_2', 'Company_3'),
(26, 'Contact_First_26', 'Contact_Last_26', 4, 9.7, 'Type_2', 'Company_3'),
(27, 'Contact_First_27', 'Contact_Last_27', 5, 9.6, 'Type_2', 'Company_3'),
(28, 'Contact_First_28', 'Contact_Last_28', 6, 9.5, 'Type_2', 'Company_3'),
(29, 'Contact_First_29', 'Contact_Last_29', 7, 9.4, 'Type_2', 'Company_3'),
(30, 'Contact_First_30', 'Contact_Last_30', 8, 9.3, 'Type_2', 'Company_3');
If this solves your problem, please remember to mark as answered, so everyone will know you no longer need help (and so I'll get rewarded for the two hours I spent helping you :-). If this doesn't meet your needs, please describe in as much detail as possible how it differs from your desired results and I'll try to revise.
I'm new to mysql, and can not figure out why this error keeps coming up. It's a simple table and I want id to be 1, 2, 3, 4 etc. alongside two other columns. Why does it keep reading, column count doesn't match value count at row 1?
CREATE DATABASE thedatabase;
USE thedatabase;
CREATE TABLE cars (
id INTEGER AUTO_INCREMENT,
model INTEGER NOT NULL,
mileage INTEGER NOT NULL,
PRIMARY KEY (id)
);
INSERT INTO thedatabase.cars (
model,
mileage
) VALUES (
(45, 34598),
(22, 23847),
(10, 3847),
(487, 93229),
(237, 238975),
(23, 23987),
(34, 3498),
(57, 34984),
(56, 34983),
(20, 9845);
You have got an extra opening bracked in INSERT statement, after VALUES below should work fine:
INSERT INTO thedatabase.cars ( model,
mileage ) VALUES (45, 34598), (22, 23847), (10, 3847), (487, 93229), (237, 238975), (23, 23987), (34, 3498), (57, 34984), (56, 34983), (20, 9845);