substring_index() returns wrong result in MySQL - mysql

I've encountered a wrong return when using substring_index.
You can reproduce my data using the query below:
create schema Airport;
use Airport;
create table aircrafttype(
aircrafttypeid char(2),
aircrafttypename varchar(20),
primary key(aircrafttypeid));
create table aircraft(
aircraftid char(2),
aircraftpurdate date,
aircraftseatcap numeric(3),
aircrafttypeid char(2),
primary key(aircraftid),
foreign key(aircrafttypeid) references aircrafttype(aircrafttypeid));
create table hangar(
hangarid char(2),
hangarlocation varchar(20),
hangarstoragecap numeric(2),
primary key(hangarid));
create table serviceteam(
teamid char(2),
teamname varchar(20),
teamlevel numeric(1),
primary key(teamid));
create table service(
serviceid char(3),
servicedate date,
hangarid char(2),
aircraftid char(2),
teamid char(2),
primary key(serviceid),
foreign key(hangarid) references hangar(hangarid),
foreign key(aircraftid) references aircraft(aircraftid),
foreign key(teamid) references serviceteam(teamid));
insert into aircrafttype values ('B7','Boeing 777');
insert into aircrafttype values ('B3','Boeing 737');
insert into aircrafttype values ('B8','Boeing 787');
insert into aircrafttype values ('B6','Boeing 767');
insert into aircrafttype values ('22','Airbus 220');
insert into aircrafttype values ('31','Airbus 310');
insert into aircraft values('A1','2012-06-19',140,'B3');
insert into aircraft values('A2','2013-08-14',129,'B6');
insert into aircraft values('A3','2013-05-01',104,'B3');
insert into aircraft values('A4','2017-04-19',296,'B7');
insert into aircraft values('A5','2018-03-02',120,'B6');
insert into aircraft values('A6','2014-10-19',191,'31');
insert into aircraft values('A7','2015-10-03',198,'31');
insert into aircraft values('A8','2016-12-31',204,'22');
insert into aircraft values('A9','2017-01-01',173,'22');
insert into hangar values('H1','Sydney, NSW',7);
insert into hangar values('H2','Melbourne, VIC',22);
insert into hangar values('H3','Sydney, NSW',25);
insert into hangar values('H4','Brisbane, QLD',8);
insert into hangar values('H5','Launceston, TAS',14);
insert into serviceteam values('T1','Sydney Rabbitohs',5);
insert into serviceteam values('T2','Melbourne Rebels',3);
insert into serviceteam values('T3','Queensland Reds',5);
insert into serviceteam values('T4','Team TRU',4);
insert into service values('S1','2019-09-25','H3','A3','T1');
insert into service values('S2','2019-08-27','H2','A7','T2');
insert into service values('S3','2019-09-22','H5','A7','T4');
insert into service values('S4','2019-05-13','H5','A4','T4');
insert into service values('S5','2019-01-08','H1','A4','T1');
insert into service values('S6','2019-09-07','H4','A9','T3');
insert into service values('S7','2019-12-20','H3','A9','T1');
insert into service values('S8','2019-12-20','H4','A3','T3');
insert into service values('S9','2019-05-18','H4','A2','T3');
insert into service values('S10','2019-05-14','H3','A3','T1');
insert into service values('S11','2019-05-27','H3','A3','T1');
insert into service values('S12','2019-08-11','H3','A9','T1');
insert into service values('S13','2019-08-17','H4','A2','T3');
insert into service values('S14','2019-12-14','H4','A4','T3');
insert into service values('S15','2025-01-25','H5','A1','T4');
Now, I want to:
Print all details of the service if the aircraft has been serviced at any hangar in NSW.
So I tried using the substring_index but it gives me an empty return, which is not true. Here's the code that I used:
select s.*, hangarlocation
from service s, hangar h
where s.hangarid = h.hangarid
and SUBSTRING_INDEX(hangarlocation, ',', -1) = 'NSW';
Can someone help me point out what is wrong here?

spaceNSW (which is what is returned by the substring index) is not the same as NSW either TRIM or include a space in the test
https://www.db-fiddle.com/f/jWBxcfXZJ8HnUuGTBKYtrt/0

