I'm creating a view and using inner join and receiving the following error:
ORA-00904: "B"."CUSTOMERNO": invalid identifier
This is the code I'm working with to create view and inner join
CREATE VIEW RentalInfoOct
(branch_no, branch_name, customer_no)
AS
SELECT b.branchNo, b.branchName, b.customerNo, c.customerNo
FROM branch b
INNER JOIN
customer c
ON b.customerNo = c.customerNo
Here are the create table commands as well.
CREATE TABLE Branch
(
branchNo SMALLINT NOT NULL,
branchName VARCHAR(20) NOT NULL,
branchAddress VARCHAR(40) NOT NULL,
PRIMARY KEY (BranchNo)
);
CREATE TABLE Customer
(
customerNo SMALLINT NOT NULL,
customerName VARCHAR(15) NOT NULL,
customerAddress VARCHAR(40) NOT NULL,
customerTel VARCHAR(10),
PRIMARY KEY (CustomerNo)
);
CREATE VIEW RentalInfoOct
(branch_no, branch_name, customer_no)
AS
SELECT b.branchNo, b.branchName, b.customerNo, c.customerNo
You specify 3 columns for the view, but you select 4 columns in SELECT
Related
I have a windows function (over, partitioned by) in my code:
FROM (SELECT wp_posts.id,
wp_postmeta.post_id,
post_title,
post_type,
meta_value,
Row_number()
OVER(
partition BY post_title
ORDER BY wp_postmeta.meta_value) rn
but apparently this isn't supported on MariaDB before 10.2 (-I am using 10.1). Could someone please suggest alternative code which is both efficient and works on MariaDB 10.1 also?
dbfiddle provided, unfortunately with only MariaDB 10.2 as the oldest; can't test 10.1 directly here
create table wp_posts (
ID integer primary key auto_increment,
post_title varchar(30),
post_type varchar(30)
);
✓
create table wp_postmeta (
ID integer primary key auto_increment,
post_id integer,
meta_key varchar(30) not null default '_regular_price',
meta_value integer not null
);
✓
insert into wp_posts (post_title, post_type) values
('Apple Pie','Product'),
('French Toast','Product'),
('Shepards Pie','Product'),
('Jam Pie','Product'),
('Jam Pie','Product'),
('Plate','Not a Product'),
('Bucket','Not a Product'),
('Chequebook','Not a Product'),
('French Toast','Product'),
('French Toast','Product'),
('Banana','Product'),
('Banana','Product'),
('Banana','Product');
✓
insert into wp_postmeta (post_id, meta_value) values
(1,10),
(2,5),
(3,9),
(4,8),
(5,11),
(6,12),
(7,10),
(8,6),
(9,1),
(10,1),
(11,7),
(12,2),
(13,2);
✓
-- Deleting all duplicate products in wp_posts table
DELETE FROM wp_posts
WHERE id IN (SELECT id
FROM (SELECT id,
post_title,
post_type,
meta_value
FROM (SELECT wp_posts.id,
wp_postmeta.post_id,
post_title,
post_type,
meta_value,
Row_number()
OVER(
partition BY post_title
ORDER BY wp_postmeta.meta_value) rn
FROM wp_postmeta
JOIN wp_posts
ON wp_postmeta.post_id = wp_posts.id
WHERE wp_posts.post_type = 'Product'
AND wp_postmeta.meta_key = '_regular_price'
) t
WHERE t.rn <> 1) AS aliasx);
✓
db<>fiddle here
Having issues creating a query that displays the customerID, customerFirName, and customerZip from the customers table, based on when the customer has purchased more than one vehicle within the sales table. Here are the table creations:
CREATE TABLE CUSTOMERS
(customerID INT PRIMARY KEY,
customerFirName VARCHAR(20) NOT NULL,
customerLasName VARCHAR(20) NOT NULL,
customerMiName VARCHAR(1) NOT NULL,
customerStreet VARCHAR(40) NOT NULL,
customerState VARCHAR(15) NOT NULL,
customerCity VARCHAR(20) NOT NULL,
customerZip VARCHAR(15) NOT NULL);
CREATE TABLE SALES
(saleID INT PRIMARY KEY,
grossSalePrice DECIMAL(9,2),
vehicleStatus VARCHAR(10) NOT NULL CHECK (lower(vehicleStatus) IN ('available', 'sold', 'pending')),
saleDate DATE,
saleMileage INT,
customerID INT,
salespersonID INT,
vehicleVIN VARCHAR(25),
CONSTRAINT SALES_FK1 FOREIGN KEY (customerID) REFERENCES CUSTOMERS(customerID);
Here is the desired output:
customerID customerFirName customerZip Number_of_Sales
1 Bob 12345 2
2 Jim 94949 3
3 Tom 99330 4
Here is what I've tried....I'm having issues creating a single SELECT statement that has an inner join to combine the SALES.customerID field on the CUSTOMERS.customerID field. Where am I going wrong? Thanks!
SELECT CUSTOMERS.customerFirName, CUSTOMERS.CustomerID, CUSTOMERS.customerZip, COUNT(SALES.customerID)
FROM CUSTOMERS
INNER JOIN SALES ON CUSTOMERS.customerID=SALES.customerID
GROUP BY SALES.customerID
HAVING COUNT(SALES.customerID) > 1;
AND
SELECT COUNT (CUSTOMERS.customerID), customerFullName, customerZip
FROM CUSTOMERS
INNER JOIN SALES ON CUSTOMERS.customerID=SALES.customerID
GROUP BY SALES.customerID
HAVING COUNT(SALES.customerID) > 1;
I guess the issus is on group by field.
SELECT CUSTOMERS.customerFirName, CUSTOMERS.CustomerID, CUSTOMERS.customerZip,
COUNT(SALES.customerID)
FROM CUSTOMERS
INNER JOIN SALES ON CUSTOMERS.customerID=SALES.customerID
GROUP BY CUSTOMERS.customerFirName, CUSTOMERS.CustomerID, CUSTOMERS.customerZip
HAVING COUNT(SALES.customerID) > 1;
Maybe just do a count in a sub select:
select * from
(SELECT CUSTOMERS.customerFirName, CUSTOMERS.CustomerID, CUSTOMERS.customerZip, COUNT(*) num_sales
FROM CUSTOMERS
INNER JOIN SALES ON CUSTOMERS.customerID=SALES.customerID
GROUP BY CUSTOMERS.customerFirName, CUSTOMERS.CustomerID, CUSTOMERS.customerZip)
where num_sales > 1;
Create table A_15006977.vehicle. (
Vin varchar(20) primary key,
Vehicle_type char(20) not null,
Mileage number(20) not null,
Manufacturer char(20) not null
);
Insert all
Into A_15006977.vehicle(vin,vehicle_type,mileage,manufacturer)
values ('tf1bb2ve533093891','panel van',18 325,'man')
A_15006977.vehicle(vin,vehicle_type,mileage,manufacturer)
values
('tf1bb2ve533093822','standard van',79 885,'ford')
Select * from dual;
Create table A_15006977.vehicle (
Vin varchar(20) CONSTRAINT vehicle__vin__pk PRIMARY KEY,
Vehicle_type char(20) CONSTRAINT vehicle__vehicle_type__nn not null,
Mileage number(20) CONSTRAINT vehicle__mileage__nn not null,
Manufacturer char(20) CONSTRAINT vehicle__manufacturer__nn not null
);
Insert all
Into A_15006977.vehicle(vin,vehicle_type,mileage,manufacturer)
VALUES ( 'tf1bb2ve533093891', 'panel van', 18325, 'man' )
INTO A_15006977.vehicle (vin,vehicle_type,mileage,manufacturer)
values ( 'tf1bb2ve533093822', 'standard van', 79885, 'ford' )
SELECT 1 FROM DUAL;
Or:
Insert Into A_15006977.vehicle( vin,vehicle_type,mileage,manufacturer )
SELECT 'tf1bb2ve533093891','panel van', 18325, 'man' FROM DUAL UNION ALL
SELECT 'tf1bb2ve533093822','standard van', 79885, 'ford' FROM DUAL;
Note:
You had an extra . after the table name in the DDL statement and spaces in the mileage (18 325 and 79 885) which need removing and you needed an INTO keyword before the second insert.
It is also useful to name your constraints (then you can easily determine which constraint has been violated in later statements).
I am trying to collect data from multiple tables with logic aritmethics like: OR AND NOT UNION INTERSECT.
I have 3 tables called: Customers, Flights, Booking.
My aim is to get all the passportno from the customers table which took a flight more than once.
Thnaks for helpers.
Use group by and having clauses. The having limits those rows that are count>1
Schema
create table bookings
(
id int auto_increment primary key,
passportno int not null,
flightnumber int not null,
`day` date not null,
row varchar(10) not null,
seat varchar(10) not null
);
insert bookings(passportno,flightnumber,`day`,row,seat) values (1,2,'2005-01-01',1,1); -- Joe
insert bookings(passportno,flightnumber,`day`,row,seat) values (1,3,'2005-02-01',1,1); -- Joe
insert bookings(passportno,flightnumber,`day`,row,seat) values (2,3,'2005-02-01',2,2); -- Sally
create table customers
(
passportno int primary key,
name varchar(100) not null,
address varchar(100) not null,
phone varchar(20) not null
);
insert customers(passportno,name,address,phone) values (1,'Joe','addr','ph');
insert customers(passportno,name,address,phone) values (2,'Sally','addr','ph');
Query and results
select c.passportno
from customers c
join bookings b
on c.passportno=b.passportno
group by c.passportno
having count(b.id)>1;
+------------+
| passportno |
+------------+
| 1 |
+------------+
Mysql manual page entitled MySQL Handling of GROUP BY
If I understand the question the query should be something like
SELECT c.PASSPORTNO
FROM CUSTOMERS c
WHERE c.PASSPORTNO IN (SELECT DISTINCT PASSPORTNO
FROM (SELECT PASSPORTNO, FLIGHTNUMBER, COUNT(*)
FROM BOOKINGS
GROUP BY PASSPORTNO, FLIGHTNUMBER
HAVING COUNT(*) > 1)
Best of luck.
Does anyone know why in Oracle 11g when you do a Count(1) with more than one natural join it does a cartesian join and throws the count way off?
Such as
SELECT Count(1) FROM record NATURAL join address NATURAL join person WHERE status=1
AND code = 1 AND state = 'TN'
This pulls back like 3 million rows when
SELECT * FROM record NATURAL join address NATURAL join person WHERE status=1
AND code = 1 AND state = 'TN'
pulls back like 36000 rows, which is the correct amount.
Am I just missing something?
Here are the tables I'm using to get this result.
CREATE TABLE addresses (
address_id NUMBER(10,0) NOT NULL,
address_1 VARCHAR2(60) NULL,
address_2 VARCHAR2(60) NULL,
city VARCHAR2(35) NULL,
state CHAR(2) NULL,
zip VARCHAR2(5) NULL,
zip_4 VARCHAR2(4) NULL,
county VARCHAR2(35) NULL,
phone VARCHAR2(11) NULL,
fax VARCHAR2(11) NULL,
origin_network NUMBER(3,0) NOT NULL,
owner_network NUMBER(3,0) NOT NULL,
corrected_address_id NUMBER(10,0) NULL,
"HASH" VARCHAR2(200) NULL
);
CREATE TABLE rates (
rate_id NUMBER(10,0) NOT NULL,
eob VARCHAR2(30) NOT NULL,
network_code NUMBER(3,0) NOT NULL,
product_code VARCHAR2(2) NOT NULL,
rate_type NUMBER(1,0) NOT NULL
);
CREATE TABLE records (
pk_unique_id NUMBER(10,0) NOT NULL,
rate_id NUMBER(10,0) NOT NULL,
address_id NUMBER(10,0) NOT NULL,
effective_date DATE NOT NULL,
term_date DATE NULL,
last_update DATE NULL,
status CHAR(1) NOT NULL,
network_unique_id VARCHAR2(20) NULL,
rate_id_2 NUMBER(10,0) NULL,
contracted_by VARCHAR2(50) NULL,
contract_version VARCHAR2(5) NULL,
bill_address_id NUMBER(10,0) NULL
);
I should mention this wasn't a problem in Oracle 9i, but when we switched to 11g it became a problem.
My advice would be to NOT use NATURAL JOIN. Explicitly define your join conditions to avoid confusion and "hidden bugs". Here is the official NATURAL JOIN Oracle documentation and more discussion about this subject.
If it happens exactly as you say then it must be an optimiser bug, you should report it to Oracle.
you should try a count(*)
There is a difference between the two.
count(1) signifies count rows where 1 is not null
count(*) signifies count the rows
Just noticed you used 2 natural joins...
From the documentation you can only use a natural join on 2 tables
Natural_Join