Multiply columns and then sum rows: Oracle SQL - sql

I'm trying to make a query in Oracle SQL Developer that must show the customer who has spend the biggest amount of money. So I have 4 tables: Cliente(Customer), Orden(Sale), Producto(Product) and a junction table to break many to many relationship between Orden and Producto.
CREATE TABLE Cliente(
id_cliente INT NOT NULL PRIMARY KEY,
Nombre VARCHAR(40) NOT NULL,
Apellido VARCHAR(40) NOT NULL,
Direccion VARCHAR(100) NOT NULL,
Telefono INT NOT NULL,
Tarjeta INT NOT NULL,
Edad INT NOT NULL,
Salario INT NOT NULL,
Genero VARCHAR(5) NOT NULL,
id_pais INT NOT NULL,
CONSTRAINT fk_cliente_pais FOREIGN KEY(id_pais) REFERENCES Pais(id_pais)
);
CREATE TABLE Producto(
id_producto INT NOT NULL PRIMARY KEY,
Nombre VARCHAR(40) NOT NULL,
Precio DECIMAL(10,2) NOT NULL,
id_categoria INT NOT NULL,
CONSTRAINT fk_categoria FOREIGN KEY(id_categoria) REFERENCES Categoria(id_categoria)
)
CREATE TABLE Orden(
id_orden INT NOT NULL,
linea_orden INT NOT NULL,
fecha_orden DATE NOT NULL,
id_cliente INT NOT NULL,
id_vendedor INT NOT NULL,
id_producto INT NOT NULL,
cantidad INT NOT NULL,
CONSTRAINT fk_orden_cliente FOREIGN KEY(id_cliente) REFERENCES Cliente(id_cliente),
CONSTRAINT fk_orden_vendedor FOREIGN KEY(id_vendedor) REFERENCES Vendedor(id_vendedor),
CONSTRAINT fk_orden_producto FOREIGN KEY(id_producto) REFERENCES Producto(id_producto),
CONSTRAINT pk_orden PRIMARY KEY(id_orden, linea_orden)
);
DROP TABLE Detalle;
CREATE TABLE Detalle(
id_detalle INT GENERATED ALWAYS AS IDENTITY,
id_producto INT NOT NULL,
id_orden INT NOT NULL,
linea_orden INT NOT NULL,
precio INT NOT NULL,
cantidad INT NOT NULL,
CONSTRAINT DETALLE_PRODUCTO
FOREIGN KEY (id_producto)
REFERENCES Producto (id_producto),
CONSTRAINT DETALLE_ORDEN
FOREIGN KEY (id_orden, linea_orden)
REFERENCES Orden (id_orden, linea_orden),
CONSTRAINT DETALLE_pk PRIMARY KEY(id_detalle, id_producto, id_orden)
);
On Table Detalle: precio (price), cantidad(quantity)
So I'm trying to get the Maximum total amount that a Customer has purchased by this query:
SELECT Cl.id_cliente, Cl.Nombre, Cl.Apellido,SUM( Detalle.precio * Detalle.cantidad) AS TOTAL
FROM Orden
INNER JOIN Cliente Cl ON Cl.id_cliente = Orden.id_cliente
INNER JOIN Detalle ON Detalle.id_orden = Orden.id_orden
GROUP BY Cl.id_cliente, Cl.Nombre, Cl.Apellido
ORDER BY TOTAL DESC;
But in the result, the TOTAL exceeds a lot from the right result, since I have another file with the result it should show.

I rewritten your query:
Select Cl.id_cliente, Cl.Nombre, Cl.Apellido,TOTAL
from(
SELECT Orden.id_cliente, SUM( Detalle.precio * Detalle.cantidad) AS TOTAL
FROM Detalle
JOIN Orden ON Detalle.id_orden = Orden.id_orden
GROUP BY Orden.id_cliente
) Orden
JOIN Cliente Cl ON Cl.id_cliente = Orden.id_cliente
ORDER BY TOTAL DESC
;

