(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
I want it to show all of the JK Rowling Harry Potter series books but for some reason it only show the first one. How do I fix this? It seems there is a bug in the select statement but i cant seem to find out what it is. Can some one help? I am just gonna type random words in quotations because stackoverflow says the my post is mostly code and i need to add more details.
create table authors (id integer primary key autoincrement, fullname TEXT, language TEXT, birthdate integer, deathdate integer, books integer, notes text);
insert into authors (fullname, language, birthdate, deathdate, books) values ("William Shakespeare", "English", 1564, 1616, 39);
insert into authors (fullname, language, birthdate, books) values ("J. K. Rowling", "English", 1965, 15);
insert into authors (fullname, language, birthdate, books, notes) values ("Gordon Korman", "English", 1963, 82, "Only favorite books");
create table books (author_id integer, name text, publish_year integer);
insert into books VALUES (1, "The Two Gentlemen of Verona", 1589);
insert into books VALUES (1, "The Comedy of Errors", 1589);
insert into books VALUES (1, "Titus Andronicus", 1591);
insert into books VALUES (1, "The Taming of The Shrew", 1593);
insert into books VALUES (1, "Love Labours Lost", 1589);
insert into books VALUES (1, "Romeo and Juliet", 1594);
insert into books VALUES (1, "A Midsummer's Night's Dream", 1595);
insert into books VALUES (1, "King John", 1595);
insert into books VALUES (1, "The Merchant of Venice", 1596);
insert into books VALUES (1, "The Merry Wives of Windsor", 1597);
insert into books VALUES (1, "Much Ado About Nothing", 1598);
insert into books VALUES (1, "As You Like It", 1599);
insert into books VALUES (1, "Julius Caesar", 1599);
insert into books VALUES (1, "Troilus and Cressida", 1600);
insert into books VALUES (1, "Hamlet", 1600);
insert into books VALUES (1, "Twelfth Night or What You Will", 1601);
insert into books VALUES (1, "All's Well That Ends Well", 1603);
insert into books VALUES (1, "Measure for Meausre", 1603);
insert into books VALUES (1, "Othello", 1603);
insert into books VALUES (1, "Timon of Athens", 1604);
insert into books VALUES (1, "King Lear", 1605);
insert into books VALUES (1, "Macbeth", 1606);
insert into books VALUES (1, "Pericles", 1607);
insert into books VALUES (1, "Antony and Cleopatra", 1608);
insert into books VALUES (1, "Coriolanus", 1608);
insert into books VALUES (1, "Cymbeline", 1609);
insert into books VALUES (1, "The Winter's Tale", 1609);
insert into books VALUES (1, "The Tempest", 1610);
insert into books VALUES (1, "The Two Noble Kinsmen", 1611);
insert into books VALUES (1, "Cardenio", 1612);
insert into books VALUES (1, "Henry VIII", 1613);
insert into books VALUES (1, "Henry V", 1599);
insert into books VALUES (1, "Richard III", 1593);
insert into books VALUES (1, "Richard II", 1595);
insert into books VALUES (2, "Fantastic Beasts and Where to Find Them", 2001);
insert into books VALUES (2, "Quidditch Through the Ages", 2001);
insert into books VALUES (2, "The Tales of Beedle the Bard", 2008);
insert into books VALUES (2, "Hogwarts: An Incomplete & Unreliable Guide", 2016);
insert into books VALUES (2, "Short Stories from Hogwarts of Heroism, Hardship and Dangerous Hobbies", 2016);
insert into books VALUES (2, "Short Stories from Hogwarts of Power, Politics and Pesky Poltergeists", 2016);
insert into books VALUES (3, "Losing Joe's Place", 1990);
insert into books VALUES (3, "The Sixth Grade Nickname Game", 1998);
insert into books VALUES (3, "Schooled", 2007);
insert into books VALUES (3, "The Juvie Three", 2008);
insert into books VALUES (3, "Ungifted", 2012);
insert into books VALUES (3, "Restart", 2018);
insert into books VALUES (3, "Whatshisface", 2018);
insert into books VALUES (3, "The Unteachbles", 2019);
insert into books VALUES (3, "Notorius", 2020);
select authors.fullname as Author, books.name as "Name", books.publish_year as "Year Published" from books left outer join authors on books.author_id = authors.id;
create table series (id integer primary key autoincrement, series_id text, name_book text);
insert into series (series_id, name_book) VALUES("1-1", "Henry VI, Part One");
insert into series (series_id, name_book) VALUES("1-1", "Henry VI, Part Two");
insert into series (series_id, name_book) VALUES("1-1", "Henry VI, Part Three");
insert into series (series_id, name_book) VALUES("1-2", "Henry IV, Part One");
insert into series (series_id, name_book) VALUES("1-2", "Henry IV, Part Two");
insert into series (series_id, name_book) VALUES("2-1", "Harry Potter and the Philosopher's Stone");
insert into series (series_id, name_book) VALUES("2-2", "Harry Potter and the Chamber of Secrets");
insert into series (series_id, name_book) VALUES("2-3", "Harry Potter and the Priosner of Azkaban");
insert into series (series_id, name_book) VALUES("2-4", "Harry Potter and the Goblet of Fire");
insert into series (series_id, name_book) VALUES("2-5", "Harry Potter and the Order of the Phoenix");
insert into series (series_id, name_book) VALUES("2-6", "Harry Potter and the Half-Blood Prince");
insert into series (series_id, name_book) VALUES("2-7", "Harry Potter and the Deathly Hallows");
insert into series (series_id, name_book) VALUES("2-8", "Harry Potter and the Cursed Child, Parts I & II");
insert into series (series_id, name_book) VALUES("3-1", "Slacker");
insert into series (series_id, name_book) VALUES("3-1", "Level 13");
insert into series (series_id, name_book) VALUES("3-2", "Masterminds");
insert into series (series_id, name_book) VALUES("3-2", "Criminal Destiny");
insert into series (series_id, name_book) VALUES("3-2", "Payback");
insert into series (series_id, name_book) VALUES("3-3", "The Hypnotists");
insert into series (series_id, name_book) VALUES("3-3", "Memory Maze");
insert into series (series_id, name_book) VALUES("3-3", "The Dragonfly Effect");
insert into series (series_id, name_book) VALUES("3-4", "Ungifted");
insert into series (series_id, name_book) VALUES("3-4", "Supergifted");
create table series_id (author_id integer, series_id text, series_name text, books_in_series integer, start_year integer);
insert into series_id VALUES (1, "1-1", "Henry VI", 3, 1590);
insert into series_id VALUES (1, "1-2", "Henry IV", 2, 1596);
insert into series_id VALUES (2, "2-1", "Harry Potter", 8, 1997);
insert into series_id VALUES (3, "3-1", "Slacker", 2, 2016);
insert into series_id VALUES (3, "3-2", "Masterminds", 3, 2015);
insert into series_id VALUES (3, "3-3", "The Hypnotists", 3, 2013);
insert into series_id VALUES (3, "3-4", "Ungifted", 2, 2012);
select authors.fullname, series_id.series_name, series_id.books_in_series, series_id.start_year, series.id, series.name_book from series_id join authors on series_id.author_id = authors.id join series on series_id.series_id = series.series_id order by series.id;
There is only 1 join for author_id = 2, and it's on series_id = "2-1":
insert into series_id VALUES (2, "2-1", "Harry Potter", 8, 1997);
insert into series (series_id, name_book) VALUES("2-1", "Harry Potter and the Philosopher's Stone");
It looks like you are cramming multiple things into series_id, namely an actual series_id and a sequence within the series. I suggest you create separate columns for each concept.
See First Normal Form - Columns should contain only 1 value.
I am trying to run an example where I want to know the manager name of each employee but i can't see all the rows.
Table structure
CREATE TABLE IF NOT EXISTS t1 (
staff_number INTEGER PRIMARY KEY,
fname VARCHAR(20),
lname VARCHAR(30),
gender CHAR(1),
country VARCHAR(30),
manager_id INTEGER,
joining DATE)
[(10, 'piyush', 'Bansal', 'M', 'Canada',16,'2014-03-28'),
(16, 'gillbart', 'Gates', 'M', 17,'United States','1980-10-28'),
(17, 'amezaa', 'Craft', 'F', 19,'Japan','2016-03-14'),
(19, 'Marbles', 'IsaDog', 'F','United States',20, '2018-11-03'),
(12, 'Maks', 'Demin', 'M','United States',16,'2018-11-03'),
(20, 'Sean', 'Hartrich', 'M','United States',19,'2014-02-16'),
(24, "Sam", "theBrit", "M","United Kingdom",10,'2014-02-16')]
Query
SELECT e.fname ,m.fname FROM t1 e INNER JOIN t1 m ON e.manager_id = m.staff_number
Output:
('piyush', 'gillbart')
('Maks', 'gillbart')
('Marbles', 'Sean')
('Sean', 'Marbles')
('Sam', 'piyush')
The entry for Bill and Brena as employee and manager are missing here. Any clues what is happening here??
Expected Output:
('piyush', 'gillbart')
('Maks', 'gillbart')
('Marbles', 'Sean')
('Sean', 'Marbles')
('Sam', 'piyush')
('gillbart','amezza')
('amezza','Marbles')
Two lines of you script are have a mistake:
(16, 'gillbart', 'Gates' , 'M', 17 ,'United States','1980-10-28'),
(17, 'amezaa' , 'Craft' , 'F', 19 ,'Japan','2016-03-14'),
The column value for country has manager_id instead.
The following is my SQL data structure I'm trying out two queries as follows:
Select
Wrk_ID, Wrk_LastName, Skill_Desc
from
Worker, Skill
where
(UPPER(skill_desc) = UPPER('Map Designer') or
UPPER(skill_Desc) = UPPER('Game Designer'));
I'm getting twice the output that I need
select
wrk_ID, concat('Wrk_FirstName','Wrk_LastName') As FullName,
COUNT(Assign_ID) as Max_Assignment
from
assignment worker
group by
Wrk_ID, concat('Wrk_FirstName','Wrk_LastName');
In this query I'm getting the wrk_firstname and last name instead of the actual data of first and last name.
--
-- Dumping data for table `region`
--
INSERT INTO `region` (`Reg_ID`, `Reg_desc`) VALUES
('R0010', 'California'),
('R0011', 'Nevada'),
('R0012', 'Arizona'),
('R0013', 'Michigan'),
('R0014', 'New York'),
('R0015', 'Texas'),
('R0016', 'Washington'),
('R0017', 'Oregon'),
('R0018', 'Ohio'),
('R0019', 'Boston');
-- --------------------------------------------------------
--
-- Table structure for table `client`
--
INSERT INTO `client` (`Cli_ID`, `Cli_FirstName`, `Cli_LastName`, `Cli_Initial`, `Cli_StreetAddress`, `Cli_Sex`, `Cli_Phone`,`Reg_ID`) VALUES
('C0010', 'Krishneel', 'Sharma', 'V', '99 Rock Road', 'M', '844-2573', 'R0010'),
('C0011', 'Ravneel', 'Sharma', 'V', 'Cecil Street', 'M', '894-1238', 'R0011'),
('C0012', 'Brad', 'Pitt', 'W', '615 Devil Road', 'M', '894-2285', 'R0012'),
('C0013', 'Tom', 'Cruz', 'F', '615 Gorge Town', 'M', '894-2180', 'R0013'),
('C0014', 'Paul', 'Walker', 'F', '615 Gran Street', 'M', '222-1672', 'R0014'),
('C0015', 'Jeniffer','Lopez', 'B', '713 Tall Road', 'F', '442-3381','R0015'),
('C0016', 'Brown', 'James', 'G', '615 South Place', 'F', '297-1228', 'R0016'),
('C0017', 'Lady', 'Gaga', 'G', '615 Hill Road', 'F', '290-2556', 'R0017'),
('C0018', 'Cristiano', 'Ronaldo', 'G', '713 Magic Road', 'M', '382-7185', 'R0018'),
('C0019', 'Lioneil', 'Messi', 'K', '615 Well Street', 'M', '297-3809', 'R0019');
-- --------------------------------------------------------
--
-- Table structure for table `worker`
--
INSERT INTO `worker` (`Wrk_ID`, `Wrk_FirstName`, `Wrk_LastName`, `Wrk_Initial`, `Wrk_HireDate`, `Wrk_BirthDate`, `Wrk_Phone`, `Wrk_IRD_Num`, `Reg_ID`) VALUES
('W0010', 'Rhonda', 'Lewis', 'G', '2000-03-21', '1985-02-19', '855-2573', 'X993473419', 'R0010'),
('W0011', 'Rhett', 'VanDam', 'V', '2002-03-19', '1985-01-11', '994-1238', 'X993473500', 'R0011'),
('W0012', 'Anne', 'Jones', 'M', '2000-04-11', '1965-08-19', '894-9985', 'X9934735001', 'R0012'),
('W0013', 'John', 'Lange', 'P', '2000-04-12', '1975-05-17', '894-5680', 'X993473502', 'R0013'),
('W0014', 'Robert', 'Williams', 'D', '2004-05-19', '1978-03-02', '244-1672', 'X993473503', 'R0014'),
('W0015', 'Jeanine','Smith', 'K', '2005-06-19', '1988-07-04', '434-3381', 'X993473506', 'R0015'),
('W0016', 'Brown', 'James', 'G', '2006-03-19', '1982-06-28', '297-1232', 'X993473507', 'R0016'),
('W0017', 'Jorge', 'Diante', 'K', '2007-01-19', '1984-11-19', '222-2556', 'X993473508', 'R0017'),
('W0018', 'Paul', 'Wiesenbach', 'R', '2007-01-19', '1976-12-12', '389-7185', 'X993473509', 'R0018'),
('W0019', 'Leighla', 'Genkazi', 'W', '2007-01-19', '1989-04-30', '292-3809', 'X993473600', 'R0019');
-- --------------------------------------------------------
--
-- Dumping data for table `project`
--
INSERT INTO `project`(`Proj_ID`,`Proj_Desc`,`Proj_Est_Start`,`Proj_Est_Finish`,`Proj_Cost`,`Proj_Act_Start`,`Proj_Act_Finish`,`Proj_Act_Cost`,`Cli_ID`) VALUES
('P0010','Map Design','2015-04-01','2015-04-30',10000.00,'2015-04-05','2015-05-01',12000.50,'C0010'),
('P0011','Level Design','2015-03-02','2015-03-15',5000.00,'2015-03-02','2015-03-15',7500.00,'C0011'),
('P0012','Controlls Design','2015-04-20','2015-05-01',20000.00,'2015-04-21','2015-04-30',20500.00,'C0012'),
('P0013','Character Design','2015-04-15','2015-05-11',30000.00,'2015-04-15','2015-05-12',50000.00,'C0010'),
('P0014','Sound Design','2015-01-01','2015-04-30',5000.00,'2015-01-10','2015-04-30',97560.00,'C0013'),
('P0015','Game Play design','2015-03-20','2015-03-30',57000.00,'2015-03-20','2015-03-25',6500.00,'C0014'),
('P0016','Server Design','2015-04-01','2015-04-30',89000.00,'2015-04-01','2015-04-30',8900.00,'C0015'),
('P0017','Online Gamplay Design','2015-05-01','2015-05-15',10000.00,'2015-05-02','2015-05-16',10000.00,'C0016'),
('P0018','Software Design','2015-04-01','2015-04-15',15000.00,'2015-04-01','2015-04-15',14500.00,'C0019'),
('P0019','Game programming','2015-01-14','2015-05-01',125000.00,'2015-01-14','2015-05-01',125000.00,'C0019');
-- --------------------------------------------------------
--
--Dumping data for table `assignment`
--
INSERT INTO `assignment`(`Assign_ID`,`Assign_Desc`,`Assign_Est_Start`,`Assign_Est_Finish`,`Assign_Act_Start`,`Assign_Act_Finish`,`Wrk_ID`,`Proj_ID`) VALUES
('A0010','Design Game maps','2015-03-15','2015-04-20','2015-03-15','2015-04-20','W0010','P0010'),
('A0011','Design All levels of Game','2015-03-02','2015-03-15','2015-03-02','2015-03-15','W0012','P0011'),
('A0012','Design Controls For all Platforms','2015-05-01','2015-05-20','2015-05-02','2015-05-21','W0013','P0012'),
('A0013','Design All Character of Game','2015-04-05','2015-04-30','2015-04-05','2015-05-01','W0014','P0013'),
('A0014','Game music Soundtracks','2015-01-03','2015-04-25','2015-02-05','2015-04-30','W0015','P0014'),
('A0015','Virtual walk through of game','2015-03-15','2015-03-30','2015-03-15','2015-03-30','W0016','P0015'),
('A0016','MultiPlayer Game Design','2015-04-02','2015-04-28','2015-04-04','2015-04-30','W0017','P0016'),
('A0017','Online Hosting Design','2015-05-01','2015-05-16','2015-05-01','2015-05-30','W0018','P0017'),
('A0018','Game Interesction Design','2015-04-01','2015-04-15','2015-04-01','2015-04-15','W0011','P0018'),
('A0019','Game codes','2015-01-14','2015-05-05','2015-01-14','2015-05-05','W0019','P0019');
-- --------------------------------------------------------
--
----Dumping data for table `task`
--
INSERT INTO `task`(`Task_ID`,`Task_Desc`,`Task_Act_Start`,`Task_Act_Finish`,`Assign_ID`) VALUES
('T0010','Impliment Map Design','2015-03-15','2015-04-20','A0010'),
('T0011','Impliment game Levels','2015-03-02','2015-03-15','A0011'),
('T0012','Impliment Game Controls','2015-05-02','2015-05-21','A0012'),
('T0013','Impliment Game Characters','2015-02-05','2015-04-30','A0013'),
('T0014','Impliment Sound Design','2015-03-15','2015-03-30','A0014'),
('T0015','Impliment Gamplay designs','2015-04-04','2015-04-30','A0015'),
('T0016','Office Management Systems','2015-05-01','2015-05-30','A0016'),
('T0017','Impliment Multiplayer Gamplay','2015-04-01','2015-04-15','A0017'),
('T0018','Impliment Online Servers','2015-01-14','2015-05-05','A0018'),
('T0019','Impliment all Game Codes','2015-01-14','2015-05-05','A0019');
-- --------------------------------------------------------
--
----Dumping data for table `invoice`
--
INSERT INTO `invoice`(`Inv_ID`,`Inv_Amount`,`Inv_Due_Date`,`Proj_ID`) VALUES
('I0010',12000.50,'2015-04-30','P0010'),
('I0011',7500.00,'2015-03-31','P0011'),
('I0012',20500.00,'2015-04-30','P0012'),
('I0013',50000.00,'2015-05-30','P0013'),
('I0014',97560.00,'2015-05-30','P0014'),
('I0015',6500.00,'2015-04-30','P0015'),
('I0016',8900.00,'2015-05-30','P0016'),
('I0017',10000.00,'2015-05-30','P0017'),
('I0018',14500.00,'2015-04-30','P0018'),
('I0019',125000.00,'2015-05-15','P0019');
-- --------------------------------------------------------
--
----Dumping data for table `skill`
--
INSERT INTO `skill` (`Skill_ID`,`Skill_Desc`,`Skill_PayRate`) VALUES
('S0010','Map Designer',55.00),
('S0011','Level Designer',40.00),
('S0012','Game Designer',65.00),
('S0013','Graphics Designer',55.00),
('S0014','Music Producer',50.00),
('S0015','Project manager',98.50),
('S0016','Software Enginneer',50.00),
('S0017','IT Technician',40.50),
('S0018','Server Adminstrator',62.00),
('S0019','Network Enginneer',125.00);
-- --------------------------------------------------------
--
----Dumping data for table `employee_skill`
--
INSERT INTO `employee_skill`(`Skill_ID`,`Wrk_ID`) VALUES
('S0010','W0010'),
('S0011','W0011'),
('S0012','W0012'),
('S0013','W0013'),
('S0014','W0014'),
('S0015','W0015'),
('S0016','W0016'),
('S0017','W0017'),
('S0018','W0018'),
('S0019','W0019');
1) your first query need join conditions such as
where worker.wrkid= skill.wrkid and ....
2) your second query has an error, you need a comma between assignment and worker. Also join conditions are required.