
CREATE TABLE "Customer_Event_Notes" (
	"Customer_Event_Note_ID" INTEGER NOT NULL, 
	"Customer_Event_ID" INTEGER NOT NULL, 
	service_type_code CHAR(15) NOT NULL, 
	resident_id INTEGER NOT NULL, 
	property_id INTEGER NOT NULL, 
	date_moved_in DATETIME NOT NULL, 
	PRIMARY KEY ("Customer_Event_Note_ID"), 
	FOREIGN KEY("Customer_Event_ID") REFERENCES "Customer_Events" ("Customer_Event_ID")
)


CREATE TABLE "Customer_Events" (
	"Customer_Event_ID" INTEGER NOT NULL, 
	customer_id INTEGER, 
	date_moved_in DATETIME, 
	property_id INTEGER, 
	resident_id INTEGER, 
	thing_id INTEGER NOT NULL, 
	PRIMARY KEY ("Customer_Event_ID"), 
	FOREIGN KEY(thing_id) REFERENCES "Things" (thing_id), 
	FOREIGN KEY(customer_id) REFERENCES "Customers" (customer_id), 
	FOREIGN KEY(resident_id, property_id, date_moved_in) REFERENCES "Residents" (resident_id, property_id, date_moved_in)
)


CREATE TABLE "Customers" (
	customer_id INTEGER NOT NULL, 
	customer_details VARCHAR(255), 
	PRIMARY KEY (customer_id)
)


CREATE TABLE "Organizations" (
	organization_id INTEGER NOT NULL, 
	parent_organization_id INTEGER, 
	organization_details VARCHAR(255), 
	PRIMARY KEY (organization_id)
)


CREATE TABLE "Properties" (
	property_id INTEGER NOT NULL, 
	property_type_code CHAR(15) NOT NULL, 
	property_address VARCHAR(255), 
	other_details VARCHAR(255), 
	PRIMARY KEY (property_id)
)


CREATE TABLE "Residents" (
	resident_id INTEGER NOT NULL, 
	property_id INTEGER NOT NULL, 
	date_moved_in DATETIME NOT NULL, 
	date_moved_out DATETIME NOT NULL, 
	other_details VARCHAR(255), 
	PRIMARY KEY (resident_id, property_id, date_moved_in), 
	FOREIGN KEY(property_id) REFERENCES "Properties" (property_id)
)


CREATE TABLE "Residents_Services" (
	resident_id INTEGER NOT NULL, 
	service_id INTEGER NOT NULL, 
	date_moved_in DATETIME, 
	property_id INTEGER, 
	date_requested DATETIME, 
	date_provided DATETIME, 
	other_details VARCHAR(255), 
	PRIMARY KEY (resident_id, service_id), 
	FOREIGN KEY(service_id) REFERENCES "Services" (service_id), 
	FOREIGN KEY(resident_id, property_id, date_moved_in) REFERENCES "Residents" (resident_id, property_id, date_moved_in)
)


CREATE TABLE "Services" (
	service_id INTEGER NOT NULL, 
	organization_id INTEGER NOT NULL, 
	service_type_code CHAR(15) NOT NULL, 
	service_details VARCHAR(255), 
	PRIMARY KEY (service_id), 
	FOREIGN KEY(organization_id) REFERENCES "Organizations" (organization_id)
)


CREATE TABLE "Things" (
	thing_id INTEGER NOT NULL, 
	organization_id INTEGER NOT NULL, 
	"Type_of_Thing_Code" CHAR(15) NOT NULL, 
	service_type_code CHAR(10) NOT NULL, 
	service_details VARCHAR(255), 
	PRIMARY KEY (thing_id), 
	FOREIGN KEY(organization_id) REFERENCES "Organizations" (organization_id)
)


CREATE TABLE "Timed_Locations_of_Things" (
	thing_id INTEGER NOT NULL, 
	"Date_and_Time" DATETIME NOT NULL, 
	"Location_Code" CHAR(15) NOT NULL, 
	PRIMARY KEY (thing_id, "Date_and_Time", "Location_Code"), 
	FOREIGN KEY(thing_id) REFERENCES "Things" (thing_id)
)


