Return all null values between two dates - oracle

I have the following query and I need it to return all the null values between those two dates.
select cust_first_name
from customers
join orders using(customer_id)
where order_date between (to_date('01-01-2007','DD-MM-YYYY'))
and (to_date('31-12-2008','DD-MM-YYYY'));

Sounds like what you want is customers with no orders within the given date range. The join you are using finds the opposite of that.
You could do this with an outer join, in which case you need to apply the date filter prior to the join. It's probably easier and more readable to use a NOT IN or NOT EXISTS subquery:
select cust_first_name
from customers
WHERE customers.customer_id NOT IN (
SELECT orders.customer_id from orders
where order_date between (to_date('01-01-2007','DD-MM-YYYY'))
and (to_date('31-12-2008','DD-MM-YYYY'))
)

Here is an example of how to do what you want.
The key part is doing a left join on your orders table, and then simply doing a not between date1 and date2
declare #customers table (
id int identity(1,1),
first_name nvarchar(50),
last_name nvarchar(50)
)
declare #orders table (
id int identity(1,1),
customer_id int,
order_date datetime
)
insert into #customers(first_name, last_name) values ('bob', 'gates')
insert into #customers(first_name, last_name) values ('cyril', 'smith')
insert into #customers(first_name, last_name) values ('harry', 'potter')
insert into #orders(customer_id, order_date) values (1, '2007-02-01')
insert into #orders(customer_id, order_date) values (2, '2015-02-15')
insert into #orders(customer_id, order_date) values (3, '2008-02-15')
select
customers.id
,customers.first_name
,customers.last_name
from #customers customers
left join #orders orders on orders.customer_id = customers.id
where orders.id is null
or orders.order_date not between ('2007-01-01') and ('2008-12-31')
group by
customers.id
,customers.first_name
,customers.last_name;

Related

In oracle SQL DB same primary Id is present more then once with different batch_id. How can I know the batch ID just before the current batch ID

I am working on oracle database.
We load customer data in source table which eventually migrates to target table.
Every time customer data is loaded in source table it is having a unique batch_id.
If we want to update some field in customer table, then we again load the same customer in source table but this time with different batch_id.
Now I want to know batch_id of the customer just before the latest batch_id.
Batch_id we take is usually the current date.
Use ROW_NUMBER analytic function
your sample data
select * from tab
order by 1,2
CUSTOMER_ID BATCH_ID
----------- -------------------
1 09.12.2019 00:00:00
1 10.12.2019 00:00:00
2 10.12.2019 00:00:00
Row_number assihns sequence number starting from 1 for each customer order descending on BATCH_ID - you are interested on one before the latest, i.e. the rows with the number 2.
with cust as (
select
customer_id, batch_id,
row_number() over (partition by customer_id order by batch_id desc) rn
from tab)
select CUSTOMER_ID, BATCH_ID
from cust
where rn = 2;
CUSTOMER_ID BATCH_ID
----------- -------------------
1 09.12.2019 00:00:00
It seems that you're basically looking for the second biggest value in the SOURCE table.
In this example code the SOURCE_TABLE represents the table containing same CUSTOMER_NO with different BATCH_NO:
create table source_table (customer_no integer, batch_no date);
insert into source_table values ('1', SYSDATE-2);
insert into source_table values ('1', SYSDATE-1);
insert into source_table values ('1', SYSDATE);
SELECT batch_no
FROM (
SELECT batch_no, row_number() over (order by batch_no desc) as row_num
FROM source_table
) t
WHERE row_num = 2
Where row_num = 2 represents the second biggest value in the table.
The query returns SYSDATE-1.

SQL delete rows not in another table