Assuming, location has always state name in the end, you might use "like" instead of substring
select s.*, hangarlocation
from service s,
hangar h
where s.hangarid = h.hangarid
and hangarlocation like '%, NSW';
And it is recommended to use JOIN-syntax. This helps you to avoid mistakes like unexpected cross-joins and is more readable
select s.*, hangarlocation
from service s
join hangar h
on s.hangarid = h.hangarid
where hangarlocation like '%, NSW';

Related

Looking For Most Frequent Values SQL Statement

I have a data set that looks like this:
id | Unit_Ids
1 | {"unit_ids" : ["5442","28397"]}
2 | {"unit_ids" : ["5442","3492","2290"]}
etc.
And I'm trying to find the most frequently appearing values in Unit_Ids. As in my example 5442 appears in both lines 1 and 2, it would be the most frequent value. I was just having trouble finding a good way of creating this statement.
Thank you in advanced!
EDIT: Sorry everyone I'm working with MySQL
If 2016+
Example
Declare #YourTable Table ([id] int,[Unit_Ids] varchar(max))
Insert Into #YourTable Values
(1,'{"unit_ids" : ["5442","28397"]}')
,(2,'{"unit_ids" : ["5442","3492","2290"]}')
Select top 1 value
From #YourTable A
Cross Apply OpenJSON([Unit_Ids],'$.unit_ids') R
Order By sum(1) over (partition by value) desc
Returns
value
5442
I'm assuming you are storing JSON strings in the Unit_Ids field. If you do that, you won't be able to extract or aggregate data stored in that field.
You can however create a child table and query it to get aggregated data. Ie:
-- Master table
create table parent(id number primary key);
-- List of units per parent
create table units(
id number not null,
parent_id number not null,
primary key (id, parent_id),
foreign key (parent_id) references parent(id)
);
-- Insert sample data
insert into parent values 1;
insert into parent values 2;
insert into units(parent_id, id) values(1, 5442);
insert into units(parent_id, id) values(1, 28397);
insert into units(parent_id, id) values(2, 5442);
insert into units(parent_id, id) values(2, 3492);
insert into units(parent_id, id) values(2, 2290);
-- Count the number of times a unit id is in the table
select id, count(id) from units group by id;

SQL query from a table