The data from details is summed multiple times, so calaculate them before joining
SELECT Cl.id_cliente, Cl.Nombre, Cl.Apellido,SUM(TOTAL ) AS TOTAL
FROM Orden
INNER JOIN Cliente Cl ON Cl.id_cliente = Orden.id_cliente
INNER JOIN (SELECT id_orden, SUM( Detalle.precio * Detalle.cantidad) AS TOTAL FROM Detalle GROUP BY id_orden) Detalle ON Detalle.id_orden = Orden.id_orden
GROUP BY Cl.id_cliente, Cl.Nombre, Cl.Apellido
ORDER BY TOTAL DESC;

Related

How do i select the highest number in one column and then search the the id in other table

How do I select the highest number in one column and then search the the ID in other table?
I have these two tables:
create table armazens_has_artigos
(
stockMinimo int not null,
stockQuantidade int not null,
idArtigos int not null,
idArmazens int not null,
idZonasFisicas int not null
constraint fk_armazens_has_artigos_idArtigos
foreign key (idArtigos)
references artigos (idArtigos) on delete cascade,
constraint fk_armazens_has_artigos_idArmazens
foreign key (idArmazens)
references armazens (idArmazens) on delete cascade,
constraint fk_armazens_has_artigos_zonasFisicas_idZonasFisicas
foreign key (idZonasFisicas)
references zonasFisicas (idZonasFisicas) on delete cascade
);
In this one I need to select the highest StockQuantidade and then with the same id select in this table the nome
create table artigos
(
idArtigos int primary key,
nome varchar (45) not null,
descr varchar (45) not null,
precoCompra float not null,
precoVenda float not null,
unidadeRepresentacao varchar (45) not null
);
There might be more then one Artigos matching the criteria:
SELECT *
FROM artigos
WHERE idArtigos IN
(
SELECT idArtigos
FROM dbo.armazens_has_artigos
WHERE stockQuantidade =
(
SELECT MAX(stockQuantidade)FROM armazens_has_artigos
)
);
SELECT
nome
FROM artigos
JOIN armazens_has_artigos
ON armazens_has_artigos.idArtigos = artigos.idArtigos
WHERE armazens_has_artigos.stockQuantidade = (SELECT
MAX(stockQuantidade)
FROM armazens_has_artigos)

SQL Select Query Giving me duplicate columns

I am trying to write a query to display information, but when I use INNER JOIN I am getting duplicate ID fields. My tables and query are below. I am getting duplicate columns for wrestlerID. could it be my PK constraints?
CREATE TABLE WRESTLER
(
WrestlerID CHAR(6) NOT NULL,
WrestlerFirst VARCHAR2(15) NOT NULL,
WrestlerLast VARCHAR2(25) NOT NULL,
WrestlerStyle CHAR(2) NOT NULL,
WrestleKilos NUMERIC(3,0) NOT NULL,
WrestleMeters NUMERIC(3,2),
WrestleCity VARCHAR2(40) NOT NULL,
WrestlerState CHAR(2) NOT NULL,
WrestlerBirthdate Date,
CONSTRAINT WRESTLER_PK PRIMARY KEY (WrestlerID)
);
CREATE TABLE CLUB
(
ClubID CHAR(5) NOT NULL,
Club VARCHAR2(35) NOT NULL,
WrestlerID CHAR(6) NOT NULL,
CONSTRAINT CLUB_PK PRIMARY KEY (ClubID, WrestlerID),
CONSTRAINT CLUB_FK
FOREIGN KEY (WrestlerID) REFERENCES WRESTLER(WrestlerID)
);
CREATE TABLE SCHOOL
(
SchoolID VARCHAR2(10) NOT NULL,
School VARCHAR2(35) NOT NULL,
WrestlerID CHAR(6) NOT NULL,
CONSTRAINT SCHOOL_PK PRIMARY KEY (SchoolID, WrestlerID),
CONSTRAINT SCHOOL_FK
FOREIGN KEY (WrestlerID) REFERENCES WRESTLER(WrestlerID)
);
CREATE TABLE MEDAL
(
WrestlerID CHAR(6) NOT NULL,
WorldMedal NUMERIC(4,0),
CONSTRAINT MEDAL_PK PRIMARY KEY (WrestlerID, WorldMedal),
CONSTRAINT MEDAL_FK
FOREIGN KEY (WrestlerID) REFERENCES WRESTLER(WrestlerID)
);
SELECT
t1.WrestlerID, t1.WrestlerFirst, t1.WrestlerLast,
t2.WrestlerID, t2.School,
t2.WrestlerID, t3.Club
FROM
WRESTLER t1
INNER JOIN
SCHOOL t2 ON t1.WrestlerID = t2.WrestlerID
INNER JOIN
CLUB t3 ON t1.WrestlerID = t3.WrestlerID;
Fixing my query worked
SELECT
t1.WrestlerID, t1.WrestlerFirst, t1.WrestlerLast,
t2.School,
t3.Club FROM
WRESTLER t1 INNER JOIN
SCHOOL t2 on t1.WrestlerID = t2.WrestlerID INNER JOIN
CLUB t3 on t1.WrestlerID = t3.WrestlerID;

