Alternative to window function in MariaDB 10.1 - syntax

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

Related

Foreign key keeps saying not enough values

I have been trying to get this thing working for a few day now (foreign key) and it just don't work, and feel like every solution I used don't work, so i'm asking here to learn what was the problem and how to fix it
Table creation :
CREATE TABLE CUSTOMER
(
Customer_ID varchar(255) NOT NULL,
Customer_Name varchar(50),
Customer_Gender varchar(10),
Customer_DOB varchar(20) ,
CONSTRAINT CUSTOMER_PK PRIMARY KEY(Customer_ID)
) ;
CREATE TABLE PAYMENT
(
Payment_ID varchar(255) NOT NULL,
Cust_ID varchar(255),
Payment_Method varchar(30),
Payment_Date varchar(20),
Payment_Total NUMBER(10,2) ,
CONSTRAINT PAYMENT_PK PRIMARY KEY(Payment_ID),
CONSTRAINT fk_customer FOREIGN KEY(Cust_ID) REFERENCES CUSTOMER(Customer_ID)
) ;
Inserting values :
INSERT INTO CUSTOMER VALUES ('1277','Jenny','Female', ( TO_Date ( '03/04/1988' , 'DD/MM/yyyy')));
INSERT INTO CUSTOMER VALUES ('3423','Bryan','Male', ( TO_Date ( '15/06/1990' , 'DD/MM/YYYY')));
INSERT INTO CUSTOMER VALUES ('4385','Mohd Shafik','Male',( TO_Date ( '20/08/1993' , 'DD/MM/YYYY')));
INSERT INTO PAYMENT VALUES ('24P','Cash', ( TO_Date ( '11/02/2022' , 'DD/MM/YYYY')),24.50);
INSERT INTO PAYMENT VALUES ('09p','Online Transfer', ( TO_Date ( '08/04/2022' , 'DD/MM/YYYY')),25.00);
INSERT INTO PAYMENT VALUES ('10P','Cash', ( TO_Date ( '08/07/2022' , 'DD/MM/YYYY')),22.50);
The foreign keys are now working , but just for life of me can't figure out why the it spits out ORA-00947: not enough values and
ORA-01400: cannot insert NULL into ("SQL_GUUNNGDQAOXJVYPBKNMILVXJR"."PAYMENT"."PAYMENT_ID") ORA-06512: at "SYS.DBMS_SQL", line 1721
Also, Please explain to me how references works, i read a few places but they use words that just confuse me. Please and thank you!
Use DATE data types to store date values (and VARCHAR2 instead of VARCHAR) in your tables:
CREATE TABLE CUSTOMER(
Customer_ID varchar2(255) NOT NULL,
Customer_Name varchar2(50),
Customer_Gender varchar2(10),
Customer_DOB DATE,
CONSTRAINT CUSTOMER_PK PRIMARY KEY(Customer_ID)
);
CREATE TABLE PAYMENT(
Payment_ID varchar2(255) NOT NULL,
Cust_ID varchar2(255),
Payment_Method varchar2(30),
Payment_Date DATE,
Payment_Total NUMBER(10,2) ,
CONSTRAINT PAYMENT_PK PRIMARY KEY(Payment_ID),
CONSTRAINT fk_customer FOREIGN KEY(Cust_ID) REFERENCES CUSTOMER(Customer_ID)
);
Then name the columns in your INSERT statements:
INSERT INTO CUSTOMER (
customer_id, customer_name, customer_gender, customer_dob
) VALUES (
'1277','Jenny','Female', TO_Date('03/04/1988', 'DD/MM/yyyy')
);
INSERT INTO CUSTOMER (
customer_id, customer_name, customer_gender, customer_dob
) VALUES (
'3423','Bryan','Male', TO_Date('15/06/1990', 'DD/MM/YYYY')
);
INSERT INTO CUSTOMER (
customer_id, customer_name, customer_gender, customer_dob
) VALUES (
'4385','Mohd Shafik','Male', TO_Date('20/08/1993' , 'DD/MM/YYYY')
);
Then for the PAYMENT inserts, you have 5 columns in the table but only 4 pieces of data being inserted:
INSERT INTO PAYMENT (
payment_id, payment_method, payment_date, payment_total
) VALUES (
'24P','Cash', TO_Date('11/02/2022', 'DD/MM/YYYY'),24.50
);
INSERT INTO PAYMENT (
payment_id, payment_method, payment_date, payment_total
) VALUES (
'09p','Online Transfer', TO_Date( '08/04/2022' , 'DD/MM/YYYY'),25.00
);
INSERT INTO PAYMENT (
payment_id, payment_method, payment_date, payment_total
) VALUES (
'10P','Cash', TO_Date('08/07/2022', 'DD/MM/YYYY'),22.50
);
You have not provided a Cust_ID value so it will default to NULL in those rows.
If you want to provide a Cust_ID then add it to the statement:
INSERT INTO PAYMENT (
payment_id, cust_id, payment_method, payment_date, payment_total
) VALUES (
'ABC', '1277', 'Cash', TO_Date('08/07/2022', 'DD/MM/YYYY'),22.50
);
db<>fiddle here
ALTER SESSION SET NLS_DATE_FORMAT = 'DD-MON-YYYY';
CREATE TABLE CUSTOMER
(
Customer_ID varchar(255) NOT NULL,
Customer_Name varchar(50),
Customer_Gender varchar(10),
Customer_DOB DATE ,
CONSTRAINT CUSTOMER_PK PRIMARY KEY(Customer_ID)
) ;
CREATE TABLE PAYMENT
(
Payment_ID varchar(255) NOT NULL,
Customer_ID varchar(255),
Payment_Method varchar(30),
Payment_Date DATE,
Payment_Total NUMBER(10,2) ,
CONSTRAINT PAYMENT_PK PRIMARY KEY(Payment_ID),
CONSTRAINT fk_customer FOREIGN KEY(Customer_ID) REFERENCES CUSTOMER(Customer_ID)
) ;
INSERT INTO CUSTOMER VALUES ('1277','Jenny','Female', ( TO_Date ( '03/04/1988' , 'DD/MM/yyyy')));
INSERT INTO PAYMENT VALUES ('24P','1277','Cash', ( TO_Date ( '11/02/2022' , 'DD/MM/YYYY')),24.50);
SELECT * FROM Customer;
CUSTOMER_ID CUSTOMER_NAME CUSTOMER_GENDER CUSTOMER_DOB
1277 Jenny Female 03-APR-1988
SELECT * FROM payment;
PAYMENT_ID CUSTOMER_ID PAYMENT_METHOD PAYMENT_DATE PAYMENT_TOTAL
24P 1277 Cash 11-FEB-2022 24.5