I'm looking for a good SQL approach (Oracle database) to fulfill the next requirements:
Delete rows from Table A that are not present in Table B.
Both tables have identical structure
Some fields are nullable
Amount of columns and rows is huge (more 100k rows and 20-30 columns to compare)
Every single field of every single row needs to be compared from Table A against table B.
Such requirement is owing to a process that must run every day as changes will come from Table B.
In other words: Table A Minus Table B => Delete the records from the Table A
delete from Table A
where (field1, field2, field3) in
(select field1, field2, field3
from Table A
minus
select field1, field2, field3
from Table B);
It's very important to mention that a normal MINUS within DELETE clause fails as does not take the nulls on nullable fields into consideration (unknown result for oracle, then no match).
I also tried EXISTS with success, but I have to use NVL function to replace the nulls with dummy values, which I don't want it as I cannot guarantee that the value replaced in NVL will not come as a valid value in the field.
Does anybody know a way to accomplish such thing? Please remember performance and nullable fields as "a must".
Thanks ever
decode finds sameness (even if both values are null):
decode( field1, field2, 1, 0 ) = 1
To delete rows in table1 not found in table2:
delete table1 t
where t.rowid in (select t1.rowid
from table1 t1
left outer join table2 t2
on decode(t1.field1, t2.field1, 1, 0) = 1
and decode(t1.field2, t2.field2, 1, 0) = 1
and decode(t1.field3, t2.field3, 1, 0) = 1
/* ... */
where t2.rowid is null /* no matching row found */
)
to use existing indexes
...
left outer join table2 t2
on (t1.index_field1=t2.index_field1 or
t1.index_field1 is null and t2.index_field1 is null)
and ...
Use a left outer join and test for null in your where clause
delete a
from a
left outer join b on a.x = b.x
where b.x is null
Have you considered ORALCE SQL MERGE statement?
Use Bulk operation for huge number of records. Performance wise it will be faster.
And use join between two table to get rows to be delete. Nullable columns can be compared with some default value.
Also, if you want Table A to be similar as Table B, why don't you truncate table A and then insert data from table b
Assuming you the same PK field available on each table...(Having a PK or some other unique key is critical for this.)
create table table_a (id number, name varchar2(25), dob date);
insert into table_a values (1, 'bob', to_date('01-01-1978','MM-DD-YYYY'));
insert into table_a values (2, 'steve', null);
insert into table_a values (3, 'joe', to_date('05-22-1989','MM-DD-YYYY'));
insert into table_a values (4, null, null);
insert into table_a values (5, 'susan', to_date('08-08-2005','MM-DD-YYYY'));
insert into table_a values (6, 'juan', to_date('11-17-2001', 'MM-DD-YYYY'));
create table table_b (id number, name varchar2(25), dob date);
insert into table_b values (1, 'bob', to_date('01-01-1978','MM-DD-YYYY'));
insert into table_b values (2, 'steve',to_date('10-14-1992','MM-DD-YYYY'));
insert into table_b values (3, null, to_date('05-22-1989','MM-DD-YYYY'));
insert into table_b values (4, 'mary', to_date('12-08-2012','MM-DD-YYYY'));
insert into table_b values (5, null, null);
commit;
-- confirm minus is working
select id, name, dob
from table_a
minus
select id, name, dob
from table_b;
-- from the minus, re-query to just get the key, then delete by key
delete table_a where id in (
select id from (
select id, name, dob
from table_a
minus
select id, name, dob
from table_b)
);
commit;
select * from table_a;
But, if at some point in time, tableA is to be reset to the same as tableB, why not, as another answer suggested, truncate tableA and select all from tableB.
100K is not huge. I can do ~100K truncate and insert on my laptop instance in less than 1 second.
> DELETE FROM purchase WHERE clientcode NOT IN (
> SELECT clientcode FROM client );
This deletes the rows from the purchase table whose clientcode are not in the client table. The clientcode of purchase table references the clientcode of client table.
DELETE FROM TABLE1 WHERE FIELD1 NOT IN (SELECT CLIENT1 FROM TABLE2);

Why does full outer join in HIVE gives weird result when one of the join fields is missing?