CREATE TABLE "Timed_Status_of_Things" (
	thing_id INTEGER NOT NULL, 
	"Date_and_Date" DATETIME NOT NULL, 
	"Status_of_Thing_Code" CHAR(15) NOT NULL, 
	PRIMARY KEY (thing_id, "Date_and_Date", "Status_of_Thing_Code"), 
	FOREIGN KEY(thing_id) REFERENCES "Things" (thing_id)
)

CREATE TABLE "Customer_Event_Notes" (
	"Customer_Event_Note_ID" INTEGER NOT NULL, 
	"Customer_Event_ID" INTEGER NOT NULL, 
	service_type_code CHAR(15) NOT NULL, 
	resident_id INTEGER NOT NULL, 
	property_id INTEGER NOT NULL, 
	date_moved_in DATETIME NOT NULL, 
	PRIMARY KEY ("Customer_Event_Note_ID"), 
	FOREIGN KEY("Customer_Event_ID") REFERENCES "Customer_Events" ("Customer_Event_ID")
)

/*
2 rows from Customer_Event_Notes table:
Customer_Event_Note_ID	Customer_Event_ID	service_type_code	resident_id	property_id	date_moved_in
18	70	Cleanning	58	107	2016-08-04 01:06:05
21	428	Check	87	445	2016-12-11 01:05:14
*/


CREATE TABLE "Customer_Events" (
	"Customer_Event_ID" INTEGER NOT NULL, 
	customer_id INTEGER, 
	date_moved_in DATETIME, 
	property_id INTEGER, 
	resident_id INTEGER, 
	thing_id INTEGER NOT NULL, 
	PRIMARY KEY ("Customer_Event_ID"), 
	FOREIGN KEY(thing_id) REFERENCES "Things" (thing_id), 
	FOREIGN KEY(customer_id) REFERENCES "Customers" (customer_id), 
	FOREIGN KEY(resident_id, property_id, date_moved_in) REFERENCES "Residents" (resident_id, property_id, date_moved_in)
)

/*
2 rows from Customer_Events table:
Customer_Event_ID	customer_id	date_moved_in	property_id	resident_id	thing_id
70	19	2015-03-27 12:00:00	605	10	1
160	5	2015-03-27 12:00:00	879	23	80
*/


CREATE TABLE "Customers" (
	customer_id INTEGER NOT NULL, 
	customer_details VARCHAR(255), 
	PRIMARY KEY (customer_id)
)

/*
2 rows from Customers table:
customer_id	customer_details
4	Mr. Raul Prosacco
5	Esteban Senger
*/


CREATE TABLE "Organizations" (
	organization_id INTEGER NOT NULL, 
	parent_organization_id INTEGER, 
	organization_details VARCHAR(255), 
	PRIMARY KEY (organization_id)
)

/*
2 rows from Organizations table:
organization_id	parent_organization_id	organization_details
7	7	Reinger, Hudson and Nolan Group
8	7	Denesik and Sons Party
*/


CREATE TABLE "Properties" (
	property_id INTEGER NOT NULL, 
	property_type_code CHAR(15) NOT NULL, 
	property_address VARCHAR(255), 
	other_details VARCHAR(255), 
	PRIMARY KEY (property_id)
)

/*
2 rows from Properties table:
property_id	property_type_code	property_address	other_details
24	apartment	85456 Annie Lodge Suite 449	USA
107	house	2580 Yundt Plains	USA
*/


CREATE TABLE "Residents" (
	resident_id INTEGER NOT NULL, 
	property_id INTEGER NOT NULL, 
	date_moved_in DATETIME NOT NULL, 
	date_moved_out DATETIME NOT NULL, 
	other_details VARCHAR(255), 
	PRIMARY KEY (resident_id, property_id, date_moved_in), 
	FOREIGN KEY(property_id) REFERENCES "Properties" (property_id)
)