Oracle : It give a error in a dynamic view

I have two table one is employee and one is department. I am creating the dynamic view that will rank all departments by salary. The view should pull information from Department and Employee, sum the salary by department, and rank the department by salary.
CREATE TABLE DEPARTMENT
(DEPARTMENT_ID NUMBER PRIMARY KEY,
DEPARTMENT_NAME VARCHAR(30) NOT NULL
);
CREATE TABLE JOBS
(JOB_ID NUMBER PRIMARY KEY,
JOB_TITLE VARCHAR(35) NOT NULL,
MIN_SALARY DECIMAL NOT NULL,
MAX_SALARY DECIMAL NOT NULL
);
CREATE TABLE EMPLOYEES
(EMPLOYEE_ID NUMBER PRIMARY KEY,
FIRST_NAME VARCHAR(20) NOT NULL,
LAST_NAME VARCHAR(25) NOT NULL,
EMAIL VARCHAR(25) NOT NULL,
PHONE_NUMBER VARCHAR(20) NOT NULL,
HIRE_DATE DATE NOT NULL,
JOB_ID NUMBER NOT NULL,
SALARY DECIMAL NOT NULL,
DEPARTMENT_ID NUMBER NOT NULL,
CONSTRAINT emp_job_fk FOREIGN KEY(JOB_ID) REFERENCES JOBS(JOB_ID),
CONSTRAINT emp_department_fk FOREIGN KEY(DEPARTMENT_ID) REFERENCES DEPARTMENT(DEPARTMENT_ID)
);
INSERT INTO DEPARTMENT (DEPARTMENT_ID,DEPARTMENT_NAME)
VALUES(1,'IT');
INSERT INTO DEPARTMENT (DEPARTMENT_ID,DEPARTMENT_NAME)
VALUES(2,'Sales');
INSERT INTO JOBS (JOB_ID,JOB_TITLE,MIN_SALARY,MAX_SALARY)
VALUES (1,'IT Administrator',250000.00,50000.00);
INSERT INTO JOBS (JOB_ID,JOB_TITLE,MIN_SALARY,MAX_SALARY)
VALUES (2,'Salesman',200000.00,40000.00);
Here is I create so far but it give me a error
ORA-00979: not a GROUP BY expression
00979. 00000 - "not a GROUP BY expression"
*Cause:
*Action:
Error at Line: 4 Column: 9
Here is my code
select department_id,department_name,total_salary
from(
select department_id,department_name, SALARY, count(*) as total_salary from(
select dep.department_id , dep.department_name ,emp.SALARY,
DENSE_RANK() OVER (PARTITION BY department_name ORDER BY salary)
from departments dep
inner join employees emp on dep.DEPARTMENT_ID = emp.DEPARTMENT_ID
)
GROUP BY SALARY)
Your query needs to join EMPLOYEES (to get the salaries) to DEPARTMENT (to get the DEPARTMENT_NAME). Calculate the total salary for each department by summing the employee salaries, not counting them. The GROUP BY needs to include the non-aggregated columns.
Then you need to rank the departments by the total salary per department. This query ranks the departments with highest salary = 1. It uses a left join to cater for departments with no employees.
select department_id
, department_name
, total_salary
, rank() over (order by total_salary desc) as dept_rank
from (
select d.department_id
, d.department_name
, sum(e.SALARY) as total_salary
from department d
left join employees e
on e.department_id = d.department_id
group by d.department_id
, d.department_name
)
/

