Getting of list of top 3 per 2 parameters - mysql

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.

Related

How can I see the employee IDs for a project that lasts longer than 20 hours per week?

(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

SQLlite inner join query giving giving absurd results

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.

Using mysql: column count doesn't match value count at row 1

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);

Conversion failed when converting date and / or time when creating a table

So I'am making a table with a char, varchar and date. I got an error message saying "Conversion failed when converting date and / or time". If anyone can help me fix, this you got sincere thank you. :D
this is my code for creating and inserting data on my table:
Create table Employee
(EMP_NUM char(3),
EMP_LNAME varchar(15),
EMP_FNAME varchar(15),
EMP_INITIAL char(1),
EMP_HIREDATE date,
JOB_CODE char (3),
EMP_YEARS char(2))
Insert into Employee (EMP_NUM, EMP_LNAME, EMP_FNAME, EMP_INITIAL,
EMP_HIREDATE, JOB_CODE)
Values (101, 'News', 'John', 'G','08-Nov-00', 502),
(102, 'Senior', 'David', 'H','12-Jul-89', 501),
(103, 'Arbough', 'June', 'E','01-Dec-96', 500),
(104, 'Ramoras', 'Anne', 'K','15-Nov-87', 501),
(105, 'Johnson', 'Alice', 'k','01-Feb-93', 502),
(106, 'Smithfield', 'William', null, '22-Jun-04', 500),
(107, 'Alonzo', 'Maria', 'D','10-Oct-93', 500),
(108, 'Washington', 'Ralph', 'B', '22-Aug-91',501),
(109, 'Smith', 'Larry', 'W', '18-Jul-97', 501),
(110, 'Olenko', 'Gerald', 'A', '11-Dec-95', 505),
(111, 'Wabash', 'Geoff', 'B', '04-Apr-91', 506),
(112, 'Smithson', 'Darlene', 'M', '23-Oct-94', 507),
(113, 'Joenbrood', 'Delbert', 'K', '15-ov-96', 508),
(114, 'Jones', 'Annelise', null, '20-Aug-93', 508),
(115, 'Bawangi', 'Travis', 'B','25-Jan-92', 501),
(116, 'Pratt', 'Gerald', 'L','05-Mar-97', 510),
(117, 'Williamson', 'Angie', 'M', '19-Jun-96', 509),
(118, 'Frommer', 'james', 'J','04-Jan-05', 510);
and this is the complete message result :
Msg 241, Level 16, State 1, Line 11
Conversion failed when converting date and/or time from character string.
Use CONVERSION for EMP_HIREDATE column for date :
For ex :
SELECT CAST('18-Jul-97' AS DATE)
In your query :
Insert into Employee (EMP_NUM, EMP_LNAME, EMP_FNAME, EMP_INITIAL,
EMP_HIREDATE, JOB_CODE)
SELECT 101, 'News', 'John', 'G',CAST('08-Nov-00' AS DATE), 502

(SQL) Is it possible to update a field value with the value of the field in the previous row?

I have a table from which I would like the update the value of a certain colum of fields.
Basicly moving one value down and those under it should inherit the previous value of the one about them.
I wonder if this action is possible using a single SQL query.
My table:
CREATE TABLE `menu` (
`slot_index` int(2) NOT NULL,
`language_ID` int(2) NOT NULL,
`menuItem` text NOT NULL,
PRIMARY KEY (`slot_index`,`language_ID`),
KEY `language_ID` (`language_ID`)
)
The content in it:
INSERT INTO `menu` (`slot_index`, `language_ID`, `menuItem`) VALUES
(1, 1, 'Home'),
(2, 1, 'Wie zijn wij'),
(21, 1, 'Missie'),
(22, 1, 'Doelen'),
(23, 1, 'Visie'),
(24, 1, 'Test'),
(3, 1, 'Wat doen wij'),
(31, 1, 'Medische kaart voor op reis'),
(32, 1, 'Huisartsenkaart'),
(33, 1, 'Huisartsenkaart anderstaligen'),
(4, 1, 'Perskamer'),
(5, 1, 'Beheer'),
(6, 1, 'FAQ'),
(7, 1, 'Ervaringen'),
(8, 1, 'Contact'),
(81, 1, 'Disclaimer'),
(9, 1, 'Links'),
(91, 1, 'Adresgegevens')
I would like to move slot_index 5 to 9, and make the fields under it move up inheriting the value from the field above.
Is this possible with a single query at all, or should I just write a script for this?
Thanks in advance.
Wolfert
Maybe using auto increment will help for future uses.
As for the current one I doubt that this can go with single query, because you cant query the table you are trying to update.