Ambiguous column name 'ProductNumber'

Can't figure out why my question 3 is getting the error mentioned above.
Below is my code
/* Question 1 */
CREATE TABLE CUSTOMERS
(
CustomerID INT PRIMARY KEY,
CustFirstName VARCHAR(50) NOT NULL,
CustLastName VARCHAR(50) NOT NULL,
CustStreetAddress VARCHAR(50) NOT NULL,
CustCity VARCHAR(50) NOT NULL,
CustState VARCHAR(26) NOT NULL,
CustZipCode INT NOT NULL,
CustAreaCode INT NOT NULL,
CustPhoneNumber VARCHAR (26) NOT NULL
);
CREATE TABLE EMPLOYEES
(
EmployeeID INT PRIMARY KEY,
EmpFirstName VARCHAR(50) NOT NULL,
EmpLastName VARCHAR(50) NOT NULL,
EmpCity VARCHAR (50) NOT NULL,
EmpState VARCHAR(26) NOT NULL,
EmpZipCode INT NOT NULL,
EmpAreaCode INT NOT NULL,
EmpPhoneNumber VARCHAR(26) NOT NULL,
EmpBirthDate DATE NOT NULL
);
CREATE TABLE ORDERS
(
OrderNumber INT PRIMARY KEY,
OrderDate DATE NOT NULL,
ShipDate DATE NOT NULL,
CustomerID INT NOT NULL,
EmployeeID INT NOT NULL,
FOREIGN KEY(EmployeeID) REFERENCES EMPLOYEES(EmployeeID),
FOREIGN KEY(CustomerID) REFERENCES CUSTOMERS(CustomerID)
);
CREATE TABLE CATEGORIES
(
CategoryID INT PRIMARY KEY,
CategoryDescription VARCHAR(255) NOT NULL
);
CREATE TABLE PRODUCTS
(
ProductNumber INT PRIMARY KEY,
ProductName VARCHAR(50) NOT NULL,
ProductDescription VARCHAR(255) NOT NULL,
RetailPrice INT NOT NULL,
QuantityOnHand INT NOT NULL,
CategoryID INT NOT NULL,
FOREIGN KEY(CategoryID) REFERENCES CATEGORIES (CategoryID)
);
CREATE TABLE ORDER_DETAILS
(
OrderNumber INT NOT NULL,
ProductNumber INT NOT NULL,
QuotedPrice INT NOT NULL,
QuantityOrdered INT NOT NULL,
PRIMARY KEY (OrderNumber, ProductNumber),
FOREIGN KEY (OrderNumber) REFERENCES ORDERS(OrderNumber),
FOREIGN KEY(ProductNumber) REFERENCES PRODUCTS(ProductNumber)
);
CREATE TABLE VENDORS
(
VendorID INT PRIMARY KEY,
VendName VARCHAR(100) NOT NULL,
VendStreetAddress VARCHAR(50) NOT NULL,
VendCity VARCHAR(50) NOT NULL,
VendState VARCHAR(26) NOT NULL,
VendFaxNumber VARCHAR(50) NOT NULL,
VendWebPage VARCHAR(100) NOT NULL,
VendEmailAddress VARCHAR(100) NOT NULL
);
CREATE TABLE PRODUCT_VENDORS
(
ProductNumber INT NOT NULL,
VendorID INT NOT NULL,
WholeSalePrice INT NOT NULL,
DaysToDeliver INT NOT NULL,
PRIMARY KEY(ProductNumber, VendorID),
FOREIGN KEY(ProductNumber) REFERENCES PRODUCTS(ProductNumber),
FOREIGN KEY(VendorID) REFERENCES Vendors(VendorID)
);
/* QUESTION 2 */
SELECT
OrderDate, CustFirstName, CustLastName
FROM
CUSTOMERS C, ORDERS O
WHERE
C.CustomerID = O.OrderNumber;
/* QUESTION 3 */
SELECT
ProductNumber, WholeSalePrice, VendName
FROM
PRODUCTS P, PRODUCT_VENDORS PV, VENDORS V
WHERE
P.PRODUCTNUMBER = PV.ProductNumber AND PV.VendorID = V.VendorID;
I got the error
Ambiguous column name 'ProductNumber'
Because there are two table have column name of ProductNumber in your query you need to tell DB engine which column you want to get.
Also, use JOIN instead of , comma CROSS JOIN, because Join is more clear than a comma on the relationship between the two tables
SELECT PV.ProductNumber, WholeSalePrice, VendName
FROM PRODUCTS P
JOIN PRODUCT_VENDORS PV ON P.PRODUCTNUMBER = PV.ProductNumber
JOIN VENDORS V ON PV.VendorID = V.VendorID;
Your query stipulates SELECT ProductNumber From two tables that both have ProductNumber. In the SELECT columns list, prefix ProductNumber with the table name from which you want to receive the data.
also, Have a look at your question 2. I don't think you really want to join customer id to order number, and you should use JOIN syntax intead of joining in the WHERE clause
Ambiguous error means that you are calling a certain field in which exist in both Table and the SQL has no idea where to get it. See, with your query, you're just literally calling the ProductNumber field in which the SQL has no idea where to get it since there are ProductNumber fields on both Tables, and you didn't specify any table.
so it is better when you have same column in multiple tables use table preface 1st then column like table1.col1,table2.col2
so in your case
SELECT p.ProductNumber, WholeSalePrice, v.VendName
FROM PRODUCTS P join
PRODUCT_VENDORS PV on P.PRODUCTNUMBER = PV.ProductNumber //
join VENDORS V on PV.VendorID = V.VendorID //use join instead your's