Error: ora-00917: missing comma

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).

handling exception for insert into select from

SCHEMA 1
I have table transaction table
create table TXN_HEADER
(
txn_id NUMBER(10) not null,
txn_Date date
product_id NUMBER(10),
company_id NUMBER(10),
dealer_id NUMBER(10),
tran_amt number(10,2)
)
The above table having foreign key references to product.product_id and company.company_id.
This table having 5m rows
SCHEMA 2
create table TXN_HEADER_REPORTS
(
txn_id NUMBER(10) not null,
txn_Date date
product_id NUMBER(10),
company_id NUMBER(10),
dealer_id NUMBER(10),
tran_amt number(10,2)
)
here also we have the same constraints , having foreign key references to product.product_id and company.company_id.
in schema 2 we are trying to insert all the rows from schemea 1 to schema 2 in one shot, like this
begin
insert into TXN_HEADER_REPORTS (
txn_id, txn_Date ,product_id,company_id , dealer_id , tran_amt)
select
txn_id, txn_Date ,product_id,company_id , dealer_id , tran_amt
from schema1.TXN_HEADER;
commit;
exception
when others then
< ... procedure to log the errors >
end;
now we are trying to execute the above procedure , and it failed due to foreign key constraint of one rows. But entire my transaction rollback. Actually i dont want to use cursor to process the rows one by one , at it takes long time. So i used to "insert into .. SElect from " but due to constraints of 1 row all my transaction not moved to schema2.txn_Extract_hdr.
Is there any way to trap only that failed and to process the other rows without terminating
Please advice ..
You can create an error log table, and then use a single insert:
exec dbms_errlog.create_error_log(dml_table_name => 'TXN_HEADER_REPORTS');
insert into TXN_HEADER_REPORTS ( txn_id, txn_Date ,product_id,company_id ,
dealer_id , tran_amt)
select txn_id, txn_Date ,product_id,company_id , dealer_id , tran_amt
from schema1.TXN_HEADER
log errors into ERR$_TXN_HEADER_REPORTS reject limit unlimited;
Any rows that can't inserted will be recorded in the ERR table. Note that this is plain SQL, it doesn't need to be in a PL/SQL block.
Read more in the documentation.
I don't understand your constraint.
Does your insert fail because the product_id and company_id don't exists in schema2?
In that case, it may be better to insert the missing company and product records before you insert records into TXN_HEADER_REPORTS of schema2.
insert into company com_sch2
(col1, col2, col2,...)
select col1, col2, col3, ...
from schema1.company com_sch1
where not exists (select 'x'
from company com2
where com2.company_id = com_sch1.company_id);
And the seem for the product table.

Create View inner join error

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

Resources