I'm comparing the behavior between SQL engines. Oracle has the behavior I would expect from a SQL engine for full outer joins:
Oracle
CREATE TABLE sql_test_a
(
ID VARCHAR2(4000 BYTE),
FIRST_NAME VARCHAR2(200 BYTE),
LAST_NAME VARCHAR2(200 BYTE)
);
CREATE TABLE sql_test_b
(
NUM VARCHAR2(4000 BYTE),
FIRST_NAME VARCHAR2(200 BYTE),
LAST_NAME VARCHAR2(200 BYTE)
);
INSERT INTO sql_test_a (ID, FIRST_NAME, LAST_NAME) VALUES ('1', 'John', 'Snow');
INSERT INTO sql_test_a (ID, FIRST_NAME, LAST_NAME) VALUES ('2', 'Mike', 'Tyson');
INSERT INTO sql_test_b (NUM, FIRST_NAME, LAST_NAME) VALUES ('20', 'Mike', 'Tyson');
When I execute the following, it gives me the expected result. The resulting table contains two rows, with one of the rows containing NULL for the NUM field, because there is no john snow in the table sql_test_b.
SELECT A.FIRST_NAME, A.LAST_NAME, A.ID, B.NUM
FROM
SQL_TEST_A A
FULL OUTER JOIN
SQL_TEST_B B
ON
A.FIRST_NAME = B.FIRST_NAME
AND
A.LAST_NAME = B.LAST_NAME;
You can test the sql script here: http://sqltest.net/
HIVE
In HIVE, however, if you were to try the same thing, the full outer join results in a table with two rows. The row that should be the "John Snow" row contains NULL for the fields FIRST_NAME, LAST_NAME, and NUM. The 1 is filled in for ID, but that's it.
Why such weird behavior in HIVE? Is this a bug? Or am I missing something...because Oracle 11g seems to handle this much better. Thanks.
I could not simulate the result reported by #Candic3
I used the below statements along with the same "select" query as in the question.
CREATE TABLE IF NOT EXISTS sql_test_a (ID String, FIRST_NAME String, LAST_NAME String) COMMENT 'sql_test_a'
ROW FORMAT DELIMITED
FIELDS TERMINATED BY '\t'
LINES TERMINATED BY '\n'
STORED AS TEXTFILE;
CREATE TABLE IF NOT EXISTS sql_test_b (NUM String, FIRST_NAME String, LAST_NAME String) COMMENT 'sql_test_b'
ROW FORMAT DELIMITED
FIELDS TERMINATED BY '\t'
LINES TERMINATED BY '\n'
STORED AS TEXTFILE;
INSERT INTO sql_test_a VALUES ('1', 'John', 'Snow');
INSERT INTO sql_test_a VALUES ('2', 'Mike', 'Tyson');
INSERT INTO sql_test_b VALUES ('20', 'Mike', 'Tyson');
SELECT A.FIRST_NAME, A.LAST_NAME, A.ID, B.NUM
FROM
SQL_TEST_A A
FULL OUTER JOIN
SQL_TEST_B B
ON
A.FIRST_NAME = B.FIRST_NAME
AND
A.LAST_NAME = B.LAST_NAME;
Please find the result attached.
However, select query would return NULL due to unnoticed minor mistakes like data-type mismatch between the DDL and the actual data (say, from flat files) or mismatch among the delimiter mentioned in the DDL and the ones in the actual data.
I think issue with "(" after on condition which is slightly different than traditional sql.
SELECT A.FIRST_NAME, A.LAST_NAME, A.ID, B.NUM
FROM
SQL_TEST_A A
FULL OUTER JOIN
SQL_TEST_B B ON
(A.FIRST_NAME = B.FIRST_NAME AND A.LAST_NAME = B.LAST_NAME);
In select statement you have used A.FIRST_NAME, A.LAST_NAME which is not present for the row from table B. That is why the null value. Instead use COALESCE to find non null value between A.FIRST_NAME and B.FIRST_NAME
SELECT COALESCE(A.FIRST_NAME, B.FIRST_NAME) as FIRST_NAME, COALESCE(A.LAST_NAME, B.LAST_NAME) as LAST_NAME, A.ID, B.NUM
FROM
SQL_TEST_A A
FULL OUTER JOIN
SQL_TEST_B B
ON
A.FIRST_NAME = B.FIRST_NAME
AND
A.LAST_NAME = B.LAST_NAME;

Create a Monthly Report Procedure in ORACLE