For my CIS class, I have SQL project, I'm still very new to SQL and trying to learn it. Any help would be greatly appreciated.
Query: Write a query to show the total value of all the orders that customers living in San Jose or Turlock have placed.
This is what I tried and giving me an error:
select SUM(price * quantity) as Revenue
FROM customer,salesorder
where customer.cno = salesorder.cno AND
zip = 95124 AND zip = 95380
AND zip = 95382
The error I'm getting is: #1054 - Unknown column 'price' in 'field list'
Table: Table
Database codes:
create table zipcode (
zip integer(5) primary key,
city varchar(30),
State varchar(20));
create table employee (
eno varchar(10) primary key,
ename varchar(30),
zip integer(5) references zipcode(zip),
hire_date date);
create table book (
bno integer(5) primary key,
bname varchar(30),
qoh integer(5) not null,
price dec(6,2) not null);
create table customer (
cno integer(5) primary key,
cname varchar(30),
street varchar(30),
zip integer(5) references zipcode(zip),
phone char(12));
create table salesOrder (
ono integer(5) primary key,
cno integer(5) references customer(cno),
eno varchar(10) references employees(Eno),
received date,
shipped date
);
create table orderLine (
ono integer(5) references salesOrder(ono),
bno integer(5) references book(bno),
quantity integer(10) not null,
primary key (ono, bno));
insert into zipcode values (98225, 'Bellingham', 'WA');
insert into zipcode values (95388, 'Winton', 'CA');
insert into zipcode values (44242, 'Stow', 'OH');
insert into zipcode values (61536, 'Hanna city', 'IL');
insert into zipcode values (01254, 'Richmond', 'MA');
insert into zipcode values (95124, 'San Jose', 'CA');
insert into zipcode values (95382, 'Turlock', 'CA');
insert into zipcode values (95380, 'Turlock', 'CA');
insert into zipcode values (98102, 'Seattle', 'WA');
insert into employee values ('P0239401', 'Jones Hoffer',98225, '2000-12-12');
insert into employee values ('P0239402', 'Jeffrey Prescott',95388, '2016-11-07');
insert into employee values ('P0239403', 'Fred NcFaddeb',95124, '2008-09-01');
insert into employee values ('P0239404', 'Karen Ives',98102, '2014-05-21');
insert into book values (10501, 'Forensic Accounting',200, 229.99);
insert into book values (10502, 'SAP Business One',159, 149.99);
insert into book values (10503, 'Fraud Cases',190, 179.99);
insert into book values (10504, 'CPA Review',65, 279.99);
insert into book values (10605, 'Quickbooks for Business',322, 59.99);
insert into book values (10704, 'Financial Accounting',129, 164.99);
insert into book values (10879, 'Managerial Accounting',155, 114.99);
insert into book values (10933, 'Cost Accounting',122, 219.99);
insert into book values (10948, 'Intermediate Accounting',123, 164.99);
insert into book values (10965, 'Accounting Information Systems',211, 259.99);
insert into book values (10988, 'XBRL in Nutshell',124, 109.99);
insert into customer values (23511, 'Michelle Kuan', '123 Main St.',98225, '360-636-5555');
insert into customer values (23512, 'George Myer', '237 Ash Ave.',95124, '312-678-5555');
insert into customer values (23513, 'Richard Gold', '111 Inwood St.',95124, '312-883-7337');
insert into customer values (23514, 'Robert Smith', '54 Gate Dr.',95388, '206-832-1221');
insert into customer values (23515, 'Christopher David', '777 Loto St.',98225, '360-458-9878');
insert into customer values (23516, 'Adam Beethoven', '234 Park Rd.',95380, '209-546-7299');
insert into customer values (23517, 'Ludwig Bach', '5790 Walnut St.',95382, '209-638-2712');
insert into customer values (23518, 'Kathleen Pedersen', '1233 Federal Ave E', 98102, '360-573-7239');
insert into salesOrder values (1020, 23511, 'P0239403', '2018-01-13', '2018-01-15');
insert into salesOrder values (1021, 23513, 'P0239401', '2018-01-13', '2018-01-16');
insert into salesOrder values (1022, 23513, 'P0239402', '2018-01-15', '2018-01-17');
insert into salesOrder values (1023, 23512, 'P0239403', '2018-01-16', '2018-01-18');
insert into salesOrder values (1024, 23511, 'P0239402', '2018-01-18', '2018-01-20');
insert into salesOrder values (1025, 23511, 'P0239403', '2018-01-29', '2017-01-31');
insert into salesOrder values (1026, 23512, 'P0239404', '2018-01-30', '2018-01-31');
insert into salesOrder values (1027, 23512, 'P0239402', '2018-01-30', '2018-01-31');
insert into salesOrder values (1028, 23512, 'P0239404', '2018-01-30', '2018-01-31');
insert into salesOrder (ONO, CNO, ENO, RECEIVED) values (1029, 23513, 'P0239402', '2018-01-31');
insert into salesOrder (ONO, CNO, ENO, RECEIVED) values (1030, 23511, 'P0239401', '2018-01-31');
insert into orderLine values (1020, 10501,7);
insert into orderLine values (1020, 10502,15);
insert into orderLine values (1020, 10504,3);
insert into orderLine values (1020, 10503,6);
insert into orderLine values (1021, 10605,4);
insert into orderLine values (1022, 10605,2);
insert into orderLine values (1022, 10704,4);
insert into orderLine values (1023, 10879,4);
insert into orderLine values (1023, 10988,19);
insert into orderLine values (1024, 10502,7);
insert into orderLine values (1024, 10988,2);
insert into orderLine values (1025, 10502,4);
insert into orderLine values (1025, 10988,3);
insert into orderLine values (1025, 10948,2);
insert into orderLine values (1026, 10965,15);
insert into orderLine values (1026, 10933,5);
insert into orderLine values (1027, 10933,21);
insert into orderLine values (1028, 10933,9);
insert into orderLine values (1028, 10965,11);
insert into orderLine values (1029, 10933,4);
insert into orderLine values (1029, 10965,10);
insert into orderLine values (1029, 10988,3);
insert into orderLine values (1030, 10965,6);
You can try this.
That no make sense on
AND zip = 95124
AND zip = 95380
AND zip = 95382
I guess you want to use IN
From your table schema join Orderline and Book you will get price and quantity
select SUM(b.price * o.quantity)
FROM customer c
INNER JOIN salesorder s ON c.cno = s.cno
INNER JOIN Orderline o ON s.ono = o.ono
INNER JOIN Book b ON b.bno = o.bno
where zip IN (95124,95380,95382);
sqlfiddle : http://sqlfiddle.com/#!9/07a9c0b/8
Once check the table where the column price exists in...I see it in the table book
and yet you did not use the table book in your query... so this is how you should do it..
select SUM(Book.price * Orderline.quantity)
FROM customer
INNER JOIN salesorder ON customer.cno = salesorder.cno
INNER JOIN Orderline ON salesorder.ono = Orderline.ono
INNER JOIN Book ON Book.bno = Orderline.bno
where customer.zip IN (95124,95380,95382);
The error I'm getting is: #1054 - Unknown column 'price' in 'field list'`
As per your query you have not added the table having price column.
If you can see your DDL statements price column is in book table and you have to join this table to get the required result.
I guess this should work
SELECT SUM(book.price*orderline.quantity) AS "order value" from customer
JOIN salesorder
ON salesorder.cno=customer.cno
JOIN orderline
ON salesorder.ono=orderline.ono
JOIN book
ON orderline.bno=book.bno
JOIN zipcode
ON customer.zip = zipcode.zip
WHERE city in ('Turlock', 'San Jose')
First thing that needs correction is an aggregate function like sum, avg etc need a group clause as well. For example
select sum(price * quantity) as revenue from customers c, salesorder s where
c.customer_no=s.customer_no group by c.customer_no
It will return the total revenue a single customer generated . Second thing is
for filtering based on several Zip Code you can use IN clause like
where zip_no in ('A','B','C')
Third please check you are writing correct column name and spelling of column as well of price

Find max of count on group by

I am having some trouble answering the following question.
For each age value that appears in the Student table, find the level value that
appears most often. For example, if there are more FR level students aged 18 than SR,
JR, or SO students aged 18, you should print the pair (18, FR).
from this data set.
create database university;
use university;
create table student(
snum decimal(9) primary key,
sname varchar(30),
major varchar(25),
level varchar(2),
age int
);
create table faculty(
fid decimal(9) primary key,
fname varchar(30),
deptid decimal(2)
);
create table class(
cname varchar(40) primary key,
meets_at varchar(20),
room varchar(10),
fid decimal(9),
foreign key(fid) references faculty(fid)
);
create table enrolled(
snum decimal(9),
cname varchar(40),
primary key(snum,cname),
foreign key(snum) references student(snum),
foreign key(cname) references class(cname)
);
insert into student values(051135593,'Maria White','English','SR',21);
insert into student values(060839453,'Charles Harris','Architecture','SR',22);
insert into student values(099354543,'Susan Martin','Law','JR',20);
insert into student values(112348546,'Joseph Thompson','Computer Science','SO',19);
insert into student values(115987938,'Christopher Garcia','Computer Science','JR',20);
insert into student values(132977562,'Angela Martinez','History','SR',20);
insert into student values(269734834,'Thomas Robinson','Psychology','SO',18);
insert into student values(280158572,'Margaret Clark','Animal Science','FR',18);
insert into student values(301221823,'Juan Rodriguez','Psychology','JR',20);
insert into student values(318548912,'Dorthy Lewis','Finance','FR',18);
insert into student values(320874981,'Daniel Lee','Electrical Engineering','FR',17);
insert into student values(322654189,'Lisa Walker','Computer Science','SO',17);
insert into student values(348121549,'Paul Hall','Computer Science','JR',18);
insert into student values(351565322,'Nancy Allen','Accounting','JR',19);
insert into student values(451519864,'Mark Young','Finance','FR',18);
insert into student values(455798411,'Luis Hernandez','Electrical Engineering','FR',17);
insert into student values(462156489,'Donald King','Mechanical Engineering','SO',19);
insert into student values(550156548,'George Wright','Education','SR',21);
insert into student values(552455318,'Ana Lopez','Computer Engineering','SR',19);
insert into student values(556784565,'Kenneth Hill','Civil Engineering','SR',21);
insert into student values(567354612,'Karen Scott','Computer Engineering','FR',18);
insert into student values(573284895,'Steven Green','Kinesiology','SO',19);
insert into student values(574489456,'Betty Adams','Economics','JR',20);
insert into student values(578875478,'Edward Baker','Veterinary Medicine','SR',21);
insert into faculty values(142519864,'Ivana Teach',20);
insert into faculty values(242518965,'James Smith',68);
insert into faculty values(141582651,'Mary Johnson',20);
insert into faculty values(011564812,'John Williams',68);
insert into faculty values(254099823,'Patricia Jones',68);
insert into faculty values(356187925,'Robert Brown',12);
insert into faculty values(489456522,'Linda Davis',20);
insert into faculty values(287321212,'Michael Miller',12);
insert into faculty values(248965255,'Barbara Wilson',12);
insert into faculty values(159542516,'William Moore',33);
insert into faculty values(090873519,'Elizabeth Taylor',11);
insert into faculty values(486512566,'David Anderson',20);
insert into faculty values(619023588,'Jennifer Thomas',11);
insert into faculty values(489221823,'Richard Jackson',33);
insert into faculty values(548977562,'Ulysses Teach',20);
insert into class values('Data Structures','MWF 10','R128',489456522);
insert into class values('Database Systems','MWF 12:30-1:45','1320 DCL',142519864);
insert into class values('Operating System Design','TuTh 12-1:20','20 AVW',489456522);
insert into class values('Archaeology of the Incas','MWF 3-4:15','R128',248965255);
insert into class values('Aviation Accident Investigation','TuTh 1-2:50','Q3',011564812);
insert into class values('Air Quality Engineering','TuTh 10:30-11:45','R15',011564812);
insert into class values('Introductory Latin','MWF 3-4:15','R12',248965255);
insert into class values('American Political Parties','TuTh 2-3:15','20 AVW',619023588);
insert into class values('Social Cognition','Tu 6:30-8:40','R15',159542516);
insert into class values('Perception','MTuWTh 3','Q3',489221823);
insert into class values('Multivariate Analysis','TuTh 2-3:15','R15',090873519);
insert into class values('Patent Law','F 1-2:50','R128',090873519);
insert into class values('Urban Economics','MWF 11','20 AVW',489221823);
insert into class values('Organic Chemistry','TuTh 12:30-1:45','R12',489221823);
insert into class values('Marketing Research','MW 10-11:15','1320 DCL',489221823);
insert into class values('Seminar in American Art','M 4','R15',489221823);
insert into class values('Orbital Mechanics','MWF 8','1320 DCL',011564812);
insert into class values('Dairy Herd Management','TuTh 12:30-1:45','R128',356187925);
insert into class values('Communication Networks','MW 9:30-10:45','20 AVW',141582651);
insert into class values('Optical Electronics','TuTh 12:30-1:45','R15',254099823);
insert into class values('Intoduction to Math','TuTh 8-9:30','R128',489221823);
insert into enrolled values(112348546,'Database Systems');
insert into enrolled values(115987938,'Database Systems');
insert into enrolled values(348121549,'Database Systems');
insert into enrolled values(322654189,'Database Systems');
insert into enrolled values(552455318,'Database Systems');
insert into enrolled values(455798411,'Operating System Design');
insert into enrolled values(552455318,'Operating System Design');
insert into enrolled values(567354612,'Operating System Design');
insert into enrolled values(112348546,'Operating System Design');
insert into enrolled values(115987938,'Operating System Design');
insert into enrolled values(322654189,'Operating System Design');
insert into enrolled values(567354612,'Data Structures');
insert into enrolled values(552455318,'Communication Networks');
insert into enrolled values(455798411,'Optical Electronics');
insert into enrolled values(301221823,'Perception');
insert into enrolled values(301221823,'Social Cognition');
insert into enrolled values(301221823,'American Political Parties');
insert into enrolled values(556784565,'Air Quality Engineering');
insert into enrolled values(099354543,'Patent Law');
insert into enrolled values(574489456,'Urban Economics');
My best attempt is correct, which is:
select age, level from (select age, level, count(level) as levelCount from student group by age, level order by age, levelCount desc) as counts group by age;
But it breaks the rules of standard sql where every selected value must be group by, which I don't do with the outer most select statement, level. I am taking advantage of MySQl's functionality to return non-aggregated data.
The question is how can I return the max of a count for each group that is created from a group by, while conforming to standard sql best practices.
select age, level, count(level) as levelCount from student group by age, level order by age, levelCount desc;
Please and thank you
This is a pain in MySQL. One way is to use variables. However, the simplest way uses group_concat()/substring_index() trick:
select age,
substring_index(group_concat(level order by levelCount desc), ',', 1) as mode_level
from (select age, level, count(level) as levelCount
from student
group by age, level
order by age, levelCount desc
) as counts
group by age;
(Statistically, what you are looking for is called the mode.)
Note: This is a trick. The intermediate space for group_concat() is -- by default -- 1,024 characters, so it could result in an out-of-space/overflow error. This limit is easily increased.

