Q: List the numbers and names of all customers who are represented by Ann Hull but who do not currently have orders on file.
This is what I have so far... but it isn't the right output:
CREATE TABLE REP (
REP_NUM char(2) PRIMARY KEY,
LAST_NAME char(15),
FIRST_NAME char(15),
STREET char(15),
CITY char(15),
PROVINCE char(3),
ZIP char(5),
COMMISSION decimal(7, 2),
RATE decimal(3, 2)
);
CREATE TABLE CUSTOMER (
CUSTOMER_NUM char(3) PRIMARY KEY,
CUSTOMER_NAME char(35) NOT NULL,
STREET char(15),
CITY char(15),
PROVINCE char(3),
ZIP char(5),
BALANCE decimal(8, 2),
CREDIT_LIMIT decimal(8, 2),
REP_NUM char(2)
);
CREATE TABLE ORDERS (
ORDER_NUM char(5) PRIMARY KEY,
ORDER_DATE date,
CUSTOMER_NUM char(3)
);
CREATE TABLE PART (
PART_NUM char(4) PRIMARY KEY,
DESCRIPTION char(15),
ON_HAND decimal(4, 0),
CLASS char(2),
WAREHOUSE char(1),
PRICE decimal(6, 2)
);
CREATE TABLE ORDER_LINE (
ORDER_NUM char(5),
PART_NUM char(4),
NUM_ORDERED decimal(3, 0),
QUOTED_PRICE decimal(6, 2),
PRIMARY KEY (ORDER_NUM, PART_NUM)
);
SELECT * FROM CUSTOMER c
LEFT JOIN REP r
ON c.REP_NUM = r.REP_NUM
LEFT JOIN ORDERS o
ON o.CUSTOMER_NUM = c.CUSTOMER_NUM
WHERE o.CUSTOMER_NUM ISNULL
AND r.FIRST_NAME = 'Ann Hull'
Hope the above helps, It should work with minor modifications to the where clause especially combining the first and last names.
Filter the Rep table on firstname and lastname for Ann Hull and join it with the Rep table on the rep_num fields. To only return customers with no orders use a subquery to check that no orders exists (NOT EXISTS) in the Order table for a customer.
SELECT * FROM CUSTOMER c
JOIN REP r
ON c.REP_NUM = r.REP_NUM
AND r.FIRSTNAME = 'Ann'
AND r.LASTNAME = 'Hull'
AND NOT EXISTS(SELECT * FROM ORDERS o WHERE o.CUSTOMER_NUM = c.CUSTOMER_NUM)
Related
assuming these 3 tables
create table item(
item_id integer NOT NULL primary key,
name varchar(50) NOT NULL,
description varchar(150) NOT NULL,
in_stock integer NOT NULL
)
create table customer(
customer_id VARCHAR(9) NOT NULL primary key,
name VARCHAR(50) NOT NULL,
lastname VARCHAR(50) NOT NULL,
phone VARCHAR(15) NOT NULL,
join_date DATE NOT NULL
)
create table purchase(
purchase_id integer references item,
customer_id varchar(9) references customer,
purchase_date TIMESTAMP NOT NULL,
amount INTEGER NOT NULL,
PRIMARY KEY(purchase_id, customer_id, purchase_date)
)
how could I get each unique name and the total amount of items purchased?
how could I get each purchase name and the buyer's name and lastname?
how could I get each item and how many of it were sold?
The two topics you are looking to learn are how to use GROUP BY and how to JOIN tables. Here's an example (more or less) that answers your first question and uses both tools:
select
C.customer_id as customer_id,
max(C.name) as customer_name,
sum(amount) as total_amount
from customer C
left join purchase P on C.customer_id = P.customer_id
group by C.customer_id
I need to do an average median with Oracle. I have this:
SELECT V.Id_Aeropuerto_Destino AS Id_Aeropuerto, C.Nombre AS Ciudad_Destino, A.nombre AS Aeropuerto,
(SELECT SUM((TO_NUMBER(V.Retraso_Salida,'99999999D99','nls_numeric_characters=''.,''')) +
(TO_NUMBER(V.Retraso_Llegada,'99999999D99','nls_numeric_characters=''.,''')))
FROM Vuelo V
WHERE V.Id_Aeropuerto_Destino=Id_Aeropuerto) Retraso_Total
FROM Ciudad C, Aeropuerto A, Vuelo V
WHERE V.Id_Aeropuerto_Destino=A.Id_Aeropuerto AND A.Ciudad = C.Id_Ciudad;
This calculates the total delay of a number of flights ("Retraso_Total"). Now I need to do an AVG from "Retraso_Total", to get ("Retraso_Total" / number of flights) -> Retraso_Medio, something like this:
SELECT V.Id_Aeropuerto_Destino AS Id_Aeropuerto, C.Nombre AS Ciudad_Destino, A.nombre AS Aeropuerto,
(SELECT AVG SUBQUERY) Retraso_Medio,
(SELECT SUM((TO_NUMBER(V.Retraso_Salida,'99999999D99','nls_numeric_characters=''.,''')) +
(TO_NUMBER(V.Retraso_Llegada,'99999999D99','nls_numeric_characters=''.,''')))
FROM Vuelo V
WHERE V.Id_Aeropuerto_Destino=Id_Aeropuerto) Retraso_Total
FROM Ciudad C, Aeropuerto A, Vuelo V
WHERE V.Id_Aeropuerto_Destino=A.Id_Aeropuerto AND A.Ciudad = C.Id_Ciudad;
I try with this:
SELECT AVG(SUM((TO_NUMBER(V.Retraso_Salida,'99999999D99','nls_numeric_characters=''.,''')) +
(TO_NUMBER(V.Retraso_Llegada,'99999999D99','nls_numeric_characters=''.,'''))))
FROM Vuelo V
WHERE V.Id_Aeropuerto_Destino=Id_Aeropuerto
GROUP BY V.Id_Aeropuerto_Destino) Retraso_Medio
But this subquery doesn't work.
How I can do that (all in the same query)?
Thanks!!
Table Vuelo (Flight) has (id_plane, id_origin_airport, id_destiny_airport, id_company, departure hour, arrival hour, departure delay, arrival delay, date, canceled, passengers, milles:
CREATE TABLE Vuelo(
Id_Avion number(4),
Id_Aeropuerto_Origen number(5),
Id_Aeropuerto_Destino number(5),
Id_Aerolinea varchar(2),
Hora_Salida number(4),
Hora_Llegada number(4),
Retraso_Salida varchar(5),
Retraso_Llegada varchar(5),
Fecha varchar(10),
Cancelado varchar(3) NOT NULL,
Pasajeros varchar(10) NOT NULL,
Distancia varchar(10) NOT NULL,
CONSTRAINT pk_Vuelo PRIMARY KEY(Id_Avion, Id_Aeropuerto_Origen, Fecha, Hora_salida),
CONSTRAINT fk_Avion FOREIGN KEY(Id_Avion) REFERENCES Avion(Id_Avion),
CONSTRAINT fk_Aeropuerto_Origen FOREIGN KEY(Id_Aeropuerto_Origen) REFERENCES Aeropuerto(Id_Aeropuerto),
CONSTRAINT fk_Aeropuerto_Destino FOREIGN KEY(Id_Aeropuerto_Destino) REFERENCES Aeropuerto(Id_Aeropuerto),
CONSTRAINT fk_Aerolinea FOREIGN KEY(Id_Aerolinea) REFERENCES Aerolinea(Id_Aerolinea),
CONSTRAINT fk_Fecha FOREIGN KEY(Fecha) REFERENCES Fecha(Id_Fecha)
);
Table Ciudad (City) has id_city, name, latitude, longitude, population, timezone:
CREATE TABLE Ciudad(
Id_Ciudad number(7),
Nombre varchar(80) NOT NULL,
Latitud varchar(15) NOT NULL,
Longitud varchar(15) NOT NULL,
Habitantes number(10) NOT NULL,
Timezone varchar(80) NOT NULL,
CONSTRAINT pk_Ciudad PRIMARY KEY(Id_Ciudad)
);
Table Aeropuerto (Airport) has id_airport, name, code, id_city, state, state_code:
CREATE TABLE Aeropuerto(
Id_Aeropuerto number(5),
Nombre varchar(80) NOT NULL,
Codigo varchar(4) NOT NULL,
Ciudad number(5) NOT NULL,
Estado varchar(80) NOT NULL,
Codigo_Estado varchar(4) NOT NULL,
CONSTRAINT pk_Aeropuerto PRIMARY KEY(Id_Aeropuerto),
CONSTRAINT fk_Ciudad FOREIGN KEY(Ciudad) REFERENCES Ciudad(Id_Ciudad)
);
From your comments it seems you want to calculate a MEAN rather than a MEDIAN as your question's opening line says.
This is quite simple. The Oracle AVG() function calculates the mean for us, without the need to derive totals and counts. Obviously it's still messy, due to the casting because table uses strings to store numeric values.
An average is an aggregate function, so we need to group by the non-aggregate columns. In your example that means the destination airport ID, City and Name. So this should work for you:
SELECT V.Id_Aeropuerto_Destino AS Id_Aeropuerto
, C.Nombre AS Ciudad_Destino
, A.nombre AS Aeropuerto
, AVG((TO_NUMBER(V.Retraso_Salida,'99999999D99','nls_numeric_characters=''.,''')) +
(TO_NUMBER(V.Retraso_Llegada,'99999999D99','nls_numeric_characters=''.,''')))
as "Retraso_Total"
FROM Vuelo V
join Aeropuerto A
on V.Id_Aeropuerto_Destino=A.Id_Aeropuerto
join Ciudad C
on A.Ciudad = C.Id_Ciudad
group by V.Id_Aeropuerto_Destino
, C.Nombre
, A.nombre
/
Display the number of pledges made by each donor. Include the donor ID, first name, last name, and number of pledges.
Below is what I have so far, but it isn't working. It counts the total donors, which makes sense, but I need to count how many pledges each donor made.
select iddonor, firstname, lastname, (select distinct count(iddonor) from dd_pledge) as pledges from dd_donor;
Below are the tables needed:
CREATE TABLE DD_Donor (
idDonor number(4),
Firstname varchar2(15),
Lastname varchar2(30),
Typecode CHAR(1),
Street varchar2(40),
City varchar2(20),
State char(2),
Zip varchar2(9),
Phone varchar2(10),
Fax varchar2(10),
Email varchar2(25),
News char(1),
dtentered date DEFAULT SYSDATE,
CONSTRAINT donor_id_pk PRIMARY KEY(idDonor) );
CREATE TABLE DD_Pledge (
idPledge number(5),
idDonor number(4),
Pledgedate DATE,
Pledgeamt number(8,2),
idProj number(5),
idStatus number(2),
Writeoff number(8,2),
paymonths number(3),
Campaign number(4),
Firstpledge char(1),
CONSTRAINT pledge_id_pk PRIMARY KEY(idPledge),
CONSTRAINT pledge_idDonor_fk FOREIGN KEY (idDonor)
REFERENCES dd_donor (idDonor),
CONSTRAINT pledge_idProj_fk FOREIGN KEY (idProj)
REFERENCES dd_project (idProj),
CONSTRAINT pledge_idStatus_fk FOREIGN KEY (idStatus)
REFERENCES dd_status (idStatus));
You should join the tables and count.
select d.idDonor, d.Firstname, d.Lastname, count(*) count
from DD_Donor d
join DD_Pledge p
on d.idDonor=p.idDonor
group by (d.idDonor, d.Firstname, d.Lastname)
Please note that this only shows a count for donors with a pledge. Those without a pledge will not be shown.
Does this work better?
select iddonor, firstname, lastname, count(dd_pledge.iddonor)
from dd_donor
inner join dd_pledge on dd_doner.iddoner = dd_pledge.iddoner
group by firstname,lastname;
I have used W3school and my in class notes. Was wondering if someone could point me in the right direction.
I am working on an SQL peoject.
I have a database that made from three tables employees, customers, and customer orders.
CUSTOMER (
CUST_NBR NUMBER(10) NOT NULL ,
FNAME NVARCHAR2(20) NULL,
LNAME NVARCHAR2(20) NULL,
PRIMARY KEY(CUST_NBR)
);
EMPLOYEE (
EMP_ID NUMBER(10) NOT NULL ,
FNAME NVARCHAR2(20) NULL,
LNAME NVARCHAR2(20) NULL,
MANAGER_EMP_ID NUMBER(10) NULL,
PRIMARY KEY(EMP_ID),
FOREIGN KEY(MANAGER_EMP_ID)
REFERENCES EMPLOYEE(EMP_ID)
);
CUST_ORDER (
ORDER_NBR NUMBER(10) NOT NULL ,
CUST_NBR NUMBER(10) NOT NULL,
SALES_EMP_ID NUMBER(10) NOT NULL,
SALE_PRICE NUMBER(10, 2) NULL,
PRIMARY KEY(ORDER_NBR),
FOREIGN KEY(SALES_EMP_ID)
REFERENCES EMPLOYEE(EMP_ID),
FOREIGN KEY(CUST_NBR)
REFERENCES CUSTOMER(CUST_NBR)
);
I have filled in sample data for all tables. I am looking to write a query that will allow me to return information fname, lname, order_nbr for the item with the lowest sale price.
I have tried several variations of
Select *
FROM CUST_ORDERS
WHERE SALE_PRICE = min(Sale_price):
Any help would be greatly appreciated.
SELECT TOP 1 * FROM CUST_ORDERS ORDER BY SALE_PRICE;
SELECT TOP 1
fname, lname, order_nbr
FROM cust_order co
JOIN customer с
ON c.cust_nbr = co.cust_nbr
ORDER BY
co.sale_price
I need some help formulating a select statement. I need to select the total quantity shipped for each part with a distinct color. So the result should be a row with the color name and the total.
Here's my schema:
create table s
( sno char(5) not null,
sname char(20) not null,
status smallint,
city char(15),
primary key (sno)
);
create table p
( pno char(6) not null,
pname char(20) not null,
color char(6),
weight smallint,
city char(15),
primary key (pno)
);
create table sp
( sno char(5) not null,
pno char(6) not null,
qty integer not null,
primary key (sno, pno)
);
As your schema stands each product PNO has only one colour, so I'm not sure what your question actually requires.
Sales by product:
select p.pno
, sum (sp.qty)
from p join sp on (p.pno = sp.pno)
group by p.pno;
Sales by colour:
select p.color
, sum (sp.qty)
from p join sp on (p.pno = sp.pno)
group by p.color;
edit
To get sales for a specific colour is easy:
select sum (sp.qty)
from p join sp on (p.pno = sp.pno)
where p.color = 'BLUE';