/*
2 rows from Residents table:
resident_id	property_id	date_moved_in	date_moved_out	other_details
6	954	2017-06-17 15:43:33	2017-12-18 03:46:04	Anderson Batz
10	605	2015-03-27 12:00:00	2018-03-17 07:48:09	Miss Naomie Osinski
*/


CREATE TABLE "Residents_Services" (
	resident_id INTEGER NOT NULL, 
	service_id INTEGER NOT NULL, 
	date_moved_in DATETIME, 
	property_id INTEGER, 
	date_requested DATETIME, 
	date_provided DATETIME, 
	other_details VARCHAR(255), 
	PRIMARY KEY (resident_id, service_id), 
	FOREIGN KEY(service_id) REFERENCES "Services" (service_id), 
	FOREIGN KEY(resident_id, property_id, date_moved_in) REFERENCES "Residents" (resident_id, property_id, date_moved_in)
)

/*
2 rows from Residents_Services table:
resident_id	service_id	date_moved_in	property_id	date_requested	date_provided	other_details
6	49	2017-06-17 15:43:33	954	2016-07-25 01:32:23	2018-02-26 00:27:11	Satisfied
23	41	2015-03-27 12:00:00	879	2016-10-10 21:42:21	2017-08-21 06:23:06	Unsatisfied
*/


CREATE TABLE "Services" (
	service_id INTEGER NOT NULL, 
	organization_id INTEGER NOT NULL, 
	service_type_code CHAR(15) NOT NULL, 
	service_details VARCHAR(255), 
	PRIMARY KEY (service_id), 
	FOREIGN KEY(organization_id) REFERENCES "Organizations" (organization_id)
)

/*
2 rows from Services table:
service_id	organization_id	service_type_code	service_details
10	10	Cleanning	Satisfied
11	7	Check	Satisfied
*/


CREATE TABLE "Things" (
	thing_id INTEGER NOT NULL, 
	organization_id INTEGER NOT NULL, 
	"Type_of_Thing_Code" CHAR(15) NOT NULL, 
	service_type_code CHAR(10) NOT NULL, 
	service_details VARCHAR(255), 
	PRIMARY KEY (thing_id), 
	FOREIGN KEY(organization_id) REFERENCES "Organizations" (organization_id)
)

/*
2 rows from Things table:
thing_id	organization_id	Type_of_Thing_Code	service_type_code	service_details
1	8	Electronics	Check	Unsatisfied
2	8	Electronics	Cleanning	Satisfied
*/


CREATE TABLE "Timed_Locations_of_Things" (
	thing_id INTEGER NOT NULL, 
	"Date_and_Time" DATETIME NOT NULL, 
	"Location_Code" CHAR(15) NOT NULL, 
	PRIMARY KEY (thing_id, "Date_and_Time", "Location_Code"), 
	FOREIGN KEY(thing_id) REFERENCES "Things" (thing_id)
)

/*
2 rows from Timed_Locations_of_Things table:
thing_id	Date_and_Time	Location_Code
2	2016-10-12 12:03:07	064 Charles Mou
10	2016-12-29 00:39:19	288 Lang Ferry
*/


CREATE TABLE "Timed_Status_of_Things" (
	thing_id INTEGER NOT NULL, 
	"Date_and_Date" DATETIME NOT NULL, 
	"Status_of_Thing_Code" CHAR(15) NOT NULL, 
	PRIMARY KEY (thing_id, "Date_and_Date", "Status_of_Thing_Code"), 
	FOREIGN KEY(thing_id) REFERENCES "Things" (thing_id)
)

/*
2 rows from Timed_Status_of_Things table:
thing_id	Date_and_Date	Status_of_Thing_Code
2	2016-05-27 23:33:34	Open
2	2016-07-17 08:35:27	Close
*/
What are the id and details of the customers who have at least 3 events?
SELECT T1.customer_id ,  T1.customer_details FROM Customers AS T1 JOIN Customer_Events AS T2 ON T1.customer_id  =  T2.customer_id GROUP BY T1.customer_id HAVING count(*)  >=  3
[{'customer_id': 19, 'customer_details': 'Sabryna Moore PhD'}]