Syntax Error in CREATE TABLE Statement in MS Access

I have created a table called Customers with a CustomerID, Last Name, First Name, Address, and City, but when I tried to use the INSERT INTO to add data and ran the SQL Statement, it gives me an error: "Syntax error in CREATE TABLE statement". Below is the SQL Statement I have so far:
CREATE TABLE Customers
(
CustomerID int,
LastName varchar(50),
FirstName varchar(50),
Address varchar(50),
City varchar(50)
);
INSERT INTO (CustomerID, LastName, FirstName, Address, City)
VALUES ('10001', 'Smith', 'John', '1002 Danville Road', 'Louisville');
You are missing the table name from your INSERT statement:
INSERT INTO Customers (CustomerID, LastName, FirstName, Address, City)
VALUES ('10001', 'Smith', 'John', '1002 Danville Road', 'Louisville');
As to why you got an error pointing to your CREATE TABLE statement (which looks correct), my guess is that Access tried to connect the botched INSERT statement to the create in an effort to parse everything correctly.

Insert into table if a field value do not already exist

I want to insert values to a row in my customer table if the Name value I'm providing do not already exist,
After some searching I used this sql query to do it and it does not work :(
IF NOT EXISTS (SELECT Name FROM customer WHERE Name = 'Riyafa')
INSERT INTO customer (`Name`, `Address`, `ContactNo`,`Total_amout`)
VALUES ('Riyafa', 'ABC', '555','1000');
Please instruct me why that is incorrect.
The if statement is only allowed in stored procedures, functions, and triggers. One way you can do this is:
INSERT INTO customer (`Name`, `Address`, `ContactNo`,`Total_amout`)
SELECT name, address, contactno, total_amount
FROM (SELECT 'Riyafa' as name, 'ABC' as address, '555' as contact no, '1000' as total_amount) t
WHERE NOT EXISTS (SELECT 1 FROM customer c WHERE c.name = t.name);
A better approach, however, is to have the database enforce uniqueness on the name. Start by creating a unique index or name:
CREATE UNIQUE INDEX idx_customer_name ON customer(name);
Then use a construct such as on duplicate key update:
INSERT INTO customer (`Name`, `Address`, `ContactNo`,`Total_amout`)
SELECT 'Riyafa' as name, 'ABC' as address, '555' as contact no, '1000' as total_amount
ON DUPLICATE KEY UPDATE Name = VALUES(Name);
The expression ON DUPLICATE KEY UPDATE Name = VALUES(Name) actually doesn't do anything, but it prevents the INSERT from returning an error.