Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- DROP TABLE purchases;
- DROP TABLE inventory;
- DROP TABLE employees;
- DROP TABLE stores;
- DROP TABLE addresses;
- DROP TABLE customers;
- DROP TABLE employee_types;
- DROP TABLE address_types;
- */
- -- ***
- CREATE TABLE address_types (
- add_type_id INTEGER,
- add_literal VARCHAR(128) UNIQUE NOT NULL,
- PRIMARY KEY (add_type_id)
- );
- INSERT INTO address_types (add_type_id, add_literal)
- VALUES (4321, 'Store')
- ,(4444, 'Employee')
- ,(4567, 'Customer')
- ;
- -- ***
- CREATE TABLE employee_types (
- emp_id INTEGER,
- role_literal VARCHAR(128) NOT NULL,
- PRIMARY KEY(emp_id)
- );
- INSERT INTO employee_types (emp_id, role_literal)
- VALUES (0001, 'Manager')
- ,(0002, 'Sales')
- ,(0003, 'Support')
- ;
- -- ***
- CREATE TABLE customers (
- cust_id INTEGER GENERATED ALWAYS AS IDENTITY (START WITH 10000, INCREMENT BY 1),
- cust_firstname VARCHAR(128) NOT NULL,
- cust_lastname VARCHAR(128) NOT NULL,
- PRIMARY KEY(cust_id)
- );
- INSERT INTO customers (cust_firstname, cust_lastname)
- VALUES ('Johnny', 'Morris')
- ,('Jack', 'Johnson')
- ,('Billy', 'Williams')
- ,('BB', 'King')
- ,('Robert', 'Plant')
- ,('Jimmy', 'Falon')
- ,('Wycleff', 'Jean')
- ,('Tim', 'Robbins')
- ,('Bruce', 'Willis')
- ,('Mel', 'Gibson')
- ;
- -- ^^
- CREATE TABLE addresses (
- add_id INTEGER,
- add_type_id INTEGER NOT NULL,
- city VARCHAR(128) NOT NULL,
- state VARCHAR(128) NOT NULL,
- zip VARCHAR(11) NOT NULL,
- residential CHAR(1) NOT NULL,
- CONSTRAINT fk_add_type_id FOREIGN KEY (add_type_id) REFERENCES address_types(add_type_id),
- PRIMARY KEY(add_id)
- );
- INSERT INTO addresses (add_id, add_type_id, city, state, zip, residential)
- VALUES (1111, 4321, 'Springfield', 'Missouri', '65804', '0')
- ,(1212, 4444, 'Springfield', 'Missouri', '65804', '1')
- ,(1313, 4444, 'Springfield', 'Missouri', '65804', '1')
- ,(1414, 4444, 'Springfield', 'Missouri', '65804', '1')
- ,(1515, 4444, 'Springfield', 'Illinois', '47895', '1')
- ,(1616, 4444, 'Springfield', 'Missouri', '65804', '1')
- ,(1717, 4444, 'Springfield', 'Missouri', '65804', '1')
- ,(1818, 4444, 'Springfield', 'Illinois', '47895', '1')
- ,(1919, 4444, 'Springfield', 'Missouri', '65804', '1')
- ,(2121, 4444, 'Springfield', 'Illinois', '47895', '1')
- ,(2222, 4444, 'Springfield', 'Missouri', '65804', '1')
- ,(2323, 4444, 'Springfield', 'Missouri', '65804', '1')
- ,(2424, 4444, 'Springfield', 'Illinois', '47895', '1')
- ,(2525, 4444, 'Springfield', 'Missouri', '65804', '1')
- ,(2626, 4444, 'Springfield', 'Missouri', '65804', '1')
- ,(2727, 4444, 'Springfield', 'Illinois', '47895', '1')
- ,(2828, 4444, 'Springfield', 'Missouri', '65804', '1')
- ,(2929, 4444, 'Springfield', 'Massachusettes', '12345', '1')
- ,(3030, 4444, 'Springfield', 'Missouri', '65804', '1')
- ,(3131, 4444, 'Springfield', 'Missouri', '65804', '1')
- ;
- -- ^^
- CREATE TABLE stores (
- store_id INTEGER,
- store_name VARCHAR(128) NOT NULL,
- store_add_id INTEGER NOT NULL,
- tax_percent DECIMAL(3, 2) NOT NULL,
- CONSTRAINT fk_store_add_id FOREIGN KEY (store_add_id) REFERENCES addresses(add_id),
- CONSTRAINT ck_store_name CHECK (LENGTH(store_name) > 3),
- PRIMARY KEY(store_id)
- );
- CREATE INDEX store_name_idx
- ON stores(store_name);
- INSERT INTO stores (store_id, store_name, store_add_id, tax_percent)
- VALUES (622, 'Bike Store', 1111, 0.12);
- -- vv
- CREATE TABLE employees (
- emp_id INTEGER GENERATED ALWAYS AS IDENTITY (START WITH 1000, INCREMENT BY 1),
- emp_role_id INTEGER NOT NULL,
- emp_store_id INTEGER NOT NULL,
- emp_firstname VARCHAR(128) NOT NULL,
- emp_lastname VARCHAR(128) NOT NULL,
- emp_phone VARCHAR(128) NOT NULL,
- emp_email VARCHAR(128) UNIQUE NOT NULL,
- emp_address INTEGER NOT NULL,
- CONSTRAINT fk_emp_role_id FOREIGN KEY (emp_role_id) REFERENCES employee_types(emp_id),
- CONSTRAINT fk_emp_store_id FOREIGN KEY (emp_store_id) REFERENCES stores(store_id),
- CONSTRAINT fk_emp_address FOREIGN KEY (emp_address) REFERENCES addresses(add_id),
- PRIMARY KEY (emp_id)
- );
- INSERT INTO employees (emp_role_id, emp_store_id, emp_firstname, emp_lastname, emp_phone, emp_email, emp_address)
- VALUES (0001, 622, 'Jody', 'Pretz', '234-1234', 'pretz@gmail.com', 1212)
- ,(0002, 622, 'Bob', 'Smith', '563-3321', 'smith@gmail.com', 1313)
- ,(0002, 622, 'Joe', 'Ledger', '123-5134', 'wow@gmail.com', 1414)
- ,(0002, 622, 'July', 'Cobb', '753-7753', 'yikes@gmail.com', 1515)
- ,(0002, 622, 'Sindey', 'Rock', '714-7765', 'aaah@gmail.com', 1616)
- ,(0002, 622, 'Mike', 'Pocket', '582-2334', 'rough@gmail.com', 1717)
- ,(0002, 622, 'Bobby', 'Walt', '678-5689', 'dewd@gmail.com', 1818)
- ,(0002, 622, 'Allard', 'Free', '333-2221', 'woah@gmail.com', 1919)
- ,(0002, 622, 'Fred', 'Walder', '124-5534', 'aased@gmail.com', 2121)
- ,(0002, 622, 'Mark', 'Mahan', '675-1321', 'mailacc@gmail.com', 2222)
- ,(0002, 622, 'Mitch', 'Traush', '367-5356', 'yeet@gmail.com', 2323)
- ,(0002, 622, 'Clyde', 'Maky', '332-4621', 'guyr@gmail.com', 2424)
- ,(0002, 622, 'Klyde', 'Loore', '123-5555', 'leet@gmail.com', 2525)
- ,(0002, 622, 'Rich', 'Quetz', '770-1111', 'ajax@gmail.com', 2626)
- ,(0002, 622, 'Ron', 'Well', '923-3351', 'aasey@gmail.com', 2727)
- ,(0002, 622, 'Tim', 'Pool', '122-6678', 'Looper@gmail.com', 2828)
- ,(0002, 622, 'Shawnda', 'Zed', '111-1112', 'profane@gmail.com', 2929)
- ,(0003, 622, 'Precious', 'Yeary', '222-2238', 'rocky@gmail.com', 3030)
- ,(0003, 622, 'Trudy', 'Green', '333-1987', 'loik@gmail.com', 3131)
- ;
- -- vv
- CREATE TABLE inventory (
- inv_vin CHAR(17),
- inv_store_id INTEGER NOT NULL,
- inv_year CHAR(4) NOT NULL,
- inv_make VARCHAR(128) NOT NULL,
- inv_model VARCHAR(128) NOT NULL,
- inv_color VARCHAR(128) NOT NULL,
- inv_acquired_date DATE NOT NULL,
- inv_mileage INTEGER NOT NULL,
- CONSTRAINT fk_inv_store_id FOREIGN KEY (inv_store_id) REFERENCES stores(store_id),
- PRIMARY KEY (inv_vin)
- );
- INSERT INTO inventory (inv_vin, inv_store_id, inv_year, inv_make, inv_model, inv_color, inv_acquired_date, inv_mileage)
- VALUES ('1ZVBP8AM0D5265429', 622, '2018', 'Ford', 'Mustang GT', 'Black', '2017-01-02', 1234)
- ,('4S3BK6354S6355265', 622, '2008', 'Inifiniti', 'G35', 'Crimson Red', '2017-02-03', 123000)
- ,('1ZVFT80N475211367', 622, '2013', 'Cheverolet', 'Camaro', 'Black and White', '2017-03-04', 45020)
- ,('JS3TX92V364102160', 622, '1995', 'Acura', 'NSX', 'White', '2017-04-05', 700)
- ,('YV1LZ5647W2469314', 622, '2008', 'Pontiac', 'G8 GXP', 'Maroon', '2017-05-06', 83400)
- ,('1J4BA3H10AL171412', 622, '2019', 'Cheverolet', 'Corvette ZR1', 'Midnight Blue', '2017-06-07', 25)
- ,('1G4GJ11Y9HP422546', 622, '2018', 'Dodge', 'Challenger Demon', 'Red', '2017-07-08', 123)
- ,('JH4KA7532NC036794', 622, '2012', 'Maserati', 'Gran Turismo', 'Blue', '2017-08-09', 24000)
- ,('JG1MR215XJK752025', 622, '1965', 'Ferrari', '250 GTO', 'Rosso Corsa', '2017-09-10', 120)
- ,('1N6DD21S4WC303918', 622, '1999', 'McLaren', 'F1', 'Yellow', '2017-10-11', 1230)
- ,('JN8AZ2NE5C9016953', 622, '2005', 'Porsche', 'Carrera GT', 'Silver', '2017-11-12', 5600)
- ,('4F4ZR17V7XTM07477', 622, '2012', 'Ferrari', '458 Italia', 'Charcoal Black', '2017-12-13', 12340)
- ,('JH4KA7650PC002520', 622, '2017', 'Lamborghini', 'Aventador S', 'Lime Green', '2018-01-01', 653)
- ,('WP0AA0941HN450519', 622, '2015', 'Porsche', '911 Turbo S', 'Silver', '2018-02-02', 123)
- ,('1HGEM21991L005461', 622, '2016', 'Mercedes', 'AMG GT-R', 'Crazy Green', '2018-03-03', 9034)
- ,('4S3BD4353T7209207', 622, '2014', 'Dodge', 'Viper ACR', 'Black and White', '2018-04-04', 9880)
- ,('JH4DC4433RS801008', 622, '2019', 'Ford', 'Mustang GT350', 'Blue', '2018-05-05', 8830)
- ,('WP0CD2A91BS773285', 622, '2008', 'Lotus', 'Elise', 'Orange', '2018-06-06', 7188)
- ,('5HD1CT3157K417590', 622, '2018', 'BMW', 'M4 GTS', 'Red', '2018-01-02', 9777)
- ,('YS3DF78K527013330', 622, '2013', 'Jaguar', 'F-Type SVR', 'Wild Blue', '2018-01-12', 6554)
- ;
- -- -
- CREATE TABLE purchases (
- purch_id INTEGER,
- purch_store_id INTEGER NOT NULL,
- purch_salesperson INTEGER NOT NULL,
- purch_vin CHAR(17) NOT NULL,
- purch_cust_id INTEGER NOT NULL,
- purch_price DECIMAL(10,2) NOT NULL,
- purch_tax DECIMAL(10,2),
- purch_title DECIMAL(10,2),
- purch_license DECIMAL(10,2),
- purch_sold_mileage INTEGER NOT NULL,
- purch_date DATE NOT NULL DEFAULT CURRENT_DATE,
- CONSTRAINT fk_purch_store_id FOREIGN KEY (purch_store_id) REFERENCES stores(store_id),
- CONSTRAINT fk_purch_salesperson FOREIGN KEY (purch_salesperson) REFERENCES employees(emp_id),
- CONSTRAINT fk_purch_vin FOREIGN KEY (purch_vin) REFERENCES inventory(inv_vin),
- CONSTRAINT fk_purch_cust_id FOREIGN KEY (purch_cust_id) REFERENCES customers(cust_id),
- PRIMARY KEY(purch_id)
- );
- CREATE INDEX purch_date_idx
- ON purchases(purch_date);
- INSERT INTO purchases (purch_id, purch_store_id, purch_salesperson, purch_vin, purch_cust_id, purch_price, purch_tax, purch_title, purch_license, purch_sold_mileage, purch_date)
- VALUES (1443, 622, 1007, '1ZVBP8AM0D5265429', 10001, 45000, 23.12, 134.23, 134.23, 1236, '2017-01-01')
- ,(1798, 622, 1003, '4S3BK6354S6355265', 10009, 12000, 34.12, 34.23, 34.23, 123002, '2017-02-01')
- ,(1427, 622, 1004, '1ZVFT80N475211367', 10009, 23000, 73.12, 445.23, 445.23, 45025, '2017-03-01')
- ,(1518, 622, 1003, 'JS3TX92V364102160', 10007, 550000, 5432.12, 2324.23, 2324.23, 7023, '2017-04-01')
- ,(1536, 622, 1006, 'YV1LZ5647W2469314', 10006, 14000, 234.12, 2234.23, 2234.23, 83406, '2017-05-01')
- ,(1446, 622, 1007, '1J4BA3H10AL171412', 10008, 125000, 343.12, 6234.23, 6234.23, 28, '2017-06-01')
- ,(1938, 622, 1008, '1G4GJ11Y9HP422546', 10005, 90000, 654.12, 4234.23, 4234.23, 129, '2017-07-01')
- ,(1710, 622, 1003, 'JH4KA7532NC036794', 10009, 150000, 124.12, 3234.23, 3234.23, 24002, '2017-08-01')
- ,(1685, 622, 1010, 'JG1MR215XJK752025', 10009, 65000000, 42344.12, 91234.23, 91234.23, 121, '2017-09-01')
- ,(1943, 622, 1007, '1N6DD21S4WC303918', 10008, 12000000, 2345.12, 71234.23, 71234.23, 1232, '2017-10-01')
- ,(1911, 622, 1012, 'JN8AZ2NE5C9016953', 10007, 1500000, 1254.12, 21234.23, 21234.23, 5604, '2017-11-01')
- ,(1092, 622, 1013, '4F4ZR17V7XTM07477', 10006, 360000, 43.12, 1634.23, 1634.23, 12343, '2017-12-01')
- ,(1689, 622, 1007, 'JH4KA7650PC002520', 10005, 430000, 654.12, 1134.23, 1134.23, 656, '2018-01-01')
- ,(1089, 622, 1015, 'WP0AA0941HN450519', 10004, 185000, 123.12, 4234.23, 4234.23, 127, '2018-03-01')
- ,(1842, 622, 1007, '1HGEM21991L005461', 10003, 160000, 32.12, 2234.23, 2234.23, 9038, '2018-05-01')
- ,(1447, 622, 1007, '4S3BD4353T7209207', 10002, 95000, 366.12, 1234.23, 1234.23, 9889, '2018-07-01')
- ;
- -- #6: All cars acquired in 2017
- SELECT i.inv_vin AS vin,
- i.inv_make AS make,
- i.inv_model AS model,
- i.inv_year AS model_year,
- i.inv_color AS color,
- i.inv_mileage AS mileage,
- i.inv_acquired_date AS acquired,
- i.inv_store_id AS store_id,
- s.store_name,
- s.tax_percent AS store_tax,
- s.store_add_id AS address_id,
- a.city,
- a.state,
- a.zip
- FROM inventory i
- JOIN stores s
- ON i.inv_store_id = s.store_id
- JOIN addresses a
- ON s.store_add_id = a.add_id
- JOIN address_types a_t
- ON a.add_type_id = a_t.add_type_id
- WHERE i.inv_acquired_date BETWEEN '2017-01-01' AND '2017-12-31'
- ;
- -- #7: All cars sold in 2018, 1st quarter
- SELECT *
- FROM purchases p
- JOIN inventory i
- ON p.purch_vin = i.inv_vin
- JOIN stores s
- ON s.store_id = p.purch_store_id
- JOIN customers c
- ON c.cust_id = p.purch_cust_id
- WHERE p.purch_date BETWEEN '2018-01-01' AND '2018-03-31'
- ;
- -- #8: All cars that have not been sold
- SELECT i.inv_vin AS vin,
- i.inv_make AS make,
- i.inv_model AS model,
- i.inv_year AS modelyear,
- i.inv_color AS color,
- i.inv_mileage AS mileage,
- i.inv_acquired_date AS acquired,
- i.inv_store_id AS store_id,
- s.store_name,
- s.tax_percent AS store_tax,
- s.store_add_id AS address_id,
- a.city,
- a.state,
- a.zip
- FROM inventory i
- JOIN stores s
- ON i.inv_store_id = s.store_id
- JOIN addresses a
- ON s.store_add_id = a.add_id
- JOIN address_types a_t
- ON a.add_type_id = a_t.add_type_id
- LEFT JOIN purchases p
- ON p.purch_vin = i.inv_vin
- WHERE p.purch_vin IS NULL
- ;
- -- #9: Sales Report
- SELECT e.emp_id AS employee_id,
- e_t.role_literal AS employee_role,
- e.emp_firstname AS employee_firstname,
- e.emp_lastname AS employee_lastname,
- e.emp_phone AS employee_phone,
- e.emp_email AS employee_email,
- e.emp_address AS employee_address_id,
- e_a.city AS employee_city,
- e_a.state AS employee_state,
- e_a.zip AS employee_zip,
- e.emp_store_id AS store_id,
- s.store_name AS store_name,
- s.store_add_id AS store_address_id,
- s_a.city AS store_city,
- s_a.state AS store_state,
- s_a.zip AS store_zip,
- p.purch_id AS sale_id,
- i.inv_acquired_date AS acquired,
- p.purch_date AS sale_date,
- p.purch_vin AS sold_vin,
- i.inv_year AS model_year,
- i.inv_make AS make,
- i.inv_model AS model,
- i.inv_color AS color,
- p.purch_sold_mileage AS mileage,
- p.purch_price AS sold_for,
- p.purch_tax AS sale_tax,
- p.purch_title AS sale_title,
- p.purch_license AS sale_license,
- c.cust_id AS customer_id,
- c.cust_firstname AS customer_firstname,
- c.cust_lastname AS customer_lastname
- FROM employees e
- JOIN employee_types e_t
- ON e.emp_role_id = e_t.emp_id
- JOIN stores s
- ON e.emp_store_id = s.store_id
- JOIN addresses s_a
- ON s.store_add_id = s_a.add_id
- JOIN addresses e_a
- ON e.emp_address = e_a.add_id
- JOIN address_types sa_t
- ON s_a.add_type_id = sa_t.add_type_id
- JOIN address_types ea_t
- ON e_a.add_type_id = ea_t.add_type_id
- JOIN purchases p
- ON p.purch_salesperson = e.emp_id
- AND p.purch_store_id = s.store_id
- JOIN inventory i
- ON p.purch_vin = i.inv_vin
- AND s.store_id = i.inv_store_id
- JOIN customers c
- ON p.purch_cust_id = c.cust_id
- ;
- -- #10: Salespersons who have not sold a car
- SELECT e.*
- FROM employees e LEFT OUTER JOIN purchases p
- ON purch_salesperson = emp_id WHERE purch_salesperson IS NULL AND emp_role_id = 0002;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement