Get max id for each entry from another related table - sql

I have 2 tables namely entry and entry_log whose schema is as below:
entry:
id NUMBER PRIMARY KEY
name VARCHAR2(100)
entry_log:
id NUMBER PRIMARY KEY
parent_id NUMBER NOT NULL --> constraint el FOREIGN KEY (parent_id) REFERENCES entry ( id )
user_id NUMBER NOT NULL --> constaint rl_uk FOREIGN KEY (user_id) REFERENCES user ( id )
Note: User is another table that I have.
For every row in entry table I can have multiple rows in the entry_log table, where entry log table actually holds parent_id and the user who made the modification to a row in entry table.
Basically, entry is the actual table and a row is inserted in the entry_log table every time a create or update occurs in the entry table.
I need to have a query such that it returns the following columns:
id from entry table
name from entry table
max(id) from entry_log table where parent_id in entry_log = id in entry table
I have the below query which works but I want to achieve this without having to use subquery to improve performance.
select max(log_id) as log_id from (
SELECT
entry_log.id log_id,
entry_log.parent_id
FROM
entry
INNER JOIN entry_log ON entry_log.parent_id = entry.id
) group by parent_id
)
SELECT
entry.id,
entry.name,
mif.log_id "MAX_ID",
FROM
entry
INNER JOIN entry_log ON entry_log.parent_id = entry.id
INNER JOIN max_id_finder mif ON mif.log_id = entry_log.id
WHERE 1=1
Is there any better way to achieve this without impacting performance?

Pl check if the following solution works for you:
select a.*, b.* from entry a
inner join (select id, max(p_id) as p_id, `name` from entry_log group by p_id) b
on a.id = b.p_id
Dataset used:
CREATE TABLE `entry` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1;
CREATE TABLE `entry_log` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`p_id` int(11) DEFAULT NULL,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `fk_p_id` (`p_id`),
CONSTRAINT `fk_p_id` FOREIGN KEY (`p_id`) REFERENCES `entry` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=19 DEFAULT CHARSET=latin1;
insert into entry values (1, "A1");
insert into entry values (2, "A2");
insert into entry values (3, "A3");
insert into entry values (4, "A4");
insert into entry values (5, "A5");
insert into entry values (6, "A6");
insert into entry values (7, "A7");
insert into entry values (8, "A8");
insert into entry_log values (1, 1, "P001");
insert into entry_log values (2, 2, "P002");
insert into entry_log values (3, 1, "P003");
insert into entry_log values (4, 2, "P004");
insert into entry_log values (5, 3, "P005");
insert into entry_log values (6, 1, "P006");
insert into entry_log values (7, 2, "P007");
insert into entry_log values (8, 5, "P008");
insert into entry_log values (9, 2, "P007");
insert into entry_log values (10, 4, "P008");
insert into entry_log values (11, 7, "P001");
insert into entry_log values (12, 8, "P002");
insert into entry_log values (13, 6, "P003");
insert into entry_log values (14, 6, "P004");
insert into entry_log values (15, 3, "P005");
insert into entry_log values (16, 7, "P006");
insert into entry_log values (17, 2, "P007");
insert into entry_log values (18, 5, "P008");

Thanks user1300830
The below query works perfectly.
SELECT
entry.id,
entry.name,
entry_log.log_id,
FROM
entry
INNER JOIN (SELECT max(id) as log_id, parent_id from entry_log group by parent_id) entry_log ON entry_log.parent_id = entry.id
WHERE 1=1

Related

SQL script for multiple reservations by one customer (Restaurant)

I need my SQL db to be able to hold more than one reservation for a customer.
However I get some duplicate pk errors.
Here is my SQL script: (I wanna be able to let one customer have more than 1 reserv.)
/*
db_name: Restaurant DB
update_timestamp: 6.10.2021 # 09:48
sign: The Actual Daniel
*/
DROP DATABASE IF EXISTS restaurantDB;
CREATE DATABASE restaurantDB;
USE restaurantDB;
CREATE TABLE `bill` (
`billID` int NOT NULL AUTO_INCREMENT,
`payAmount` varchar(255) NOT NULL,
PRIMARY KEY (`billID`)
);
INSERT INTO `bill` VALUES (1, '$258');
INSERT INTO `bill` VALUES (2, '$420');
INSERT INTO `bill` VALUES (3, '$114');
INSERT INTO `bill` VALUES (4, '$88');
INSERT INTO `bill` VALUES (5, '$137');
INSERT INTO `bill` VALUES (6, '$286');
INSERT INTO `bill` VALUES (7, '$97');
INSERT INTO `bill` VALUES (8, '$110');
INSERT INTO `bill` VALUES (9, '$358');
INSERT INTO `bill` VALUES (10, '$190');
CREATE TABLE `customer` (
`customerID` int NOT NULL AUTO_INCREMENT,
`firstName` varchar(255) NOT NULL,
`lastName` varchar(255) NOT NULL,
`phone` varchar(20) NOT NULL,
PRIMARY KEY (`customerID`)
);
INSERT INTO `customer` VALUES (1, 'Nero', 'Lee', '420420589');
INSERT INTO `customer` VALUES (2, 'Lucius', 'Malt', '578632221');
INSERT INTO `customer` VALUES (3, 'Aurelian', 'Keen', '639456773');
INSERT INTO `customer` VALUES (4, 'Jasmine', 'Wang', '227453891');
INSERT INTO `customer` VALUES (5, 'Sophie', 'Clinck', '905674321');
INSERT INTO `customer` VALUES (6, 'Augustus', 'Smith', '523764112');
INSERT INTO `customer` VALUES (7, 'Isabelle', 'Niaom', '786554330');
INSERT INTO `customer` VALUES (8, 'Alix', 'Black', '009886775');
INSERT INTO `customer` VALUES (9, 'Leia', 'Mudworth', '345667228');
INSERT INTO `customer` VALUES (10, 'Claudius', 'Reed', '427654221');
CREATE TABLE `meal` (
`mealID` int NOT NULL AUTO_INCREMENT,
`sides` varchar(255) NULL,
`main` varchar(255) NULL,
`beverage` varchar(255) NOT NULL,
PRIMARY KEY (`mealID`)
);
INSERT INTO `meal` VALUES (1, 'potatos', 'steak', 'wine');
INSERT INTO `meal` VALUES (2, 'ice', 'cube steak', 'spring water');
INSERT INTO `meal` VALUES (3, 'rice', 'cake', 'beer');
INSERT INTO `meal` VALUES (4, 'salad', 'pasta', 'wine');
INSERT INTO `meal` VALUES (5, 'radish', 'noodles', 'soju');
INSERT INTO `meal` VALUES (6, 'rice', 'rice', 'pumpkin juice');
INSERT INTO `meal` VALUES (7, 'butter, honey', 'steak', 'wine');
INSERT INTO `meal` VALUES (8, 'ice', 'salad', 'mineral water');
INSERT INTO `meal` VALUES (9, 'sweet potatos', 'pork', 'beer');
INSERT INTO `meal` VALUES (10, 'sourdough bread', 'fruit bowl', 'mineral water');
CREATE TABLE `order` (
`orderID` int NOT NULL,
`time` datetime NOT NULL,
PRIMARY KEY (`orderID`)
);
INSERT INTO `order` VALUES (1, '21-09-27 13:36:06');
INSERT INTO `order` VALUES (2, '21-02-14 17:18:03');
INSERT INTO `order` VALUES (3, '21-03-27 11:34:16');
INSERT INTO `order` VALUES (4, '21-07-14 15:58:32');
INSERT INTO `order` VALUES (5, '21-02-27 18:16:33');
INSERT INTO `order` VALUES (6, '21-01-14 19:28:12');
INSERT INTO `order` VALUES (7, '21-08-27 16:27:07');
INSERT INTO `order` VALUES (8, '21-06-14 11:21:09');
INSERT INTO `order` VALUES (9, '21-11-27 16:30:03');
INSERT INTO `order` VALUES (10, '21-03-14 17:22:11');
CREATE TABLE `reservation` (
`reservationID` int NOT NULL AUTO_INCREMENT,
`checkIn` datetime NOT NULL,
`checkOut` datetime NOT NULL,
`customerForeign` int NOT NULL,
PRIMARY KEY (`reservationID`),
FOREIGN KEY (`customerForeign`) REFERENCES customer(customerID)
);
INSERT INTO `reservation` VALUES (1, '21-09-27 13:24:06', '21-09-27 14:17:23', 1);
INSERT INTO `reservation` VALUES (2, '21-02-14 17:25:03', '21-02-14 19:22:14', 2);
INSERT INTO `reservation` VALUES (3, '21-03-27 11:34:16', '21-03-27 13:34:16', 3);
INSERT INTO `reservation` VALUES (4, '21-07-14 15:58:32', '21-07-14 16:58:32', 4);
INSERT INTO `reservation` VALUES (5, '21-02-27 18:16:33', '21-02-27 19:16:33', 5);
INSERT INTO `reservation` VALUES (6, '21-01-14 19:28:12', '21-01-14 20:28:12', 6);
INSERT INTO `reservation` VALUES (7, '21-08-27 16:27:07', '21-08-27 17:27:07', 7);
INSERT INTO `reservation` VALUES (8, '21-06-14 11:21:09', '21-06-14 14:21:09', 8);
INSERT INTO `reservation` VALUES (9, '21-11-27 16:30:03', '21-11-27 17:30:03', 9);
INSERT INTO `reservation` VALUES (10, '21-03-14 17:22:11', '21-03-14 19:22:11', 10);
CREATE TABLE `table` (
`tableID` int NOT NULL,
`numOfSeats` int NOT NULL,
PRIMARY KEY (`tableID`)
);
INSERT INTO `table` VALUES (1, 1);
INSERT INTO `table` VALUES (2, 2);
INSERT INTO `table` VALUES (3, 2);
INSERT INTO `table` VALUES (4, 2);
INSERT INTO `table` VALUES (5, 4);
INSERT INTO `table` VALUES (6, 4);
INSERT INTO `table` VALUES (7, 6);
INSERT INTO `table` VALUES (8, 6);
INSERT INTO `table` VALUES (9, 8);
INSERT INTO `table` VALUES (10, 12);
ALTER TABLE `bill` ADD CONSTRAINT `fk_bill_order_1` FOREIGN KEY (`billID`) REFERENCES `order` (`orderID`);
ALTER TABLE `meal` ADD CONSTRAINT `fk_meal_order_1` FOREIGN KEY (`mealID`) REFERENCES `order` (`orderID`);
ALTER TABLE `order` ADD CONSTRAINT `fk_order_customer_1` FOREIGN KEY (`orderID`) REFERENCES `customer` (`customerID`);
ALTER TABLE `reservation` ADD CONSTRAINT `fk_reservation_customer_1` FOREIGN KEY (`reservationID`) REFERENCES `customer` (`customerID`);
ALTER TABLE `table` ADD CONSTRAINT `fk_table_reservation_1` FOREIGN KEY (`tableID`) REFERENCES `reservation` (`reservationID`);
This is error I get when trying to insert new reservation for customer with ID of 1

Can I reference this table?

I am trying to show amount paid for each tutor sorted by month and then by tutor id. I have the first part correct and can sort by month but cannot sort by tutor id because it is from a different table.
Here is the script for my tables:
create table match_history
(match_id number(3),
tutor_id number(3),
student_id number(4),
start_date date,
end_date date,
constraint pk_match_history primary key (match_id),
constraint fk1_match_history foreign key (tutor_id) references tutor(tutor_id),
constraint fk2_match_history foreign key (student_id) references student(student_id));
create table tutor_report
(match_id number(3),
month date,
hours number(3),
lessons number(3),
constraint pk_tutor_report primary key (match_id, month),
constraint fk1_tutor_report foreign key (match_id) references match_history(match_id));
insert into tutor values (100, '05-JAN-2017', 'Active');
insert into tutor values (101, '05-JAN-2017', 'Temp Stop');
insert into tutor values (102, '05-JAN-2017', 'Dropped');
insert into tutor values (103, '22-MAY-2017', 'Active');
insert into tutor values (104, '22-MAY-2017', 'Active');
insert into tutor values (105, '22-MAY-2017', 'Temp Stop');
insert into tutor values (106, '22-MAY-2017', 'Active');
insert into student values (3000, 2.3);
insert into student values (3001, 5.6);
insert into student values (3002, 1.3);
insert into student values (3003, 3.3);
insert into student values (3004, 2.7);
insert into student values (3005, 4.8);
insert into student values (3006, 7.8);
insert into student values (3007, 1.5);
insert into match_history values (1, 100, 3000, '10-JAN-2017', null);
insert into match_history values (2, 101, 3001, '15-JAN-2017', '15-MAY-2017');
insert into match_history values (3, 102, 3002, '10-FEB-2017', '01-MAR-2017');
insert into match_history values (4, 106, 3003, '28-MAY-2017', null);
insert into match_history values (5, 103, 3004, '01-JUN-2017', '15-JUN-2017');
insert into match_history values (6, 104, 3005, '01-JUN-2017', '28-JUN-2017');
insert into match_history values (7, 104, 3006, '01-JUN-2017', null);
insert into tutor_report values (1, '01-JUN-2017', 8, 4);
insert into tutor_report values (4, '01-JUN-2017', 8, 6);
insert into tutor_report values (5, '01-JUN-2017', 4, 4);
insert into tutor_report values (4, '01-JUL-2017', 10, 5);
insert into tutor_report values (1, '01-JUL-2017', 4, 2);
This is what I have so far:
Select (hours * 10) as amount paid from tutor_report group by month, tutor_id
however obviously I cannot just say tutor_id at the end.
You can join match_history to get the tutor_id.
But your statement and the query don't match. If you want to sort use ORDER BY.
SELECT tr.hours * 10 amount_paid
FROM tutor_report tr
INNER JOIN match_history mh
ON mh.match_id = tr.match_id
ORDER BY tr.month,
mh.tutor_id;
If you want to aggregate, hours needs to be argument to some aggregation function. Maybe you're after the sum of hours?
SELECT sum(tr.hours) * 10 amount_paid
FROM tutor_report tr
INNER JOIN match_history mh
ON mh.match_id = tr.match_id
GROUP BY tr.month,
mh.tutor_id;
If you are grouping based on columns on two tables,you need to join them on the matching Id and then use group by
Select (hours * 10) as amount paid
from tutor_report a
join match_history b on a. match_id = b.match_id
group by month, tutor_id

Running an SQL Script file and having issues printing the Current_Orders table cant understand the errors

I have to prepare and execute a script file to create and populate a relational database in Oracle with data about customers, current orders, and products. These are the relationships: Each customer can place any number of current orders and each current order is placed by one customer.Each current order is for one product but each product can be in any number of
current orders.
I can't figure out the errors when running this script. The Customer and Products table are successfully filled, but the Current_Orders table is not. There are a few error codes that appear when I run it but I am not understanding them as I am fairly new to SQL. Any help would be appreciated. Thanks! ORA-00001, ORA-00955, ORA-02449.
drop table Customer;
drop table Current_Orders;
drop table Products;
create table Customer
(Customer_Number integer not null,
Customer_Name char(20) not null,
Customer_City char(20) not null,
PRIMARY KEY(Customer_Number));
create table Products
(Product_Number integer not null,
Product_Description char(20) not null,
Unit_Price integer not null,
PRIMARY KEY (Product_Number));
create table Current_Orders
(Order_Number integer not null,
Order_Date date not null,
Shipping_Method char(20) not null,
Product_Number integer not null,
Quantity_Ordered integer not null,
Customer_Number integer not null,
PRIMARY KEY(Order_Number), FOREIGN KEY(Customer_Number)
references Customer(Customer_Number) ON DELETE CASCADE,
FOREIGN KEY(Product_Number) references Products(Product_Number)
ON DELETE CASCADE);
insert into Customer
values (1, 'Khizur Sheikh', 'Milpitas');
insert into Customer
values (2, 'Fatima Sheikh', 'Fremont');
insert into Customer
values (3, 'Syed Sheikh', 'Davis');
insert into Customer
values (4, 'Mohammed Sheikh', 'Oakland');
insert into Customer
values (5, 'Ali Sheikh', 'Dublin');
insert into Products
values (6, 'iphone', 300);
insert into Products
values (7, 'ipad', 400);
insert into Products
values (8, 'imac', 500);
insert into Products
values (9, 'ipod', 600);
insert into Products
values (10, 'ihome', 700);
insert into Products
values (11, 'apple', 800);
insert into Products
values (12, 'banana', 900);
insert into Products
values (13, 'orange', 1000);
insert into Products
values (14, 'grape', 1100);
insert into Products
values (15, 'avocado', 1200);
insert into Products
values (16, 'bread', 1300);
insert into Products
values (17, 'muffin', 1400);
insert into Products
values (18, 'cheese', 1500);
insert into Products
values (19, 'milk', 1600);
insert into Products
values (20, 'brownies', 1700);
insert into Products
values (21, 'candy', 1800);
insert into Products
values (22, 'soup', 1900);
insert into Products
values (23, 'strawberry', 11000);
insert into Products
values (24, 'cookies', 50);
insert into Products
values (25, 'chocolate', 10);
insert into Current_Orders
values (26, '22-oct-2017', 'truck', 6, 1, 1);
insert into Current_Orders
values (27, '22-nov-2017', 'ship', 7, 1, 2);
insert into Current_Orders
values (28, '22-dec-2017', 'train', 8, 3, 3);
insert into Current_Orders
values (29, '22-jan-2017', 'truck', 9, 2, 4);
insert into Current_Orders
values (30, '22-feb-2017', 'train', 10, 1, 5);
insert into Current_Orders
values (31, '22-mar-2017', 'truck', 12, 4, 2);
insert into Current_Orders
values (32, '22-apr-2017', 'plane', 17, 7, 4);
insert into Current_Orders
values (33, '22-may-2017', 'train', 19, 1, 5);
insert into Current_Orders
values (34, '22-jun-2017', 'ship', 22, 3, 2);
insert into Current_Orders
values (35, '22-jan-2017', 'ship', 21, 4, 3);
commit;
All the commands in your script work fine while running for the first time. However Issue seems to occur when you run the DROP TABLE statements on second and successive executions when table exists and has data.
drop table Customer;
drop table Current_Orders;
Error report - ORA-02449: unique/primary keys in table referenced by
foreign keys
02449. 00000 - "unique/primary keys in table referenced by foreign keys"
*Cause: An attempt was made to drop a table with unique or
primary keys referenced by foreign keys in another table.
*Action: Before performing the above operations the table, drop the
foreign key constraints in other tables.
I see you have defined a FOREIGN KEY on the column Customer_Number
FOREIGN KEY(Customer_Number)
references Customer(Customer_Number) ON DELETE CASCADE
So, when you try to DROP from Table Customer before Current_Orders , you are basically deleting parent record before deleting the child record which is not allowed. ORA-00955 and ORA-00001 occur as a consequence.
So, follow this order to drop tables in your script.
drop table Current_Orders;
drop table Products;
drop table Customer;

postgresql outer join query to get all data from one table

I have two tables
CREATE TABLE REFERENCE_VALUES
(
REFERENCE_ID SERIAL,
REFERENCE_OBJ_NAME TEXT,
REFERENCE_VALUE_CODE BIGINT,
DISPLAY_NAME TEXT,
CREATED_ON TIMESTAMP,
MODIFIED_ON TIMESTAMP
);
ALTER TABLE REFERENCE_VALUES
ADD PRIMARY KEY (ID);
ALTER TABLE REFERENCE_VALUES
ADD FOREIGN KEY (REFERENCE_VALUE_CODE)
REFERENCES CATEGORY(ID);
CREATE TABLE CATEGORY
(
ID BIGSERIAL NOT NULL,
CODE TEXT NOT NULL,
NAME TEXT NOT NULL,
PARENT_ID BIGINT,
PATH TEXT,
COMP_ID BIGINT,
CREATED_ON TIMESTAMP,
MODIFIED_ON TIMESTAMP,
);
ALTER TABLE CATEGORY
ADD PRIMARY KEY (ID);
ALTER TABLE CATEGORY
ADD FOREIGN KEY (COMP_ID)
REFERENCES COMPANY_ID(ID);
I have the display names as DISPLAY_NAME in REFERENCE_VALUES table I want to SELECT all values from table CATEGORY but the value of NAME (In CATEGORY table) replaced by value from DISPLAY_NAME in REFERENCE_VALUES and if the value of of DISPLAY_NAME is NULL OR EMPTY i would keep the value of NAME (CATEGORY table).
I have been able to do that with the query below
SELECT C.ID, C.CODE, COALESCE(R.DISPLAY_NAME, C.NAME) as NAME, C.PARENT_ID, C.PATH, C.COMP_ID, C.CREATED_ON, C.MODIFIED_ON
FROM CATEGORY C
LEFT JOIN REFERENCE_VALUES R
ON C.ID = R.REFERENCE_VALUE_CODE
WHERE R.REFERENCE_OBJ_NAME = 'CATEGORY';
but i am getting only two records. How can i get all the records from the category table?
sample data to populate the tables
INSERT INTO CATEGORY VALUES (1, 'CVB', 'COMM VEH', NULL, 'CVB', 1, '2016-05-13 15:50:19.985', NULL);
INSERT INTO CATEGORY VALUES (2, 'LVB', 'AUTO', NULL, 'LVB', 1, '2016-05-13 15:50:19.994', NULL);
INSERT INTO CATEGORY VALUES (3, 'INB', 'INF', NULL, 'INB', 1, '2016-05-13 15:50:19.997', NULL);
INSERT INTO CATEGORY VALUES (4, 'OHB', 'OFF', NULL, 'OHB', 1, '2016-05-13 15:50:20', NULL);
INSERT INTO CATEGORY VALUES (5, 'LUB', 'LUB', NULL, 'LUB', 1, '2016-05-13 15:50:20.002', NULL);
INSERT INTO CATEGORY VALUES (52, 'TRA', 'TIE', 32, 'CVB.HA.TRA', 1, '2016-05-13 15:51:32.605', NULL);
INSERT INTO CATEGORY VALUES (68, 'PF', 'PER', 42, 'LVB.LA.PF', 1, '2016-05-13 15:51:33.117', NULL);
INSERT INTO CATEGORY VALUES (73, 'CE', 'CAR', 32, 'CVB.HA.CE', 1, '2016-05-13 15:51:33.733', NULL);
INSERT INTO CATEGORY VALUES (74, 'KP', 'KP', 32, 'CVB.HA.KP', 1, '2016-05-13 15:51:33.958', NULL);
INSERT INTO CATEGORY VALUES (26, 'RP', 'RING', 11, 'OHB.OH.RP', 1, '2016-05-13 15:51:30.149', NULL);
INSERT INTO CATEGORY VALUES (47, 'CP', 'COMP', 9, 'CVB.CV.CP', 1, '2016-05-13 15:51:31.903', NULL);
INSERT INTO CATEGORY VALUES (48, 'TB', 'TUB', 9, 'CVB.CV.TB', 1, '2016-05-13 15:51:31.905', NULL);
INSERT INTO CATEGORY VALUES (18, 'FB', 'FILT', 11, 'OHB.OH.FB', 1, '2016-05-13 15:51:30.002', NULL);
INSERT INTO REFERENCE_VALUES (ID, REFERENCE_OBJ_NAME, REFERENCE_VALUE_CODE, DISPLAY_NAME) VALUES (1, 'CATEGORY', 6, INDU CHANGED);
INSERT INTO REFERENCE_VALUES (ID, REFERENCE_OBJ_NAME, REFERENCE_VALUE_CODE) VALUES (2, 'CATEGORY', 7);
The WHERE condition filters out rows where R.REFERENCE_OBJ_NAME is null, which is all rows for which there are no matching REFERENCE_VALUE.
Maybe what you are trying to do is only join REFERENCE_VALUES where REFERENCE_OBJ_NAME is 'CATEGORY'. If so you could do it like this:
SELECT C.ID, C.CODE, COALESCE(R.DISPLAY_NAME, C.NAME) as NAME, C.PARENT_ID, C.PATH, C.COMP_ID, C.CREATED_ON, C.MODIFIED_ON
FROM CATEGORY C
LEFT JOIN REFERENCE_VALUES R
ON R.REFERENCE_OBJ_NAME = 'CATEGORY' AND C.ID = R.REFERENCE_VALUE_CODE