Ok this is a tricky one (for me at least) lets say I have the following tables--
TABLES
ORDERS
create table orders (
ono number(5) not null primary key,
cno number(5) references customers,
eno number(4) references employees,
received date,
shipped date);
ODETAILS
create table odetails (
ono number(5) not null references orders,
pno number(5) not null references parts,
qty integer check(qty > 0),
primary key (ono,pno));
PARTS
create table parts(
pno number(5) not null primary key,
pname varchar2(30),
qoh integer check(qoh >= 0),
price number(6,2) check(price >= 0.0),
olevel integer);
TABLES DATA
insert into orders values
(1020,1111,1000,'10-DEC-11','12-DEC-11');
insert into orders values
(1021,1111,1000,'12-JAN-12','15-JAN-12');
insert into orders values
(1022,2222,1001,'13-FEB-12','20-FEB-12');
insert into orders values
(1023,3333,1000,'12-MAR-12',null);
insert into odetails values
(1020,10506,1);
insert into odetails values
(1020,10507,1);
insert into odetails values
(1020,10508,2);
insert into odetails values
(1020,10509,3);
insert into odetails values
(1021,10601,4);
insert into odetails values
(1022,10601,1);
insert into odetails values
(1022,10701,1);
insert into odetails values
(1023,10800,1);
insert into odetails values
(1023,10900,1);
insert into parts values
(10506,'Land Before Time I',200,19.99,20);
insert into parts values
(10507,'Land Before Time II',156,19.99,20);
insert into parts values
(10508,'Land Before Time III',190,19.99,20);
insert into parts values
(10509,'Land Before Time IV',60,19.99,20);
insert into parts values
(10601,'Sleeping Beauty',300,24.99,20);
insert into parts values
(10701,'When Harry Met Sally',120,19.99,30);
insert into parts values
(10800,'Dirty Harry',140,14.99,30);
insert into parts values
(10900,'Dr. Zhivago',100,24.99,30);
Now I'm required to create a procedure which takes in a value as month and generates a report which contains the following--
a. Number of sales
b. Sales value
c. Most popular item
d. Least popular item
My approach
CREATE OR REPLACE PROCEDURE TEST_REPORT
(MONTH_NUMBER IN NUMBER )
AS
PARTS_NUMBER VARCHAR2(10);
SHIPPING_STATUS VARCHAR2(10);
V_ENO VARCHAR2(5);
V_PNO VARCHAR2(5);
SALES NUMBER(30);
V_MONTH VARCHAR2(10);
BEGIN
SELECT RECEIVED INTO V_MONTH FROM ORDERS WHERE
WHERE EXTRACT(MONTH FROM ORDERS.RECEIVED) = MONTH_NUMBER;
SELECT SUM(PRICE*QTY)
INTO SALES
FROM ORDERS,ODETAILS,PARTS
WHERE
EXTRACT(MONTH FROM ORDERS.RECEIVED) = MONTH_NUMBER
END TEST_REPORT;
Then bang my head explodes. I was thinking of using cursors.. but then I thought a SELECT query with different column alias would be simpler. But as it seems I cant get the syntax right and currently If I execute this procedure it gives me a an error
ORA-01422: exact fetch returns more than requested number of rows
As there is more than one record in the table. What approach would be right and logical? and also did I get the syntax right for the procedures? I would appreciate the help and guidance.
Many thanks
(P.S. I condemn the person's naming convention in the script which I had to run to create the tables)
Well, let's see:
Number of sales (I'm assuming "of each part"):
SELECT od.PNO, COUNT(*) AS SALE_COUNT
FROM ODETAILS od
INNER JOIN ORDERS o
ON od.ONO = o.ONO
WHERE EXTRACT(MONTH FROM o.RECEIVED) = &MONTH_NUMBER
GROUP BY od.PNO
Sales value:
SELECT od.PNO, p.PRICE, SUM(od.QTY) AS SALES_QTY, SUM(od.QTY * p.PRICE) AS SALES_VALUE
FROM ODETAILS od
INNER JOIN PARTS p
ON p.PNO = od.PNO
GROUP BY od.PNO, p.PRICE
Most popular item (just take the first row):
SELECT od.PNO, SUM(od.QTY) AS TOTAL_QTY
FROM ODETAILS od
GROUP BY od.PNO
ORDER BY SUM(od.QTY) DESC
Least popular item (which has any sales) (just take the first row):
SELECT od.PNO, SUM(od.QTY) AS TOTAL_QTY
FROM ODETAILS od
GROUP BY od.PNO
ORDER BY SUM(od.QTY) ASC
Number of order cancellations and value, number of returns and values, item that has highest return: there does not appear to be data in the question (at this point) which would supply this information. I'll add a comment to the question to this effect. If further information or explanations are forthcoming I'll include this in my answer.
Best of luck.
Both these queries have issues.
Below query will return more than one record from ORDERS table. INTO clause will work only when the query returns exactly one record at a time.
SELECT RECEIVED INTO V_MONTH FROM ORDERS WHERE
WHERE EXTRACT(MONTH FROM ORDERS.RECEIVED) = MONTH_NUMBER;
This query have 2 issues, no join between ORDERS, ODETAILS and PARTS as well as INTO clause. Here also it will return more than one record.
SELECT SUM(PRICE*QTY)
INTO SALES
FROM ORDERS,ODETAILS,PARTS
WHERE
EXTRACT(MONTH FROM ORDERS.RECEIVED) = MONTH_NUMBER
END TEST_REPORT;
You need to define your requirement to suggest approach for your application.

Remove duplicated records based on pair of columns?

I have a table with the folowwing columns:
id
app_id
tag_id
created_at
updated_at
The pair app_id and tag_id should be uniq. How to remove oldest duplicated records based on updated_at column to determine "oldest" and pair (app_id, tag_id) to determine "duplicated"?
ActiveRecord::Base.connection.execute <<-SQL
delete from my_table where id in (
select t1.id
from my_table t1
inner join (
select app_id, tag_id, min(updated_at) as oldest
from my_table
group by app_id, tag_id
) t2
on t1.app_id = t2.app_id
and t1.tag_id = t2.tag_id
and t1.updated_at = t2.oldest
)
SQL
Replace my_table with your actual table name.

Resources