Need help in the query (oracle 11 g)

First of all, these are my create_table statements.
/*--- Base tables ---*/
CREATE TABLE CUSTOMER (
CUST_ID VARCHAR(10) NOT NULL,
CUST_FNAME VARCHAR(15),
CUST_LNAME VARCHAR(15),
CUST_HP NUMBER(10),
CUST_EMAIL VARCHAR(40),
primary key (CUST_ID)
);
CREATE TABLE STAFF(
STAFF_ID NUMBER(12) NOT NULL,
STAFF_FNAME VARCHAR(15),
STAFF_LNAME VARCHAR(15),
STAFF_DOB Date,
STAFF_AGE NUMBER(2),
STAFF_ADDRESS VARCHAR(70),
STAFF_HP NUMBER(10),
STAFF_SALARY NUMBER(6,2),
primary key (STAFF_ID)
);
CREATE TABLE SUPPLIER(
SUPP_ID VARCHAR(10) NOT NULL,
SUPP_NAME VARCHAR(30),
SUPP_TEL NUMBER(10),
SUPP_EMAIL VARCHAR(25),
SUPP_ADDRESS VARCHAR(80),
SUPP_CITY VARCHAR(20),
SUPP_STATE VARCHAR(10),
SUPP_ZIP NUMBER(5),
primary key (SUPP_ID)
);
CREATE TABLE PRODUCT(
PROD_ID VARCHAR(10) NOT NULL,
PROD_NAME VARCHAR(30),
PROD_DESC VARCHAR(70),
R_UNIT_PRICE NUMBER(3,2) DEFAULT 4.50,
L_UNIT_PRICE NUMBER(3,2) DEFAULT 5.00,
primary key (PROD_ID)
);
CREATE TABLE INGREDIENT(
ING_ID VARCHAR(10) NOT NULL,
ING_NAME VARCHAR(20),
ING_DESC VARCHAR(60),
primary key (ING_ID)
);
CREATE TABLE TOPPING(
TOP_ID VARCHAR(10) NOT NULL,
TOP_NAME VARCHAR(20),
TOP_DESC VARCHAR(40),
TOP_PRICE NUMBER(2,2),
primary key (TOP_ID)
);
/*--- Child tables ---*/
CREATE TABLE ORDERS(
ORDERS_ID VARCHAR(10) NOT NULL,
CUST_ID VARCHAR(10) NOT NULL,
STAFF_ID NUMBER(12) NOT NULL,
TOTAL_PRICE NUMBER(5,2),
ORDERS_DATE DATE,
primary key (ORDERS_ID),
foreign key (CUST_ID) references CUSTOMER(CUST_ID),
foreign key (STAFF_ID) references STAFF(STAFF_ID)
);
CREATE TABLE ORD_DETAIL(
SK1_ID NUMBER(4) NOT NULL,
ORDERS_ID VARCHAR(10) NOT NULL,
PROD_ID VARCHAR(10) NOT NULL,
TOP_ID VARCHAR(10) NOT NULL,
TEATYPE VARCHAR(8),
CUPSIZE CHAR(1),
QTY NUMBER(2),
CONSTRAINT CHK_CUPSIZE CHECK (CUPSIZE = 'R' OR CUPSIZE = 'L'),
CONSTRAINT CHK_TEATYPE CHECK (TEATYPE = 'Oolong' OR TEATYPE = 'Jasmine'),
primary key (SK1_ID),
foreign key (ORDERS_ID) references ORDERS(ORDERS_ID),
foreign key (PROD_ID) references PRODUCT(PROD_ID),
foreign key (TOP_ID) references TOPPING(TOP_ID)
);
CREATE TABLE PROD_ING(
PROD_ID VARCHAR(10) NOT NULL,
ING_ID VARCHAR(10) NOT NULL,
primary key (PROD_ID , ING_ID),
foreign key (PROD_ID) references PRODUCT(PROD_ID),
foreign key (ING_ID) references INGREDIENT(ING_ID)
);
CREATE TABLE ING_SUPP(
ING_ID VARCHAR(10) NOT NULL,
SUPP_ID VARCHAR(10) NOT NULL,
SUPP_PRICE NUMBER(6,2),
primary key (ING_ID, SUPP_ID),
foreign key (ING_ID) references INGREDIENT(ING_ID),
foreign key (SUPP_ID) references SUPPLIER(SUPP_ID)
);
And my query is this:Which employee served the most number of customers?
But when I executed these statements:
SELECT MAX (COUNT (O.CUST_ID)) AS "Served customer"
FROM STAFF S, ORDERS O
WHERE (S.STAFF_ID = O.STAFF_ID)
GROUP BY O.STAFF_ID
HAVING COUNT (O.CUST_ID) > 0;
What I got was there was a column named "Served customer" with the maximum number only.
So, now my question is how to display the employee's ID, first name and last name along with
"Served customer" column.
The question is: "Which employee served the most number of customers? " Your query does not even return information about the employee, only about the number of customers.
The following aggregation returns for each staff member, the number of customers who were served:
SELECT O.STAFF_ID, COUNT(O.CUST_ID) AS "Served customer"
FROM STAFF SS JOIN
ORDERS O
ON S.STAFF_ID = O.STAFF_ID
GROUP BY O.STAFF_ID;
To get the maximum number we just need to order this by the count and take the first row. In Oracle, this uses a subquery:
SELECT so.*
FROM (SELECT O.STAFF_ID, COUNT(O.CUST_ID) AS "Served customer"
FROM STAFF SS JOIN
ORDERS O
ON S.STAFF_ID = O.STAFF_ID
GROUP BY O.STAFF_ID
ORDER BY COUNT(O.CUST_ID) DESC
) so
WHERE rownum = 1;