SQL Query with Multiple Possible Tables and an Exclusion Table

I have multiple possible data tables (created to support parallel processing) to search against to find a match, and another table containing entries to exclude. The tables are defined as follows, along with some example data:
CREATE TABLE HOUSEHOLD_1
(
PERSON_ID NUMBER NOT NULL ENABLE,
HOUSEHOLD_HEAD_ID NUMBER NOT NULL ENABLE,
RELATIONSHIP CHAR (1) NOT NULL ENABLE,
CONSTRAINT HOUSEHOLD_1_PK PRIMARY KEY (PERSON_ID, HOUSEHOLD_HEAD_ID, RELATIONSHIP) ENABLE
);
INSERT INTO HOUSEHOLD_1 VALUES (1, 1, 'H');
INSERT INTO HOUSEHOLD_1 VALUES (2, 1, 'S');
INSERT INTO HOUSEHOLD_1 VALUES (3, 1, 'D');
CREATE TABLE HOUSEHOLD_2
(
PERSON_ID NUMBER NOT NULL ENABLE,
HOUSEHOLD_HEAD_ID NUMBER NOT NULL ENABLE,
RELATIONSHIP CHAR (1) NOT NULL ENABLE,
CONSTRAINT HOUSEHOLD_2_PK PRIMARY KEY (PERSON_ID, HOUSEHOLD_HEAD_ID, RELATIONSHIP) ENABLE
);
INSERT INTO HOUSEHOLD_2 VALUES (4, 4, 'H');
INSERT INTO HOUSEHOLD_2 VALUES (5, 4, 'S');
INSERT INTO HOUSEHOLD_2 VALUES (6, 4, 'D');
CREATE TABLE HOUSEHOLD_3
(
PERSON_ID NUMBER NOT NULL ENABLE,
HOUSEHOLD_HEAD_ID NUMBER NOT NULL ENABLE,
RELATIONSHIP CHAR (1) NOT NULL ENABLE,
CONSTRAINT HOUSEHOLD_3_PK PRIMARY KEY (PERSON_ID, HOUSEHOLD_HEAD_ID, RELATIONSHIP) ENABLE
);
INSERT INTO HOUSEHOLD_3 VALUES (7, 7, 'H');
INSERT INTO HOUSEHOLD_3 VALUES (8, 7, 'S');
INSERT INTO HOUSEHOLD_3 VALUES (9, 7, 'D');
CREATE TABLE HOUSEHOLD_DELETIONS
(
PERSON_ID NUMBER NOT NULL ENABLE,
HOUSEHOLD_HEAD_ID NUMBER NOT NULL ENABLE,
RELATIONSHIP CHAR (1) NOT NULL ENABLE,
CONSTRAINT HOUSEHOLD_DELETIONS_PK PRIMARY KEY (PERSON_ID, HOUSEHOLD_HEAD_ID, RELATIONSHIP) ENABLE
);
INSERT INTO HOUSEHOLD_DELETIONS VALUES (9, 7, 'D');
CREATE TABLE CLOSED_ACCOUNTS
(
PERSON_ID NUMBER NOT NULL ENABLE,
CONSTRAINT CLOSED_ACCOUNTS_PK PRIMARY KEY (PERSON_ID) ENABLE
);
INSERT INTO CLOSED_ACCOUNTS VALUES (3);
INSERT INTO CLOSED_ACCOUNTS VALUES (6);
INSERT INTO CLOSED_ACCOUNTS VALUES (9);
INSERT INTO CLOSED_ACCOUNTS VALUES (10);
I need to find the PERSON_ID values in CLOSED_ACCOUNTS that have a matching PERSON_ID in either HOUSEHOLD_1, HOUSEHOLD_2, or HOUSEHOLD_3, but do not have a PERSON_ID in HOUSEHOLD_DELETIONS. With the data above, I should only find PERSON_ID values 3 and 6. I've tried the SQL tricks I know, but I have not been successful, so any assistance would be appreciated. Thanks in advance.
SET operations should work to you:
SQL> select person_id from CLOSED_ACCOUNTS
2 intersect (
3 select person_id from HOUSEHOLD_1
4 union
5 select person_id from HOUSEHOLD_2
6 union
7 select person_id from HOUSEHOLD_3
8 )
9 minus
10 select person_id from HOUSEHOLD_DELETIONS
11 /
PERSON_ID
----------
3
6