Related
I have 3 tables: invoice order, and salesteam. I want to create a query to determine the total order value for each sales representative in each company.
I am receiving an error when running my query. Could someone provide some suggestions on how to troubleshoot? Thank you!
invoice:
Order_Id,Date,Meal_Id,Company_Id,Date_of_Meal,Participants,Meal_Price,Type_of_Meal
839FKFW2LLX4LMBB,27-05-2016,INBUX904GIHI8YBD,LJKS5NK6788CYMUU,2016-05-31 07:00:00+02:00,['David Bishop'],469,Breakfast
97OX39BGVMHODLJM,27-09-2018,J0MMOOPP709DIDIE,LJKS5NK6788CYMUU,2018-10-01 20:00:00+02:00,['David Bishop'],22,Dinner
041ORQM5OIHTIU6L,24-08-2014,E4UJLQNCI16UX5CS,LJKS5NK6788CYMUU,2014-08-23 14:00:00+02:00,['Karen Stansell'],314,Lunch
YT796QI18WNGZ7ZJ,12-04-2014,C9SDFHF7553BE247,LJKS5NK6788CYMUU,2014-04-07 21:00:00+02:00,['Addie Patino'],438,Dinner
6YLROQT27B6HRF4E,28-07-2015,48EQXS6IHYNZDDZ5,LJKS5NK6788CYMUU,2015-07-27 14:00:00+02:00,['Addie Patino' 'Susan Guerrero'],690,Lunch
AT0R4DFYYAFOC88Q,21-07-2014,W48JPR1UYWJ18NC6,LJKS5NK6788CYMUU,2014-07-17 20:00:00+02:00,['David Bishop' 'Susan Guerrero' 'Karen Stansell'],181,Dinner
2DDN2LHS7G85GKPQ,29-04-2014,1MKLAKBOE3SP7YUL,LJKS5NK6788CYMUU,2014-04-30 21:00:00+02:00,['Susan Guerrero' 'David Bishop'],14,Dinner
FM608JK1N01BPUQN,08-05-2014,E8WJZ1FOSKZD2MJN,36MFTZOYMTAJP1RK,2014-05-07 09:00:00+02:00,['Amanda Knowles' 'Cheryl Feaster' 'Ginger Hoagland' 'Michael White'],320,Breakfast
orders:
Order_Id,Company_Id,Company_Name,Date,Order_Value,Converted
80EYLOKP9E762WKG,LJKS5NK6788CYMUU,Chimera-Chasing Casbah,18-02-2017,4875,1
TLEXR1HZWTUTBHPB,LJKS5NK6788CYMUU,Chimera-Chasing Casbah,30-07-2015,8425,0
839FKFW2LLX4LMBB,LJKS5NK6788CYMUU,Chimera-Chasing Casbah,27-05-2016,4837,0
97OX39BGVMHODLJM,LJKS5NK6788CYMUU,Chimera-Chasing Casbah,2018-09-27,343,0
5T4LGH4XGBWOD49Z,LJKS5NK6788CYMUU,Chimera-Chasing Casbah,2016-01-14,983,0
041ORQM5OIHTIU6L,LJKS5NK6788CYMUU,Chimera-Chasing Casbah,2014-08-24,4185,0
8QUW0UXQ3XHIL56W,LJKS5NK6788CYMUU,Chimera-Chasing Casbah,2018-09-06,3186,0
salesteam:
Sales_Rep, Sales_Rep_Id, Company_Name, Company_Id
Jessie Mcallister,97UNNAT790E0WM4N,Chimera-Chasing Casbah,LJKS5NK6788CYMUU
Jessie Mcallister,97UNNAT790E0WM4N,Two-Mile Grab,H3JRC7XX7WJAD4ZO
Jessie Mcallister,97UNNAT790E0WM4N,Three-Men-And-A-Helper Congo'S,HB25MDZR0MGCQUGX
Jessie Mcallister,97UNNAT790E0WM4N,Paleocortical Boatloads,NUQS9SHQH6IU92V8
Jessie Mcallister,97UNNAT790E0WM4N,Editorial Paintbrush,PQ79N68UEQ9FFCPU
Jessie Mcallister,97UNNAT790E0WM4N,Victorian Aim,93DU98KT3NZCOW58
Jessie Mcallister,97UNNAT790E0WM4N,Industrial Opinions,BQMPJF0W2Z2E0PEW
Lois Bowers,RRD2R9XMAJDP7TUY,Fantastic Re-Enactments,W2X6NP1JBOKWCO33
Lois Bowers,RRD2R9XMAJDP7TUY,Infamous Inoculation,D459BZ8Z7N1KAFGU
Lois Bowers,RRD2R9XMAJDP7TUY,Simple-Seeming Tenure,MR6NETSKD2PSN54L
Query:
SELECT s.Sales_Rep, SUM(o.Order_Value) AS Total_Order_Value
FROM salesteam s
JOIN orders o ON s.Company_Id = o.Company_Id
JOIN invoice i ON o.Order_Id = i.Order_Id
WHERE DISTINCT s.Company_Name
GROUP BY s.Sales_Rep;
Error Message:
ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DISTINCT s.Company_Name\n GROUP BY s.Sales_Rep' at line 6")
What you have tried to do is close to what you need, the issue in your code is the WHERE DISTINCT s.Company_Name you cannot use DISTINCT keyword in the WHERE clause.
Schema (MySQL v8.0)
CREATE TABLE invoice
(
Order_Id VARCHAR(512),
Date VARCHAR(512),
Meal_Id VARCHAR(512),
Company_Id VARCHAR(512),
Date_of_Meal VARCHAR(512),
Participants VARCHAR(512),
Meal_Price INT,
Type_of_Meal VARCHAR(512)
);
INSERT INTO invoice (Order_Id, Date, Meal_Id, Company_Id, Date_of_Meal, Participants, Meal_Price, Type_of_Meal) VALUES ('839FKFW2LLX4LMBB', '27-05-2016', 'INBUX904GIHI8YBD', 'LJKS5NK6788CYMUU', '2016-05-31 07:00:00+02:00', '[\'David Bishop\']', '469', 'Breakfast');
INSERT INTO invoice (Order_Id, Date, Meal_Id, Company_Id, Date_of_Meal, Participants, Meal_Price, Type_of_Meal) VALUES ('97OX39BGVMHODLJM', '27-09-2018', 'J0MMOOPP709DIDIE', 'LJKS5NK6788CYMUU', '2018-10-01 20:00:00+02:00', '[\'David Bishop\']', '22', 'Dinner');
INSERT INTO invoice (Order_Id, Date, Meal_Id, Company_Id, Date_of_Meal, Participants, Meal_Price, Type_of_Meal) VALUES ('041ORQM5OIHTIU6L', '24-08-2014', 'E4UJLQNCI16UX5CS', 'LJKS5NK6788CYMUU', '2014-08-23 14:00:00+02:00', '[\'Karen Stansell\']', '314', 'Lunch');
INSERT INTO invoice (Order_Id, Date, Meal_Id, Company_Id, Date_of_Meal, Participants, Meal_Price, Type_of_Meal) VALUES ('YT796QI18WNGZ7ZJ', '12-04-2014', 'C9SDFHF7553BE247', 'LJKS5NK6788CYMUU', '2014-04-07 21:00:00+02:00', '[\'Addie Patino\']', '438', 'Dinner');
INSERT INTO invoice (Order_Id, Date, Meal_Id, Company_Id, Date_of_Meal, Participants, Meal_Price, Type_of_Meal) VALUES ('6YLROQT27B6HRF4E', '28-07-2015', '48EQXS6IHYNZDDZ5', 'LJKS5NK6788CYMUU', '2015-07-27 14:00:00+02:00', '[\'Addie Patino\' \'Susan Guerrero\']', '690', 'Lunch');
INSERT INTO invoice (Order_Id, Date, Meal_Id, Company_Id, Date_of_Meal, Participants, Meal_Price, Type_of_Meal) VALUES ('AT0R4DFYYAFOC88Q', '21-07-2014', 'W48JPR1UYWJ18NC6', 'LJKS5NK6788CYMUU', '2014-07-17 20:00:00+02:00', '[\'David Bishop\' \'Susan Guerrero\' \'Karen Stansell\']', '181', 'Dinner');
INSERT INTO invoice (Order_Id, Date, Meal_Id, Company_Id, Date_of_Meal, Participants, Meal_Price, Type_of_Meal) VALUES ('2DDN2LHS7G85GKPQ', '29-04-2014', '1MKLAKBOE3SP7YUL', 'LJKS5NK6788CYMUU', '2014-04-30 21:00:00+02:00', '[\'Susan Guerrero\' \'David Bishop\']', '14', 'Dinner');
INSERT INTO invoice (Order_Id, Date, Meal_Id, Company_Id, Date_of_Meal, Participants, Meal_Price, Type_of_Meal) VALUES ('FM608JK1N01BPUQN', '08-05-2014', 'E8WJZ1FOSKZD2MJN', '36MFTZOYMTAJP1RK', '2014-05-07 09:00:00+02:00', '[\'Amanda Knowles\' \'Cheryl Feaster\' \'Ginger Hoagland\' \'Michael White\']', '320', 'Breakfast');
CREATE TABLE orders
(
Order_Id VARCHAR(512),
Company_Id VARCHAR(512),
Company_Name VARCHAR(512),
Date VARCHAR(512),
Order_Value INT,
Converted VARCHAR(512)
);
INSERT INTO orders (Order_Id, Company_Id, Company_Name, Date, Order_Value, Converted) VALUES ('80EYLOKP9E762WKG', 'LJKS5NK6788CYMUU', 'Chimera-Chasing Casbah', '18-02-2017', '4875', '1');
INSERT INTO orders (Order_Id, Company_Id, Company_Name, Date, Order_Value, Converted) VALUES ('TLEXR1HZWTUTBHPB', 'LJKS5NK6788CYMUU', 'Chimera-Chasing Casbah', '30-07-2015', '8425', '0');
INSERT INTO orders (Order_Id, Company_Id, Company_Name, Date, Order_Value, Converted) VALUES ('839FKFW2LLX4LMBB', 'LJKS5NK6788CYMUU', 'Chimera-Chasing Casbah', '27-05-2016', '4837', '0');
INSERT INTO orders (Order_Id, Company_Id, Company_Name, Date, Order_Value, Converted) VALUES ('97OX39BGVMHODLJM', 'LJKS5NK6788CYMUU', 'Chimera-Chasing Casbah', '2018-09-27', '343', '0');
INSERT INTO orders (Order_Id, Company_Id, Company_Name, Date, Order_Value, Converted) VALUES ('5T4LGH4XGBWOD49Z', 'LJKS5NK6788CYMUU', 'Chimera-Chasing Casbah', '2016-01-14', '983', '0');
INSERT INTO orders (Order_Id, Company_Id, Company_Name, Date, Order_Value, Converted) VALUES ('041ORQM5OIHTIU6L', 'LJKS5NK6788CYMUU', 'Chimera-Chasing Casbah', '2014-08-24', '4185', '0');
INSERT INTO orders (Order_Id, Company_Id, Company_Name, Date, Order_Value, Converted) VALUES ('8QUW0UXQ3XHIL56W', 'LJKS5NK6788CYMUU', 'Chimera-Chasing Casbah', '2018-09-06', '3186', '0');
CREATE TABLE salesteam
(
Sales_Rep VARCHAR(512),
Sales_Rep_Id VARCHAR(512),
Company_Name VARCHAR(512),
Company_Id VARCHAR(512)
);
INSERT INTO salesteam (Sales_Rep, Sales_Rep_Id, Company_Name, Company_Id) VALUES ('Jessie Mcallister', '97UNNAT790E0WM4N', 'Chimera-Chasing Casbah', 'LJKS5NK6788CYMUU');
INSERT INTO salesteam (Sales_Rep, Sales_Rep_Id, Company_Name, Company_Id) VALUES ('Jessie Mcallister', '97UNNAT790E0WM4N', 'Two-Mile Grab', 'H3JRC7XX7WJAD4ZO');
INSERT INTO salesteam (Sales_Rep, Sales_Rep_Id, Company_Name, Company_Id) VALUES ('Jessie Mcallister', '97UNNAT790E0WM4N', 'Three-Men-And-A-Helper CongoS', 'HB25MDZR0MGCQUGX');
INSERT INTO salesteam (Sales_Rep, Sales_Rep_Id, Company_Name, Company_Id) VALUES ('Jessie Mcallister', '97UNNAT790E0WM4N', 'Paleocortical Boatloads', 'NUQS9SHQH6IU92V8');
INSERT INTO salesteam (Sales_Rep, Sales_Rep_Id, Company_Name, Company_Id) VALUES ('Jessie Mcallister', '97UNNAT790E0WM4N', 'Editorial Paintbrush', 'PQ79N68UEQ9FFCPU');
INSERT INTO salesteam (Sales_Rep, Sales_Rep_Id, Company_Name, Company_Id) VALUES ('Jessie Mcallister', '97UNNAT790E0WM4N', 'Victorian Aim', '93DU98KT3NZCOW58');
INSERT INTO salesteam (Sales_Rep, Sales_Rep_Id, Company_Name, Company_Id) VALUES ('Jessie Mcallister', '97UNNAT790E0WM4N', 'Industrial Opinions', 'BQMPJF0W2Z2E0PEW');
INSERT INTO salesteam (Sales_Rep, Sales_Rep_Id, Company_Name, Company_Id) VALUES ('Lois Bowers', 'RRD2R9XMAJDP7TUY', 'Fantastic Re-Enactments', 'W2X6NP1JBOKWCO33');
INSERT INTO salesteam (Sales_Rep, Sales_Rep_Id, Company_Name, Company_Id) VALUES ('Lois Bowers', 'RRD2R9XMAJDP7TUY', 'Infamous Inoculation', 'D459BZ8Z7N1KAFGU');
INSERT INTO salesteam (Sales_Rep, Sales_Rep_Id, Company_Name, Company_Id) VALUES ('Lois Bowers', 'RRD2R9XMAJDP7TUY', 'Simple-Seeming Tenure', 'MR6NETSKD2PSN54L');
Query #1
SELECT S.Sales_Rep, S.Sales_Rep_Id, S.Company_Name, SUM(O.Order_Value)
FROM invoice I
JOIN orders O ON O.Order_ID = I.Order_Id AND O.Company_Id = I.Company_Id
JOIN salesteam S ON I.Company_Id = S.Company_Id
GROUP BY S.Sales_Rep, S.Sales_Rep_Id, S.Company_Name;
Sales_Rep
Sales_Rep_Id
Company_Name
SUM(O.Order_Value)
Jessie Mcallister
97UNNAT790E0WM4N
Chimera-Chasing Casbah
9365
Note: The salesteam csv data seems to have a ' so you might need to keep an eye out and handle this when you insert the data into your tables. e.g: Jessie Mcallister,97UNNAT790E0WM4N,Three-Men-And-A-Helper Congo'S,HB25MDZR0MGCQUGX
Let's say this is my data set:
CREATE TABLE orders (
`email` VARCHAR(11),
`id` INTEGER,
`year` INTEGER
);
INSERT INTO orders
(`email`, `id`, `year`)
VALUES
('foo#bar.com', '1001', '2019'),
('foo#bar.com', '1002', '2019'),
('foo#bar.com', '1003', '2019'),
('foo#bar.com', '1004', '2020'),
('foo#bar.com', '1005', '2020'),
('foo#bar.com', '1006', '2020'),
('foo#bar.com', '1007', '2021'),
('foo#bar.com', '1008', '2021'),
('foo#bar.com', '1009', '2021'),
('bar#foo.com', '1111', '2019'),
('bar#foo.com', '1112', '2019'),
('bar#foo.com', '1113', '2019'),
('bar#foo.com', '1114', '2020'),
('bar#foo.com', '1115', '2020'),
('bar#foo.com', '1116', '2021'),
('bar#foo.com', '1117', '2021'),
('bar#foo.com', '1118', '2021');
I want to know which email addresses had 3 orders (id) in the ALL of the years 2019, 2020, and 2021.
I have this query:
SELECT email,year,count(*)
FROM (SELECT distinct email,id,year
FROM orders) A
GROUP BY email,year
HAVING COUNT(*) = 3;
But what I want is just to return foo#bar.com since it's the only email that meets the condition of having 3 orders in each of the years.
You need to count twoce first get all email and years that have 3 orders and then count the years that have 3 orders
For Mysql 8
WITH CTE AS
( SELECT `email`, `year`
FROM orders
WHERE `year` IN(2019,2020,2021)
GROUP BY `email`, `year`
HAVINg COUNT(*) = 3)
SELECT `email` FROM CTE GROUP BY `email` HAVING Count(*) = 3
email
foo#bar.com
for Mysql 5.x
SELECT `email` FROM
( SELECT `email`, `year`
FROM orders
WHERE `year` IN(2019,2020,2021)
GROUP BY `email`, `year`
HAVINg COUNT(*) = 3) CTE GROUP BY `email` HAVING Count(*) = 3
email
foo#bar.com
fiddle
I am trying to write a query where I need to get
A list containing the name (surname and first name) of any employee that has picked any product(s) for a stock request.
I am confused about this as all this information is scattered amongst different tables.
The contents of my tables are as follows;
Picking_List (warehouseID, locationID, productNum, requestNum, quantityPicked, datePicked, pickerStaffID)
INSERT INTO Picking_List (warehouseID, locationID, productNum, requestNum, quantityPicked, datePicked, pickerStaffID)
VALUES ('1004','123','10','154','2','10/10/2021','4561');
INSERT INTO Picking_List (warehouseID, locationID, productNum, requestNum, quantityPicked, datePicked, pickerStaffID)
VALUES ('1004','21','143','283','1','07/07/2021','1832');
INSERT INTO Picking_List (warehouseID, locationID, productNum, requestNum, quantityPicked, datePicked, pickerStaffID)
VALUES ('1004','84','25','321','4','12/02/2020','3313');
INSERT INTO Picking_List (warehouseID, locationID, productNum, requestNum, quantityPicked, datePicked, pickerStaffID)
VALUES ('1004','76','130','254','8','02/02/2019','2121');
INSERT INTO Picking_List (warehouseID, locationID, productNum, requestNum, quantityPicked, datePicked, pickerStaffID)
VALUES ('1004','98','223','78','3','06/04/2021','1020');
Employee (StaffID, surname, firstName, dob, street, city, state, postcode, salary , WarehouseID, SupervisedBy)
INSERT INTO Employee (staffID,surname, firstName, dob, street, city, state, postcode, salary, warehouseID, supervisedBy)
VALUES ('4561', 'Snow', 'John', '30/12/1995', 'vulture', 'Brisbane', 'QLD', '4010', '60,000', '1004', 'Nosheen');
INSERT INTO Employee (staffID,surname, firstName, dob, street, city, state, postcode, salary, warehouseID, supervisedBy)
VALUES ('1832', 'Turner', 'Mike', '01/07/2000', 'vulture', 'Brisbane', 'QLD', '4010', '66,000', '1004', 'Nosheen');
INSERT INTO Employee (staffID,surname, firstName, dob, street, city, state, postcode, salary, warehouseID, supervisedBy)
VALUES ('3313', 'Smith', 'Jake', '06/06/1996', 'vulture', 'Brisbane', 'QLD', '4010', '55,000', '1004', 'Nosheen');
INSERT INTO Employee (staffID,surname, firstName, dob, street, city, state, postcode, salary, warehouseID, supervisedBy)
VALUES ('2121', 'Jones', 'Sally', '26/04/1984', 'vulture', 'Brisbane', 'QLD', '4010', '78,000', '1004', 'Nosheen');
INSERT INTO Employee (staffID,surname, firstName, dob, street, city, state, postcode, salary, warehouseID, supervisedBy)
VALUES ('1020', 'Wilson', 'Andrew', '11/03/1980', 'vulture', 'Brisbane', 'QLD', '4010', '100,000', '1004', 'Nosheen');
Stock_Request (requestNum, requestDate, clientNum )
INSERT INTO Stock_Request (requestNum,requestDate, clientNum)
VALUES ('123','10/12/2021','1234');
INSERT INTO Stock_Request (requestNum,requestDate, clientNum)
VALUES ('141','21/08/2020','134');
INSERT INTO Stock_Request (requestNum,requestDate, clientNum)
VALUES ('15','05/02/2021','434');
INSERT INTO Stock_Request (requestNum,requestDate, clientNum)
VALUES ('223','11/03/2019','472');
INSERT INTO Stock_Request (requestNum,requestDate, clientNum)
VALUES ('535','10/10/2020','1402');
Request_List (requestNum , productNum, qtyRequested )
INSERT INTO Request_List (requestNum, productNum, qtyRequested)
VALUES ('525','232','2');
INSERT INTO Request_List (requestNum, productNum, qtyRequested)
VALUES ('111','104','1');
INSERT INTO Request_List (requestNum, productNum, qtyRequested)
VALUES ('145','32','5');
INSERT INTO Request_List (requestNum, productNum, qtyRequested)
VALUES ('674','100','10');
INSERT INTO Request_List (requestNum, productNum, qtyRequested)
VALUES ('5','22','4';
so far this is what I have got.
SELECT C.surname,
C.firstName, CA.pickerStaffID
FROM employee AS C INNER JOIN Picking_List AS CA
ORDER BY employee ASC;
I am unsure if I have included everything I need in my query as per the task. Any help or advice would be appreciated as I am relatively new to SQL.
Try this:
SELECT
concat(e.surname, ', ', e.firstName) as fullName
FROM
Picking_List pl
INNER JOIN
Employee e ON e.StaffID = pl.pickerStaffID
WHERE
pl.requestNum = :RequestNumberToLookFor
:RequestNumberToLookFor is the request Number of the request you are looking for. Use appropriate parameterization syntax for your code.
I have a table with few fields like id, country, ip, created_at. Then I am trying to get the deltas between total entry of one day and total entry of the next day.
CREATE TABLE session (
id int NOT NULL AUTO_INCREMENT,
country varchar(50) NOT NULL,
ip varchar(255),
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id)
);
INSERT INTO `session` (`id`, `country`, `ip`, `created_at`) VALUES
('1', 'IN', '10.100.102.11', '2021-04-05 20:26:02'),
('2', 'IN', '10.100.102.11', '2021-04-05 19:26:02'),
('3', 'US', '10.120.102.11', '2021-04-17 10:26:02'),
('4', 'US', '10.100.112.11', '2021-04-16 12:26:02'),
('5', 'AU', '10.100.102.122', '2021-04-12 19:36:02'),
('6', 'AU', '10.100.102.122', '2021-04-12 18:20:02'),
('7', 'AU', '10.100.102.122', '2021-04-12 23:26:02'),
('8', 'US', '10.100.102.2', '2021-04-16 21:33:01'),
('9', 'AU', '10.100.102.122', '2021-04-18 20:46:02'),
('10', 'AU', '10.100.102.111', '2021-04-04 13:19:12'),
('11', 'US', '10.100.112.11', '2021-04-16 12:26:02'),
('12', 'IN', '10.100.102.11', '2021-04-05 15:26:02'),
('13', 'IN', '10.100.102.11', '2021-04-05 19:26:02');
Now I have written this query to get the delta
SELECT T1.date1 as date, IFNULL(T1.cnt1-T2.cnt2, T1.cnt1) as delta from (
select TA.dateA as date1, MAX(TA.countA) as cnt1 from (
select DATE(created_at) AS dateA, COUNT(*) AS countA
FROM session
GROUP BY DATE(created_at)
UNION
select DISTINCT DATE(DATE(created_at)+1) AS dateA, 0 AS countA
FROM session
) as TA
group by TA.dateA
) as T1
LEFT OUTER JOIN (
select DATE(DATE(created_at)+1) AS date2,
COUNT(*) AS cnt2
FROM session
GROUP BY DATE(created_at)
) as T2
ON T1.date1=T2.date2
ORDER BY date;
http://sqlfiddle.com/#!9/4f5fd26/60
Then I am getting the results as
date delta
2021-04-04 1
2021-04-05 3
2021-04-06 -4
2021-04-12 3
2021-04-13 -3
2021-04-16 3
2021-04-17 -2
2021-04-18 0
2021-04-19 -1
Now, is there any place of improvements/optimizes on it with/or window functions? (I am zero with SQL, still playing around).
Try a shorter version
with grp as (
SELECT t.dateA, SUM(t.cnt) AS countA
FROM session,
LATERAL (
select DATE(created_at) AS dateA, 1 as cnt
union all
select DATE(DATE(created_at)+1), 0 as cnt
) t
GROUP BY dateA
)
select t1.dateA as date, IFNULL(t1.countA-t2.countA, t1.countA) as delta
from grp t1
left join grp t2 on DATE(t2.dateA + 1) = t1.dateA
order by t1.dateA
db<>fiddle
I'm trying to recreate in mysql something created in MS SQL. I'm having a heck of time getting the syntax right. Does anyone know what the equivalent mysql query would be for the following:
create table #tmp
(id int, Ran varchar(10), Result int, ref_id int)
insert #tmp values (1, 'Object1', 4.0, 1)
insert #tmp values (2, 'Object2', 100, 1)
insert #tmp values (3, 'Object1', 6.0, 2)
insert #tmp values (4, 'Object3', 89.0, 2)
select * from #tmp
Select t.ref_id
,TK = max(case when t.Ran ='Object1' then t.[Result] end)
,CRP= max(case when t.Ran ='Object2' then t.[Result] end)
,HPT= max(case when t.Ran = 'Object3' then t.[Result] end)
From #tmp t
group by t.ref_id
Thank you for taking a look!
This doesn't seem difficult:
create temporary table tmp (
id int,
Ran varchar(10),
Result int,
ref_id int
);
insert into tmp(id, Ran, Result, ref_id) values (1, 'Object1', 4.0, 1);
insert into tmp(id, Ran, Result, ref_id) values (2, 'Object2', 100, 1);
insert into tmp(id, Ran, Result, ref_id) values (3, 'Object1', 6.0, 2);
insert into tmp(id, Ran, Result, ref_id) values (4, 'Object3', 89.0, 2);
select * from tmp;
Select t.ref_id,
max(case when t.Ran ='Object1' then t.Result end) as TK,
max(case when t.Ran ='Object2' then t.Result end) as CRP,
max(case when t.Ran = 'Object3' then t.Result end) as HPT
From tmp t
group by t.ref_id;
Here is a pretty close SQL Fiddle.
MySQL equivalent query for above sql query:
create table #tmp
(id int, Ran varchar(10), Result int, ref_id int);
insert into #tmp values (1, 'Object1', 4.0, 1);
insert into #tmp values (2, 'Object2', 100, 1);
insert into #tmp values (3, 'Object1', 6.0, 2);
insert into #tmp values (4, 'Object3', 89.0, 2);
select * from #tmp;
Select t.ref_id
,TK = max(case when t.Ran ='Object1' then t.Result end)
,CRP= max(case when t.Ran ='Object2' then t.Result end)
,HPT= max(case when t.Ran ='Object3' then t.Result end)
from #tmp t
group by t.ref_id;