Where clause in Mutiple table sql joins

TABLES
CREATE TABLE LocalBusiness
(
BusinessID INT NOT NULL PRIMARY KEY,
BusinessName VARCHAR2 (20) NOT NULL,
TypeID INT,
Latitude DECIMAL (10,2),
Longitude DECIMAL (10,2),
Web_address VARCHAR2 (50) NOT NULL,
Postcode VARCHAR2 (10) NOT NULL,
official_rating int,
min_price NUMBER(4,2),
max_price NUMBER(4,2),
FOREIGN KEY (TypeID) REFERENCES LocalBusinessType (TypeID),
CONSTRAINT chk_Officialrating CHECK (official_rating> 0 AND official_rating<6 )
);
CREATE TABLE Address
(
AddressID INT NOT NULL PRIMARY KEY,
BusinessID INT,
AreaID INT,
Address VARCHAR2 (50) NOT NULL,
Postcode VARCHAR2 (10) NOT NULL,
FOREIGN KEY (BusinessID) REFERENCES LocalBusiness (BusinessID),
FOREIGN KEY (AreaID) REFERENCES Area (AreaID)
);
CREATE TABLE Phone
(
PhoneNoID INT NOT NULL PRIMARY KEY,
PhoneNo VARCHAR2 (15) NOT NULL,
BusinessID INT,
Description VARCHAR2 (50) NOT NULL,
FOREIGN KEY (BusinessID) REFERENCES LocalBusiness (BusinessID),
CONSTRAINT PhoneNo_unique UNIQUE (PhoneNo)
);
CREATE TABLE Email
(
EmailID INT NOT NULL PRIMARY KEY,
email_address VARCHAR2 (50) NOT NULL,
BusinessID INT,
Description VARCHAR2 (50) NOT NULL,
FOREIGN KEY (BusinessID) REFERENCES LocalBusiness (BusinessID),
CONSTRAINT email_unique UNIQUE (email_address)
);
CREATE TABLE Area
(
AreaID INT NOT NULL PRIMARY KEY,
AreaName VARCHAR2 (20) NOT NULL,
Region VARCHAR2 (20) NOT NULL
);
SELECT statement:
SELECT
LocalBussiness.BusinessName, Address.Address, Address.Postcode,
Area.AreaName, Area.Region, LocalBusiness.OfficialRating,
LocalBusiness.min_price, LocalBusiness_max_price,
Phone.description, Phone.PhoneNo,
Email.Description, Email.email_address,
LocalBusiness.Web_address
FROM
LocalBusiness
JOIN
Address ON LocalBusiness.BusinessID = Address.BusinessID
JOIN
Area ON Address.AreaID = Area.AreaID
AND LocalBusiness.BusinessID = Address.BusinessID
JOIN
Phone ON Phone.BusinessID = LocalBusiness.BusinessID
JOIN
Email ON Email.BusinessID = LocalBusiness.BusinessID
WHERE
TypeID = '1'
ORDER BY
LocalBusiness.BusinessName ASC;
The where clause in the sql join statement written above seem to be ineffective as the some values for the TypeID return incomplete data while others return no rows at all. How do I go about fixing this?
Yyou repeat a condition
AND LocalBusiness.BusinessID=Address.BusinessID in JOIN Area
Try avoinding this repetition
SELECT LocalBussiness.BusinessName, Address.Address, Address.Postcode,
Area.AreaName, Area.Region, LocalBusiness.OfficialRating,
LocalBusiness.min_price, LocalBusiness_max_price, Phone.description,
Phone.PhoneNo, Email.Description, Email.email_address,
LocalBusiness.Web_address
FROM LocalBusiness
JOIN Address ON LocalBusiness.BusinessID=Address.BusinessID
JOIN Area ON Address.AreaID=Area.AreaID
JOIN Phone ON Phone.BusinessID=LocalBusiness.BusinessID
JOIN Email ON Email.BusinessID=LocalBusiness.BusinessID
WHERE TypeID = '1'
ORDER BY LocalBusiness.BusinessName ASC;
Otherwise check th consistence of your data ..