├── README.md
├── intro_to_postgres_case_study_summary.pdf
├── intro_to_postgres_cheat_sheet.txt
├── intro_to_postgres_setup.sql
├── setup_instructions.pdf
└── sponsored_products.csv
/README.md:
--------------------------------------------------------------------------------
1 | # Introduction-to-PostgreSQL
2 | Introduction to PostgreSQL materials
3 |
4 | Use the setup_instructions.pdf after downloading postgres 10 and PgAdmin 4 through the enterprise DB installer (linked to on main course page)
5 | During this you will be asked to copy the intro_to_postgres_setup.sql file into PgAdmin 4 query tool.
6 |
7 | The intro to postgres case study summary and cheet sheet will be used throughout the class and should follow along in the same order.
8 |
9 | The sponsored_products.csv file will be utilized at the end of day 2 of the class and should be downloaded before class on day 2.
10 |
--------------------------------------------------------------------------------
/intro_to_postgres_case_study_summary.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jess88/Introduction-to-PostgreSQL/a48e9e90da670d9c4fa6bf11f66a4985c9e73bdb/intro_to_postgres_case_study_summary.pdf
--------------------------------------------------------------------------------
/intro_to_postgres_cheat_sheet.txt:
--------------------------------------------------------------------------------
1 |
2 | ****************************************************************************************************************************************
3 | ****************************************************************************************************************************************
4 | ************************ DAY 1 ************************
5 | ****************************************************************************************************************************************
6 | ****************************************************************************************************************************************
7 |
8 | ********************************************************************************************************
9 | ******** Comments/Notes ********
10 | ********************************************************************************************************
11 | Commenting/adding notes that aren't part of the query
12 | To comment to the end of the current line, use '--'
13 | To comment between two specific points use '/* ... */'
14 |
15 |
16 | ********************************************************************************************************
17 | ******** Data Types ********
18 | ********************************************************************************************************
19 | Types:
20 | numbers:
21 | whole: integer
22 | decimal: float
23 | Decimal with limited significant values: numeric
24 |
25 | text: text
26 | Text with maximum size set: varchar(length)
27 |
28 | true/false value: boolean
29 |
30 | date objects:
31 | calendar date: date
32 | calendar date with specific time: timestamp
33 | Calendar date with specific time and timezone: timestamp without timezone
34 |
35 | specific storage of key-value pairs: JSON
36 | set or collection of values that all share same data type: array
37 | Ex: collect of whole numbers would be an array of integers where the type for all values in the collection (array)
38 | are all integers
39 |
40 |
41 | ********************************************************************************************************
42 | ******** Denormalized vs. Normalized ********
43 | ********************************************************************************************************
44 | Denormalized - Generally storing all relevant information in each table/column fully, even if is considered redundant
45 | Normalized - Efficiently organizing data so that shared values are defined in 1 location
46 |
47 |
48 | ********************************************************************************************************
49 | ******** Selecting Data ********
50 | ********************************************************************************************************
51 | Select
52 | SELECT * FROM
; -- return all data (columns and rows) in the table
53 | Examples: SELECT * FROM users;
54 | SELECT * FROM products;
55 |
56 | Aliasing
57 | Format: SELECT AS col_alias FROM AS table_alias;
58 | rename your columns and tables so you don't have to type out the full names
59 | NOTE: columns and tables alias names must start with a letter and cannot start with a number
60 | Example: SELECT address as addr FROM users u;
61 |
62 |
63 | ********************************************************************************************************
64 | ******** Limit and Order By ********
65 | ********************************************************************************************************
66 | Limit
67 | Format: Limit # -- only return # rows
68 | Example: SELECT * FROM products limit 10;
69 |
70 | ORDER BY
71 | Format: Order by -- return results ordered by the indicated value. Must use raw column name, not alias
72 | By default order by will return ascending, use 'desc' to return descending
73 | Example:
74 | SELECT * FROM products
75 | ORDER BY price desc;
76 |
77 | By Column Number: You can refer the column number for the data you want to order on,
78 | based on the order the columns are selected
79 | Example:
80 | SELECT id, price FROM products
81 | ORDER BY 2 -- will order by price since price is the 2nd column being selected
82 | LIMIT 1;
83 |
84 | Nulls: Nulls can be specifically addressed after the rest of the order by statement
85 | Example:
86 | SELECT * FROM orders
87 | ORDER BY status NULLS FIRST
88 |
89 |
90 | ********************************************************************************************************
91 | ******** Casting values ********
92 | ********************************************************************************************************
93 | Sometimes columns 'type' doesn't match what you're evaluating, for this use CAST
94 | Format: CAST( AS )
95 | Format: ::
96 | Note: By default cast will round for you
97 | Examples:
98 | SELECT CAST(order_total as int), order_total from orders;
99 |
100 | SELECT order_total::INT, order_total from orders;
101 |
102 | SELECT CAST(shipping_total AS int) as int_total,
103 | shipping_total::INTEGER as int_v2_total
104 | FROM orders;
105 |
106 |
107 | ********************************************************************************************************
108 | ******** Unique Records & Grouping ********
109 | ********************************************************************************************************
110 | Unique records: Distinct and Group by
111 | Format - Group by: Group by -- will group identical rows into a single row. Must use raw column name, not alias
112 | Format - Distinct: Distinct -- will only select unique rows of data selected/returned, any duplicate rows are not returned.
113 | Examples:
114 | SELECT product_type_id FROM products
115 | GROUP BY product_type_id;
116 |
117 | SELECT DISTINCT product_type_id FROM products;
118 |
119 | Group By & column number reference: Refer to the columns selected by the order they are returned instead of the column itself
120 | Example:
121 | SELECT product_type_id FROM products
122 | GROUP BY 1; -- will group by product_type_id since it is the 1st column being selecte
123 |
124 |
125 | ********************************************************************************************************
126 | ******** Aggregate functions ********
127 | ********************************************************************************************************
128 | Aggregate functions
129 | Note: If you select any columns/values that are not aggregate functions,
130 | these will require a 'group by' clause to group the results and be able to perform the function results.
131 |
132 | Average: returns average of the total of the column or the calculation.
133 | Format: AVG()
134 | note: null values are not included in denominator
135 | Example: SELECT avg(order_total) FROM orders;
136 |
137 | Addition: returns the sum of the total of the column or calculation
138 | Format: SUM()
139 | non-aggregate function to get by row addition: +
140 | -- Note: if either column value is null, the result will be null
141 | Example:
142 |
143 | Count: returns the total number of rows where the column or value is not null
144 | Format: COUNT()
145 | COUNT(DISTINCT ) -- Will only count unique values of the column
146 | Example:
147 |
148 | Minimum and Maximum: returns the minimum or maximum value of the rows for that column/calculation
149 | Format: MAX(), MIN()
150 | Example
151 |
152 | Examples:
153 | Select AVG(price) as avg_price,
154 | SUM(price) as total_cost,
155 | COUNT(price) as total_products,
156 | MIN(price) as least_cost,
157 | MAX(price) as most_cost
158 | FROM products;
159 |
160 | Group by example: When you select non-aggregate columns/values - Get the results by user_id
161 | Select AVG(price) as avg_price,
162 | SUM(price) as total_cost,
163 | COUNT(price) as total_products,
164 | MIN(price) as least_cost,
165 | MAX(price) as most_cost,
166 | user_id
167 | FROM products
168 | GROUP BY user_id;
169 |
170 |
171 | ********************************************************************************************************
172 | ******** Date Select Functions ********
173 | ********************************************************************************************************
174 | EXTRACT(Value From )
175 | Examples:
176 | Select id,
177 | EXTRACT(DAY FROM created_at),
178 | EXTRACT(WEEK FROM created_at),
179 | EXTRACT(MONTH FROM created_at),
180 | EXTRACT(YEAR FROM created_at)
181 | FROM users;
182 |
183 | Date Functions with aggregate (count) - How many users were created in each week, include month and year in results.
184 | SELECT COUNT(id),
185 | EXTRACT(WEEK FROM created_at) as week,
186 | EXTRACT(MONTH FROM created_at) as month,
187 | EXTRACT(YEAR FROM created_at) as year
188 | FROM users
189 | GROUP BY year, month, week
190 | ORDER BY year, month, week;
191 |
192 |
193 | ********************************************************************************************************
194 | ******** Filtering ********
195 | ********************************************************************************************************
196 | Filter: Requires using 'WHERE'
197 | Format: WHERE
198 |
199 | >, <, = -- use less than, greater than and equal to filter numbers
200 | != , <> -- use this for “not equal”
201 |
202 | and/or: Add additional clauses to your filter to either further restrict or expand the results
203 | WHERE
204 | AND
205 | AND ( OR )
206 | Example:
207 | SELECT * FROM orders
208 | WHERE shipping_cost > 10
209 | AND created_at > '2018-01-01';
210 |
211 | Combined AND and OR
212 | SELECT * FROM orders
213 | WHERE status IS NOT NULL
214 | AND created_at > '2018-01-01'
215 | AND (order_total > 10.0 OR shipping_total > 10.0) -- grab any orders with either order or shipping total over 10
216 |
217 | Nulls note: If a value being evaluated is null, the results will ONLY be included when specified to include nulls
218 | Example:
219 | SELECT * from orders where status IS NULL
220 | SELECT * from orders where status in ('ready to ship', 'order confirmed') OR status IS NULL
221 |
222 | IN -- use to select data that can be any value in a list
223 | Example:
224 | --SELECT * FROM products
225 | --WHERE product_type_id = 1
226 | --OR product_type_id = 2;
227 | --The above is the same as below
228 | SELECT * FROM products
229 | WHERE product_type_id IN (1, 2);
230 |
231 |
232 | ********************************************************************************************************
233 | ******** Text Filtering ********
234 | ********************************************************************************************************
235 | Exact match (include upper/lower case matched) use '='
236 | Match with wildcards (any text for part of the string) use like, or ilike to be case insensitive
237 | Advanced match or match anywhere in text, use '~', '~*' to be case insensitive
238 |
239 | escape a single quote so it can be searched by using two single quotes
240 | Example:
241 | SELECT * FROM product_types WHERE name = 'girl''s clothing';
242 |
243 | like and ilike - match a string using '%' to identify where any set of characters/words is acceptable
244 | like -- matches the case exactly
245 | ilike -- will ignore case when matching
246 | Examples: (return same results/rows)
247 | SELECT * from product_types WHERE name like '%clothing'
248 | SELECT * from product_types WHERE name ilike '%cLoThInG'
249 |
250 | ~ and ~* -- match the text anywhere in the column/value
251 | ~ -- matches case exactly
252 | ~* -- will ignore case when matching
253 | Examples: (results will be equivalent to the (i)like statements above)
254 | SELECT * from product_types WHERE name ~ 'clothing'
255 | SELECT * from product_types WHERE name ~* 'cLoThInG'
256 |
257 |
258 |
259 | ********************************************************************************************************
260 | ******** Dealing with Nulls ********
261 | ********************************************************************************************************
262 | NOTE: SELECT and WHERE will each treat nulls independantly.
263 | If you only address nulls in the filter, nulls will still be returned in results.
264 | If you only address nulls in the select, the filter won't have that context
265 |
266 | Filtering with Nulls - If a value being filtered is null, the results will ONLY be included when specified to include nulls
267 | Example:
268 | SELECT * from orders
269 | WHERE status IS NULL
270 |
271 | SELECT * from orders
272 | WHERE status in ('ready to ship', 'order confirmed')
273 | OR status IS NULL
274 |
275 | SELECT * FROM orders
276 | WHERE status not ilike '%complete%'
277 | OR status IS NULL
278 |
279 | Coalesce - use coalesce to convert null values to a specific result. Can be used in select and/or where
280 | Format: coalesce(,)
281 | Examples:
282 | SELECT * FROM orders
283 | WHERE coalesce(status,'just made') not ilike '%complete%';
284 |
285 | SELECT coalesce(status, 'Not Started'), *
286 | FROM orders
287 | ORDER BY status NULLS first;
288 |
289 | SELECT coalesce(status, 'Not Started'), *
290 | FROM orders
291 | WHERE coalesce(status,'just made') not ilike '%complete%'
292 | ORDER BY status NULLS first;
293 |
294 |
295 | ********************************************************************************************************
296 | ******** Date Filtering ********
297 | ********************************************************************************************************
298 | >, <, = -- use less than, greater than and equal to filter by date strings
299 | Ex: What orders were created from January through the end of March of this year?
300 | SELECT * FROM orders
301 | WHERE created_at >= '2018-01-01'
302 | AND created_at < '2018-04-01'
303 |
304 | Between -- use Between to be less redundant with dates
305 | EX: What orders were created from January through the end of March of this year?
306 | SELECT * FROM orders
307 | WHERE created_at BETWEEN '2018-01-01'
308 | AND '2018-04-01'
309 |
310 | Extract -- use to filter for exact values being required
311 | EX: What orders were created from January through the end of March of this year?
312 | SELECT * FROM orders
313 | WHERE EXTRACT( year from created_at) = 2018
314 | AND EXTRACT(month from created_at) IN (1,2,3)
315 |
316 |
317 | now() -- return the current timestamp of the database server
318 | Interval -- Manipulate dates by adding or subtracting intervals of dates
319 | Ex: interval '1 month', interval 2 days', interval '4 weeks', interval '1 year'
320 |
321 | Examples:What orders were created in…
322 | the last month? SELECT * FROM orders
323 | WHERE created_at >= now() - interval '1 month'
324 |
325 | the last year? SELECT * FROM orders
326 | WHERE created_at >= now() - interval '1 year'
327 |
328 | the last 45 days? SELECT * FROM orders
329 | WHERE created_at >= now() - interval '45 days'
330 |
331 | SELECT * FROM orders
332 | WHERE created_at >= (now() - interval '1 month')
333 |
334 |
335 | ****************************************************************************************************************************************
336 | ****************************************************************************************************************************************
337 | ************************ DAY 2 ************************
338 | ****************************************************************************************************************************************
339 | ****************************************************************************************************************************************
340 |
341 |
342 | ********************************************************************************************************
343 | ******** Set Operators ********
344 | ********************************************************************************************************
345 | Union - return the unique set of rows unioned together. Columns of results being united must match
346 | Example:
347 | SELECT product_id from order_products
348 | UNION
349 | SELECT product_id from cart_products
350 |
351 | Intersect - return the unique set of rows in the primary results AND in the secondary results
352 | Example: get products that are currently in a cart and have been ordered
353 | SELECT product_id from order_products
354 | INTERSECT
355 | SELECT product_id from cart_products
356 |
357 | Except - return the unique set of rows in the primary results NOT in the secondary results
358 | Example: products not currently in any carts
359 | SELECT id as product_id FROM products
360 | EXCEPT
361 | SELECT product_id FROM cart_products
362 |
363 |
364 | ********************************************************************************************************
365 | ******** Joins ********
366 | ********************************************************************************************************
367 | Joins will align the values of two tables to form a single set of results with columns from both tables.
368 | Using a join requires giving it a clause to use to define how it will pull the data together
369 |
370 | Joining - There are two ways to give join the clause: using ON, and using WHERE
371 | Format: ON - JOIN ON
372 | JOIN ON
373 | WHERE - JOIN , ... WHERE AND
374 | EXAMPLES:
375 | SELECT * FROM products p
376 | JOIN product_types pt ON pt.id = p.product_type_id -- define that we want results aligned on product_type for the product
377 |
378 | SELECT * FROM products p
379 | JOIN product_types pt
380 | WHERE pt.id = p.product_type_id -- define that we want results aligned on product_type for the product
381 |
382 | Inner Join - return rows from both tables where the joined values match on the clause given, JOIN without other indicators is by default an Inner join
383 | Note: join will by default be an INNER join
384 | Example:
385 | SELECT distinct order_products.product_id
386 | FROM order_products
387 | JOIN cart_products ON cart_products.product_id = order_products.product_id
388 |
389 | --Identical to above
390 | SELECT distinct order_products.product_id
391 | FROM order_products
392 | INNER JOIN cart_products on cart_products.product_id = order_products.product_id
393 |
394 | --additional example
395 | SELECT * FROM products p
396 | INNER JOIN product_types pt ON pt.id = p.product_type_id -- define that we want results aligned on product_type for the product
397 |
398 | Outer join - return all rows from the primary table, fill in all values for secondary table that don’t match the primary with null values (otherwise same as inner join)
399 | Left outer join will select the table that is first declared as the primary table
400 | Right outer join will select the table being joined to the first declared table as the primary table
401 | Example: --this will pull all rows from products and fill in cart_product values for those that don’t match
402 | SELECT * FROM products p
403 | LEFT OUTER JOIN cart_products cp on cp.product_id = p.id
404 | Example: This will pull all rows from
405 |
406 | full outer join - return all rows from all tables and fill in any unmatched rows with
407 | Example:
408 | SELECT * FROM orders o
409 | FULL OUTER JOIN users on o.user_id = u.id
410 |
411 |
412 | ********************************************************************************************************
413 | ******** Table Aliasing ********
414 | ********************************************************************************************************
415 | SELECT distinct order_products.product_id
416 | FROM order_products
417 | JOIN cart_products on cart_products.product_id = order_products.product_id
418 |
419 | -- equivalent to above, refer to order_products as op, cart_products as cp
420 | SELECT op.product_id
421 | FROM order_products op
422 | JOIN cart_products cp on cp.product_id = op.product_id
423 |
424 |
425 | ********************************************************************************************************
426 | ******** Table Creation ********
427 | ********************************************************************************************************
428 | Create table based on a query
429 | Creating tables: SELECT ... INTO and CREATE TABLE AS ...
430 | Create a table that will remain in the database based on a query
431 |
432 | Example: Creating an addresses table
433 | SELECT address INTO addresses FROM users;
434 | CREATE TABLE addresses AS SELECT address FROM users;
435 |
436 | Create Table
437 | Format: CREATE TABLE ( ,
438 | ,... )
439 |
440 | CREATE TABLE special_partner_products
441 | (id integer,
442 | partner_name text,
443 | product_name text,
444 | product_cost decimal,
445 | product_purchase_date date,
446 | sponsored_user_id integer
447 | );
448 |
449 | select * from special_partner_products;
450 |
451 |
--------------------------------------------------------------------------------
/intro_to_postgres_setup.sql:
--------------------------------------------------------------------------------
1 | --first setup database tables per Case Study
2 | DROP TABLE IF EXISTS users CASCADE;
3 | CREATE TABLE users (
4 | id SERIAL PRIMARY KEY,
5 | name varchar(50),
6 | address text,
7 | email varchar(80),
8 | password varchar(100),
9 | created_at TIMESTAMP
10 | );
11 |
12 | DROP TABLE IF EXISTS product_types CASCADE;
13 | CREATE TABLE product_types (
14 | id SERIAL PRIMARY KEY,
15 | name varchar(100)
16 | );
17 |
18 | DROP TABLE IF EXISTS products CASCADE;
19 | CREATE TABLE products (
20 | id SERIAL PRIMARY KEY,
21 | name varchar(100),
22 | price float,
23 | grams float,
24 | product_type_id integer REFERENCES product_types(id)
25 | );
26 |
27 | DROP TABLE IF EXISTS orders CASCADE;
28 | CREATE TABLE orders (
29 | id SERIAL PRIMARY KEY,
30 | user_id integer REFERENCES users(id),
31 | status varchar(20),
32 | shipping_total float, --limit orders to be 9999.99 and under
33 | order_total float,
34 | address text,
35 | created_at TIMESTAMP
36 | );
37 |
38 | DROP TABLE IF EXISTS order_products CASCADE;
39 | CREATE TABLE order_products (
40 | id SERIAL PRIMARY KEY,
41 | product_id integer REFERENCES products(id),
42 | order_id integer REFERENCES orders(id)
43 | );
44 |
45 | DROP TABLE IF EXISTS carts CASCADE;
46 | CREATE TABLE carts (
47 | id SERIAL PRIMARY KEY,
48 | user_id integer REFERENCES users(id),
49 | last_updated TIMESTAMP
50 | );
51 |
52 | DROP TABLE IF EXISTS cart_products CASCADE;
53 | CREATE TABLE cart_products (
54 | id SERIAL PRIMARY KEY,
55 | cart_id integer REFERENCES carts(id),
56 | product_id integer REFERENCES products(id),
57 | created_at TIMESTAMP
58 | );
59 |
60 | CREATE OR REPLACE FUNCTION random_date(months_back TEXT)
61 | RETURNS TIMESTAMP AS $$
62 | BEGIN
63 | RETURN (now() - $1::INTERVAL + (floor(random() * 7) * '1 day'::interval));
64 | END;
65 | $$ language 'plpgsql' STRICT;
66 |
67 | INSERT INTO users ("name", address, email, "password", "created_at")
68 | VALUES
69 | ('John Nobody', '123 nowhere town 97382', 'jno@somewhere.com', md5('oneWayHash' || 'password'), random_date('20 months')), --hashed password for security
70 | ('John Somebody', '234 a place ville 36290', 'jsome@nowhere.com', md5('oneWayHash' || 'password2'), random_date('22 months')),
71 | ('John Doe', '345 doesville 32193', 'jd@somewhere.com', md5('oneWayHash' || 'password3'), random_date('22 months')),
72 | ('John Moe', '456 somewhere 36256', 'jmoe@nowhere.com', md5('oneWayHash' || 'password4'), random_date('21 months')),
73 | ('Mary Somebody', '567 one place 47352', 'marys@nowhere.com', md5('oneWayHash' || 'password5'), random_date('21 months')),
74 | ('Mary Nobody', '678 longsville 74620', 'm_no@nowhere.com', md5('oneWayHash' || 'password6'), random_date('20 months')),
75 | ('Mary Doe', '789 wheresit 36293', 'mard@nowhere.com', md5('oneWayHash' || 'password7'), random_date('20 months')),
76 | ('Mary Moe', '109 moetown 32983', 'mary_m@nowhere.com', md5('oneWayHash' || 'password8'), random_date('18 months')),
77 | ('Jack Smith', '492 myplace 96403', 'jacks@workplace.com', md5('oneWayHash' || 'password9'), random_date('16 months')),
78 | ('Angela Parker', '472 townsville 29386', 'angie@parksplace.com', md5('oneWayHash' || 'password10'), random_date('14 months')),
79 | ('Paul Baron', '4862 hillton 38352', 'pb@someplace.com', md5('oneWayHash' || 'password11'), random_date('10 months')),
80 | ('George Washington', '9462 citytown 93628', 'g_dubb@whitehouse.com', md5('oneWayHash' || 'password12'), random_date('9 months')),
81 | ('Martha Washington', '3826 Unit B atown 39264', 'mrs_dubb@whitehouse.com', md5('oneWayHash' || 'password13'), random_date('6 months')),
82 | ('Jane Smith', '502 downtown 96463', 'jane_smith275@thatplace.com', md5('oneWayHash' || 'password14'), random_date('5 months')),
83 | ('Jerry Love', '734 Apt# 362 uptown 83628', 'jlove75@aplace.com',md5('oneWayHash' || 'password15'), random_date('4 months')),
84 | ('Jake Farm', '402 lasttwon 65217', 'jake@somefarm.com',md5('oneWayHash' || 'password16'), random_date('3 months')),
85 | ('Janet Farm', '402 lasttwon 65217', 'janet@somefarm.com',md5('oneWayHash' || 'password17'), random_date('2 months'));
86 |
87 |
88 | INSERT INTO product_types (name)
89 | VALUES ('computer hardware'),
90 | ('computer software'),
91 | ('toys and games'),
92 | ('books'),
93 | ('men''s clothing'),
94 | ('boy''s clothing'),
95 | ('women''s clothing'),
96 | ('girl''s clothing'),
97 | ('shoes'),
98 | ('accessories'),
99 | ('home decor'),
100 | ('kitchen and dining');
101 |
102 | INSERT INTO products (name, price, grams, product_type_id)
103 | VALUES
104 | ('motherboard',124.99,62.5,(select id from product_types where name = 'computer hardware')),
105 | ('hard drive',38.00,57.5,(select id from product_types where name = 'computer hardware')),
106 | ('external hard drive',44.99,82.2,(select id from product_types where name = 'computer hardware')),
107 | ('monitor',54.98,163.7,(select id from product_types where name = 'computer hardware')),
108 | ('keyboard',65.00,26.0,(select id from product_types where name = 'computer hardware')),
109 | ('mouse',26.99,72.3,(select id from product_types where name = 'computer hardware')),
110 | ('computer speakers',73.45,27.2,(select id from product_types where name = 'computer hardware')),
111 | ('antivirus',39.99,12.0,(select id from product_types where name = 'computer software')),
112 | ('text editor',19.99,13.5,(select id from product_types where name = 'computer software')),
113 | ('strategy game',59.99,11.5,(select id from product_types where name = 'computer software')),
114 | ('first person shooter computer game',59.99,14.2,(select id from product_types where name = 'computer software')),
115 | ('accounting software',44.99,17.7,(select id from product_types where name = 'computer software')),
116 | ('tax software',24.99,12.8,(select id from product_types where name = 'computer software')),
117 | ('video editing software',69.00,21.0,(select id from product_types where name = 'computer software')),
118 | ('some book',2.43,14.2,(select id from product_types where name = 'books')),
119 | ('another book',19.99,21.5,(select id from product_types where name = 'books')),
120 | ('vampire book',14.99,11.0,(select id from product_types where name = 'books')),
121 | ('fishing book',9.99,16.0,(select id from product_types where name = 'books')),
122 | ('baby book',12.95,12.4,(select id from product_types where name = 'books')),
123 | ('blue jeans', 44.95, 24.1, (select id from product_types where name = 'men''s clothing')),
124 | ('slacks', 74.99, 21.0, (select id from product_types where name = 'men''s clothing')),
125 | ('t-shirt', 19.24, 15.2, (select id from product_types where name = 'men''s clothing')),
126 | ('boxers', 15.00, 8.1, (select id from product_types where name = 'men''s clothing')),
127 | ('socks', 5.99, 4.2, (select id from product_types where name = 'boy''s clothing')),
128 | ('sweater', 45.90, null, (select id from product_types where name = 'boy''s clothing')),
129 | ('blouse', 89.99, 18.2, (select id from product_types where name = 'women''s clothing')),
130 | ('tank top', 18.00, 13.0, (select id from product_types where name = 'women''s clothing')),
131 | ('bikini swimsuit', 75.00, 9.3, (select id from product_types where name = 'women''s clothing')),
132 | ('skirt', 37.00, 25.38, (select id from product_types where name = 'women''s clothing')),
133 | ('dress', 39.00, 42.0, (select id from product_types where name = 'girl''s clothing')),
134 | ('pants', 20.00, 26.5, (select id from product_types where name = 'girl''s clothing')),
135 | ('socks', 3.99, 10.0, (select id from product_types where name = 'girl''s clothing')),
136 | ('t-shirt', 12.00, 19.7, (select id from product_types where name = 'girl''s clothing')),
137 | ('slippers', 13.99, 12.8, (select id from product_types where name = 'shoes')),
138 | ('sports shoes', 49.99, 31.2, (select id from product_types where name = 'shoes')),
139 | ('boots', 69.00, 42.7, (select id from product_types where name = 'shoes')),
140 | ('high heels', 79.00, 32.9, (select id from product_types where name = 'shoes')),
141 | ('tote bag', 29.00, 24.0, (select id from product_types where name = 'accessories')),
142 | ('scarf', 18.99, null, (select id from product_types where name = 'accessories')),
143 | ('belt', 15.00, 14.2, (select id from product_types where name = 'accessories')),
144 | ('hat', 21.99, null, (select id from product_types where name = 'accessories')),
145 | ('hair ties', 1.99, 4.7, (select id from product_types where name = 'accessories')),
146 | ('clutch purse', 35.99, 18.8, (select id from product_types where name = 'accessories')),
147 | ('cufflinks', 12.85, 13.4, (select id from product_types where name = 'accessories')),
148 | ('coaster', 15.99, 8.6, (select id from product_types where name = 'home decor')),
149 | ('clock', 22.00, 15.3, (select id from product_types where name = 'home decor')),
150 | ('wall hooks', 8.59, 6.4, (select id from product_types where name = 'home decor')),
151 | ('desk lamp', 35.00, 59.3, (select id from product_types where name = 'home decor')),
152 | ('lavender candle', 10.00, 21.6, (select id from product_types where name = 'home decor')),
153 | ('ladle', 14.99, 12.7, (select id from product_types where name = 'kitchen and dining')),
154 | ('plate set', 40.00, 48.2, (select id from product_types where name = 'kitchen and dining')),
155 | ('animal print bowl set', 30.00, 38.9, (select id from product_types where name = 'kitchen and dining')),
156 | ('stemless wine glasses', 35.00, 27.2, (select id from product_types where name = 'kitchen and dining')),
157 | ('silverware set', 19.99, 19.7, (select id from product_types where name = 'kitchen and dining')),
158 | ('napkin holders', 14.98, 8.3, (select id from product_types where name = 'kitchen and dining'));
159 |
160 | --create a function to setup our random values based on a max
161 | CREATE OR REPLACE FUNCTION random_to(high INT)
162 | RETURNS INT AS $$
163 | BEGIN
164 | RETURN floor(random()* (high) + 1);
165 | END;
166 | $$ language 'plpgsql' STRICT;
167 |
168 | --ORDER STATUS: null- in the checkout stage, ‘order confirmed’, ‘shipped’, ‘complete’
169 | INSERT INTO orders (user_id, status, shipping_total, order_total, address, created_at)
170 | VALUES
171 | (1, 'complete', 24.75, 0.00, '745 noplace 78362', (random_date('19 months') )),
172 | (1, 'complete', 54.58, 0.00, '456 somewhere 85436', (random_date('18 months') )),
173 | (1, 'complete', 16.21, 00.0, '456 somewhere 85436', (random_date('17 months'))),
174 | (1, 'complete', 21.83, 00.0, '678 nowhere 32616', (random_date('16 months'))),
175 | (1, 'complete', 12.58, 00.0, '678 nowhere 32616', (random_date('15 months'))),
176 | (1, 'complete', 12.94, 00.0, '678 nowhere 32616', (random_date('14 months'))),
177 | (1, 'complete', 15.85, 00.0, '678 nowhere 32616', (random_date('13 months'))),
178 | (1, 'complete', 20.46, 00.0, '678 nowhere 32616', (random_date('12 months'))),
179 | (1, 'complete', 17.84, 00.0, '678 nowhere 32616', (random_date('10 months'))),
180 | (1, 'complete', 15.29, 00.0, '678 nowhere 32616', (random_date('9 months'))),
181 | (1, 'complete', 10.47, 00.0, '678 nowhere 32616', (random_date('8 months'))),
182 | (1, 'complete', 14.72, 00.0, '678 nowhere 32616', (random_date('6 months'))),
183 | (1, 'complete', 12.75, 00.0, '678 nowhere 32616', (random_date('5 months'))),
184 | (1, 'complete', 9.50, 00.0, '678 nowhere 32616', (random_date('5 months'))),
185 | (1, 'complete', 14.04, 00.0, '678 nowhere 32616', (random_date('4 months'))),
186 | (1, 'complete', 11.28, 00.0, '678 nowhere 32616', (random_date('4 months'))),
187 | (1, 'complete', 8.59, 00.0, '678 nowhere 32616', (random_date('3 months'))),
188 | (1, 'complete', 18.26, 00.0, '678 nowhere 32616', (random_date('2 months'))),
189 | (1, 'ready to ship', 14.51, 00.0, '678 nowhere 32616', (random_date('1 months'))),
190 | (1, 'order confirmed', 8.62, 00.0, '678 nowhere 32616', (random_date('1 months'))),
191 | (2, 'complete', 17.82, 0.00, '9262 new place 78352', (random_date('19 months') )),
192 | (2, 'complete', 29.82, 0.00, '9262 new place 78352', (random_date('18 months') )),
193 | (2, 'complete', 43.27, 00.0, '372 others road 85438', (random_date('17 months'))),
194 | (2, 'complete', 12.93, 00.0, '9262 new place 78352', (random_date('16 months'))),
195 | (2, 'complete', 35.82, 00.0, '9262 new place 78352', (random_date('15 months'))),
196 | (2, 'complete', 12.93, 00.0, '9262 new place 78352', (random_date('14 months'))),
197 | (2, 'complete', 18.52, 00.0, '9262 new place 78352', (random_date('12 months'))),
198 | (2, 'complete', 19.17, 00.0, '372 others road 85438', (random_date('10 months'))),
199 | (2, 'complete', 21.27, 00.0, '9262 new place 78352', (random_date('9 months'))),
200 | (2, 'complete', 31.46, 00.0, '9262 new place 78352', (random_date('7 months'))),
201 | (2, 'complete', 14.19, 00.0, '9262 new place 78352', (random_date('6 months'))),
202 | (2, 'complete', 14.27, 00.0, '9262 new place 78352', (random_date('5 months'))),
203 | (2, 'complete', 21.73, 00.0, '9262 new place 78352', (random_date('4 months'))),
204 | (2, 'complete', 21.73, 00.0, '9262 new place 78352', (random_date('5 months'))),
205 | (2, 'complete', 12.91, 00.0, '234 a place ville 36290', (random_date('3 months'))),
206 | (2, 'complete', 17.39, 00.0, '234 a place ville 36290', (random_date('2 months'))),
207 | (2, 'shipped', 19.27, 00.0, '234 a place ville 36290', (random_date('2 months'))),
208 | (2, null, 15.82, 00.0, '234 a place ville 36290', (random_date('1 months'))),
209 | (3, 'complete', 8.72, 0.00, '21718 biplane 38296', (random_date('19 months') )),
210 | (3, 'complete', 11.82, 0.00, '21718 biplane 38296', (random_date('17 months') )),
211 | (3, 'complete', 9.78, 00.0, '21718 biplane 38296', (random_date('14 months'))),
212 | (3, 'complete', 15.82, 00.0, '21718 biplane 38296', (random_date('10 months'))),
213 | (3, 'complete', 31.82, 00.0, '21718 biplane 38296', (random_date('8 months'))),
214 | (3, 'complete', 13.98, 00.0, '21718 biplane 38296', (random_date('7 months'))),
215 | (3, 'complete', 13.71, 00.0, '345 doesville 32193', (random_date('6 months'))),
216 | (3, 'complete', 19.71, 00.0, '345 doesville 32193', (random_date('5 months'))),
217 | (3, 'complete', 24.83, 00.0, '345 doesville 32193', (random_date('5 months'))),
218 | (3, 'complete', 14.92, 00.0, '345 doesville 32193', (random_date('4 months'))),
219 | (3, 'complete', 18.32, 00.0, '345 doesville 32193', (random_date('3 months'))),
220 | (3, 'complete', 19.47, 00.0, '345 doesville 32193', (random_date('3 months'))),
221 | (3, 'shipped', 21.82, 00.0, '345 doesville 32193', (random_date('1 months'))),
222 | (3, null, null, 00.0, '345 doesville 32193', (random_date('1 months'))),
223 | (4, 'complete', 19.61, 0.00, '16532 village town 37261', (random_date('19 months') )),
224 | (4, 'complete', 21.73, 0.00, '16532 village town 37261', (random_date('18 months') )),
225 | (4, 'complete', 23.92, 00.0, '16532 village town 37261', (random_date('17 months'))),
226 | (4, 'complete', 15.23, 00.0, '16532 village town 37261', (random_date('16 months'))),
227 | (4, 'complete', 21.82, 00.0, '16532 village town 37261', (random_date('15 months'))),
228 | (4, 'complete', 19.43, 00.0, '16532 village town 37261', (random_date('14 months'))),
229 | (4, 'complete', 13.22, 00.0, '16532 village town 37261', (random_date('12 months'))),
230 | (4, 'complete', 17.57, 00.0, '456 somewhere 36256', (random_date('10 months'))),
231 | (4, 'complete', 18.27, 00.0, '456 somewhere 36256', (random_date('9 months'))),
232 | (4, 'complete', 16.52, 00.0, '456 somewhere 36256', (random_date('7 months'))),
233 | (4, 'complete', 14.82, 00.0, '456 somewhere 36256', (random_date('6 months'))),
234 | (4, 'complete', 13.25, 00.0, '456 somewhere 36256', (random_date('5 months'))),
235 | (4, 'complete', 17.43, 00.0, '456 somewhere 36256', (random_date('4 months'))),
236 | (4, 'complete', 18.27, 00.0, '456 somewhere 36256', (random_date('5 months'))),
237 | (4, 'complete', 19.15, 00.0, '456 somewhere 36256', (random_date('3 months'))),
238 | (4, 'complete', 14.45, 00.0, '456 somewhere 36256', (random_date('2 months'))),
239 | (4, 'complete', 12.24, 00.0, '456 somewhere 36256', (random_date('2 months'))),
240 | (4, 'order confirmed', 16.23, 00.0, '456 somewhere 36256', (random_date('1 months'))),
241 | (5, 'complete', 19.61, 0.00, '567 one place 47352', (random_date('19 months') )),
242 | (5, 'complete', 21.73, 0.00, '567 one place 47352', (random_date('17 months') )),
243 | (5, 'complete', 23.92, 00.0, '567 one place 47352', (random_date('14 months'))),
244 | (5, 'complete', 15.23, 00.0, '567 one place 47352', (random_date('12 months'))),
245 | (5, 'complete', 21.82, 00.0, '567 one place 47352', (random_date('9 months'))),
246 | (5, 'complete', 19.43, 00.0, '567 one place 47352', (random_date('5 months'))),
247 | (5, 'shipped', 13.22, 00.0, '567 one place 47352', (random_date('2 months'))),
248 | (5, 'order confirmed', 17.57, 00.0, '567 one place 47352', (random_date('1 months'))),
249 | (6, 'complete', 17.61, 0.00, '678 longsville 74620', (random_date('19 months') )),
250 | (6, 'complete', 12.62, 0.00, '678 longsville 74620', (random_date('16 months') )),
251 | (6, 'complete', 16.92, 0.00, '678 longsville 74620', (random_date('13 months') )),
252 | (6, 'complete', 11.81, 0.00, '678 longsville 74620', (random_date('8 months') )),
253 | (6, 'shipped', 16.16, 0.00, '678 longsville 74620', (random_date('5 months') )),
254 | (7, 'complete', 14.12, 0.00, '789 wheresit 36293', (random_date('16 months') )),
255 | (7, 'complete', 7.56, 0.00, '789 wheresit 36293', (random_date('13 months') )),
256 | (7, 'complete', 13.25, 0.00, '789 wheresit 36293', (random_date('10 months') )),
257 | (7, 'complete', 9.39, 0.00, '789 wheresit 36293', (random_date('7 months') )),
258 | (7, 'complete', 9.71, 0.00, '789 wheresit 36293', (random_date('5 months') )),
259 | (7, 'complete', 10.27, 0.00,'789 wheresit 36293', (random_date('3 months') )),
260 | (7, 'shipped', 10.82, 0.00, '789 wheresit 36293', (random_date('1 months') )),
261 | (7, null, null, 0.00, '789 wheresit 36293', (random_date('10 days') )),
262 | (8, 'complete', 14.72, 0.00, '109 moetown 32983', (random_date('16 months') )),
263 | (8, 'complete', 10.26, 0.00, '109 moetown 32983', (random_date('14 months') )),
264 | (8, 'complete', 21.82, 0.00, '109 moetown 32983', (random_date('11 months') )),
265 | (8, 'complete', 11.42, 0.00, '109 moetown 32983', (random_date('8 months') )),
266 | (8, 'complete', 10.63, 0.00, '109 moetown 32983', (random_date('7 months') )),
267 | (8, 'complete', 8.92, 0.00, '109 moetown 32983', (random_date('7 months') )),
268 | (8, 'complete', 11.28, 0.00, '109 moetown 32983', (random_date('6 months') )),
269 | (8, 'complete', 13.37, 0.00, '109 moetown 32983', (random_date('5 months') )),
270 | (8, 'complete', 12.51, 0.00, '109 moetown 32983', (random_date('4 months') )),
271 | (8, 'complete', 17.9, 0.00, '109 moetown 32983', (random_date('3 months') )),
272 | (8, 'complete', 15.31, 0.00, '109 moetown 32983', (random_date('3 months') )),
273 | (8, 'complete', 16.38, 0.00, '109 moetown 32983', (random_date('2 months') )),
274 | (8, 'shipped', 13.29, 0.00, '109 moetown 32983', (random_date('1 months') )),
275 | (8, 'order confirmed', 10.27, 0.00, '109 moetown 32983', (random_date('1 months') )),
276 | (8, null, null, 0.00, '109 moetown 32983', (random_date('7 days') )),
277 | (9, 'complete', 21.85, 0.00, '36127 avenue road 96405', (random_date('14 months') )),
278 | (9, 'complete', 17.53, 0.00, '36127 avenue road 96405', (random_date('13 months') )),
279 | (9, 'complete', 13.71, 0.00, '36127 avenue road 96405', (random_date('12 months') )),
280 | (9, 'complete', 8.37, 0.00, '36127 avenue road 96405', (random_date('12 months') )),
281 | (9, 'complete', 13.71, 0.00, '36127 avenue road 96405', (random_date('11 months') )),
282 | (9, 'complete', 10.49, 0.00, '36127 avenue road 96405', (random_date('9 months') )),
283 | (9, 'complete', 7.72, 0.00, '36127 avenue road 96405', (random_date('8 months') )),
284 | (9, 'complete', 14.73, 0.00, '492 myplace 96403', (random_date('6 months') )),
285 | (9, 'complete', 12.49, 0.00, '492 myplace 96403', (random_date('5 months') )),
286 | (9, 'complete', 11.02, 0.00, '492 myplace 96403', (random_date('3 months') )),
287 | (9, 'shipped', 9.83, 0.00, '492 myplace 96403', (random_date('1 months') )),
288 | (9, null, null, 0.00, '492 myplace 96403', (random_date('8 days') )),
289 | (10, 'complete', 11.84, 0.00, '472 townsville 29386', (random_date('12 months') )),
290 | (10, 'complete', 10.59, 0.00, '472 townsville 29386', (random_date('11 months') )),
291 | (10, 'complete', 9.26, 0.00, '472 townsville 29386', (random_date('9 months') )),
292 | (10, 'complete', 10.35, 0.00, '472 townsville 29386', (random_date('7 months') )),
293 | (10, 'complete', 9.25, 0.00, '472 townsville 29386', (random_date('6 months') )),
294 | (10, 'complete', 12.85, 0.00, '472 townsville 29386', (random_date('5 months') )),
295 | (10, 'complete', 11.54, 0.00, '472 townsville 29386', (random_date('3 months') )),
296 | (10, null, null, 0.00, '472 townsville 29386', (random_date('5 days') )),
297 | (11, 'complete', 11.84, 0.00, '72512 simsville 38353', (random_date('9 months') )),
298 | (11, 'complete', 13.45, 0.00, '72512 simsville 38353', (random_date('8 months') )),
299 | (11, 'complete', 9.39, 0.00, '4862 hillton 38352', (random_date('7 months') )),
300 | (11, 'complete', 13.83, 0.00, '4862 hillton 38352', (random_date('6 months') )),
301 | (11, 'complete', 12.73, 0.00, '4862 hillton 38352', (random_date('4 months') )),
302 | (11, 'complete', 10.69, 0.00, '4862 hillton 38352', (random_date('2 months') )),
303 | (11, 'shipped', 8.92, 0.00, '4862 hillton 38352', (random_date('1 months') )),
304 | (11, 'order confirmed', 11.92, 0.00, '4862 hillton 38352', (random_date('20 days') )),
305 | (12, 'complete', 13.84, 0.00, '9462 citytown 93628', (random_date('7 months') )),
306 | (12, 'complete', 16.84, 0.00, '9462 citytown 93628', (random_date('6 months') )),
307 | (12, 'complete', 9.84, 0.00, '9462 citytown 93628', (random_date('4 months') )),
308 | (12, 'complete', 6.84, 0.00, '9462 citytown 93628', (random_date('3 months') )),
309 | (12, 'shipped', 4.84, 0.00, '9462 citytown 93628', (random_date('1 months') )),
310 | (12, null, null, 0.00, '9462 citytown 93628', (random_date('9 days') )),
311 | (13, 'complete', 12.39, 0.00, '3826 Unit B atown 39264', (random_date('5 months') )),
312 | (13, 'complete', 8.72, 0.00, '3826 Unit B atown 39264', (random_date('5 months') )),
313 | (13, 'complete', 9.53, 0.00, '3826 Unit B atown 39264', (random_date('4 months') )),
314 | (13, 'complete', 7.29, 0.00, '3826 Unit B atown 39264', (random_date('4 months') )),
315 | (13, 'complete', 10.69, 0.00, '3826 Unit B atown 39264', (random_date('3 months') )),
316 | (13, 'complete', 16.96, 0.00, '3826 Unit B atown 39264', (random_date('3 months') )),
317 | (13, 'complete', 11.92, 0.00, '3826 Unit B atown 39264', (random_date('2 months') )),
318 | (13, 'complete', 9.80, 0.00, '3826 Unit B atown 39264', (random_date('2 months') )),
319 | (13, 'complete', 10.39, 0.00, '3826 Unit B atown 39264', (random_date('1 months') )),
320 | (13, 'order confirmed', 9.86, 0.00, '3826 Unit B atown 39264', (random_date('14 days') )),
321 | (15, 'complete', 9.84, 0.00, '734 Apt# 362 uptown 83628', (random_date('4 months') )),
322 | (15, 'complete', 10.29, 0.00, '734 Apt# 362 uptown 83628', (random_date('3 months') )),
323 | (15, 'complete', 11.74, 0.00, '734 Apt# 362 uptown 83628', (random_date('3 months') )),
324 | (15, 'complete', 12.93, 0.00, '734 Apt# 362 uptown 83628', (random_date('2 months') )),
325 | (15, 'shipped', 14.49, 0.00, '734 Apt# 362 uptown 83628', (random_date('1 months') )),
326 | (15, null, null, 0.00, '734 Apt# 362 uptown 83628', (random_date('12 day') )),
327 | (16, 'complete', 9.84, 0.00, '402 lasttwon 65217', (random_date('3 months') )),
328 | (16, 'shipped', 9.84, 0.00, '402 lasttwon 65217', (random_date('1 months') )),
329 | (16, null, null, 0.00, '402 lasttwon 65217', (random_date('7 days') ));
330 |
331 |
332 | INSERT INTO order_products (order_id, product_id)
333 | SELECT generate_series,floor(random_to((select max(id) from products)))
334 | FROM generate_series(1,(select max(id) from orders))
335 | UNION
336 | SELECT 1,floor(random_to((select max(id) from products)) )
337 | FROM generate_series(1,3)
338 | UNION
339 | SELECT 2,floor(random_to((select max(id) from products)) )
340 | FROM generate_series(1,3)
341 | UNION
342 | SELECT 3,floor(random_to((select max(id) from products)) )
343 | FROM generate_series(1,3)
344 | UNION
345 | SELECT 4,floor(random_to((select max(id) from products)) )
346 | FROM generate_series(1,3)
347 | UNION
348 | SELECT 5,floor(random_to((select max(id) from products)) )
349 | FROM generate_series(1,3)
350 | UNION
351 | SELECT 6,floor(random_to((select max(id) from products)) )
352 | FROM generate_series(1,3)
353 | UNION
354 | SELECT 7,floor(random_to((select max(id) from products)) )
355 | FROM generate_series(1,3)
356 | UNION
357 | SELECT 8,floor(random_to((select max(id) from products)) )
358 | FROM generate_series(1,3)
359 | UNION
360 | SELECT 9,floor(random_to((select max(id) from products)) )
361 | FROM generate_series(1,3)
362 | UNION
363 | SELECT 10,floor(random_to((select max(id) from products)) )
364 | FROM generate_series(1,3)
365 | UNION
366 | SELECT 11,floor(random_to((select max(id) from products)) )
367 | FROM generate_series(1,3)
368 | UNION
369 | SELECT 12,floor(random_to((select max(id) from products)) )
370 | FROM generate_series(1,3)
371 | UNION
372 | SELECT 13,floor(random_to((select max(id) from products)) )
373 | FROM generate_series(1,3)
374 | UNION
375 | SELECT 14,floor(random_to((select max(id) from products)) )
376 | FROM generate_series(1,3)
377 | UNION
378 | SELECT 15,floor(random_to((select max(id) from products)) )
379 | FROM generate_series(1,3)
380 | UNION
381 | SELECT 16,floor(random_to((select max(id) from products)) )
382 | FROM generate_series(1,3)
383 | UNION --make a specific product ID the one sold the most last year
384 | SELECT floor(random_to((select max(id) from products)) ), 22
385 | FROM generate_series(1,15)
386 | ;
387 |
388 | --Delete all records of 1 product ID to ensure some are never ordered to make the data valuable
389 | DELETE FROM order_products WHERE product_id = (select id from products where name = 'another book');
390 |
391 | --add some 'random' values to the products too
392 | INSERT INTO order_products (order_id, product_id)
393 | SELECT floor(random_to((select max(id) from orders ))),floor(random_to((select max(id) from products)) )
394 | FROM generate_series(1,50);
395 |
396 | UPDATE orders SET order_total = total
397 | FROM
398 | (SELECT op.order_id, sum(p.price) as total
399 | FROM order_products op, products p
400 | WHERE op.product_id = p.id
401 | GROUP BY op.order_id) order_set
402 | WHERE order_set.order_id = orders.id;
403 |
404 | --create some carts
405 | INSERT INTO carts (user_id, last_updated)
406 | VALUES
407 | (1, random_date('19 months') ),
408 | (2, random_date('19 months') ),
409 | (3, random_date('19 months') ),
410 | (4, random_date('18 months') ),
411 | (5, random_date('17 months') ),
412 | (6, random_date('16 months') ),
413 | (7, random_date('15 months') ),
414 | (8, random_date('14 months') ),
415 | (9, random_date('13 months') ),
416 | (10, random_date('12 months')),
417 | (11, random_date('9 months') ),
418 | (12, random_date('8 months') ),
419 | (13, random_date('8 months') ),
420 | (14, random_date('6 months') ),
421 | (15, random_date('4 months') ),
422 | (16, random_date('2 months') ),
423 | (17, random_date('1 month') );
424 |
425 | --Add some products to some carts
426 | INSERT INTO cart_products (cart_id, product_id, created_at)
427 | SELECT floor(random_to((select max(id) from carts ))),floor(random_to((select max(id) from products)) ), random_date('4 months')
428 | FROM generate_series(1,5)
429 | UNION
430 | SELECT floor(random_to((select max(id) from carts))),floor(random_to((select max(id) from products)) ), random_date('3 months')
431 | FROM generate_series(1,5)
432 | UNION
433 | SELECT floor(random_to((select max(id) from carts ))),floor(random_to((select max(id) from products)) ), random_date('2 months')
434 | FROM generate_series(1,5)
435 | UNION
436 | SELECT floor(random_to((select max(id) from carts ))),floor(random_to((select max(id) from products)) ), random_date('1 months')
437 | FROM generate_series(1,5)
438 | UNION
439 | SELECT floor(random_to((select max(id) from carts ))),floor(random_to((select max(id) from products)) ), random_date('14 days')
440 | FROM generate_series(1,5)
441 | UNION
442 | SELECT floor(random_to((select max(id) from carts ))),floor(random_to((select max(id) from products)) ), random_date('7 months')
443 | FROM generate_series(1,5)
444 | UNION
445 | SELECT floor(random_to((select max(id) from carts ))),floor(random_to((select max(id) from products)) ), random_date('3 days')
446 | FROM generate_series(1,5);
447 |
--------------------------------------------------------------------------------
/setup_instructions.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Jess88/Introduction-to-PostgreSQL/a48e9e90da670d9c4fa6bf11f66a4985c9e73bdb/setup_instructions.pdf
--------------------------------------------------------------------------------
/sponsored_products.csv:
--------------------------------------------------------------------------------
1 | ID,partner_name,product_name,product_cost,product_purchase_date,sponsored_user_id
2 | 1,"""Home and Garden Vendor""","""some product81""",13.59,2/12/18,19
3 | 2,"""Home Products Vendor""","""rand product55""",60.39,3/6/18,75
4 | 3,"""Home Products Vendor""","""the product6""",39.03,1/16/18,59
5 | 4,"""Services Vendor""","""a product33""",32.27,1/19/18,96
6 | 5,"""Home and Garden Vendor""","""rand product40""",55.14,1/27/18,34
7 | 6,"""Home Products Vendor""","""rand product39""",6.83,2/2/18,26
8 | 7,"""Services Vendor""","""the product47""",33.18,1/13/18,88
9 | 8,"""Technology Vendor""","""some product14""",13.29,1/26/18,65
10 | 9,"""Home Products Vendor""","""a product22""",24.77,2/18/18,75
11 | 10,"""Home and Garden Vendor""","""some product39""",56.89,1/14/18,96
12 | 11,"""Home and Garden Vendor""","""the product76""",89.49,2/19/18,75
13 | 12,"""Home and Garden Vendor""","""a product82""",15.52,2/7/18,39
14 | 13,"""Home and Garden Vendor""","""rand product22""",63.77,1/20/18,78
15 | 14,"""Services Vendor""","""some product26""",31.54,2/14/18,30
16 | 15,"""Home Products Vendor""","""a product61""",15.51,1/22/18,34
17 | 16,"""Home and Garden Vendor""","""a product55""",34.6,3/3/18,20
18 | 17,"""Home and Garden Vendor""","""some product40""",15.93,1/25/18,50
19 | 18,"""Home Products Vendor""","""a product73""",71.56,1/20/18,38
20 | 19,"""Home and Garden Vendor""","""some product89""",72.38,1/18/18,57
21 | 20,"""Home Products Vendor""","""a product44""",54.79,1/29/18,30
22 | 21,"""Home and Garden Vendor""","""a product71""",12.16,2/6/18,94
23 | 22,"""Home Products Vendor""","""the product23""",83.86,3/8/18,15
24 | 23,"""Services Vendor""","""a product17""",13.95,1/9/18,70
25 | 24,"""Home Products Vendor""","""a product16""",73.44,3/2/18,23
26 | 25,"""Home Products Vendor""","""rand product5""",31.6,2/10/18,76
27 | 26,"""Home Products Vendor""","""the product15""",19.75,2/22/18,65
28 | 27,"""Home and Garden Vendor""","""the product79""",76.6,3/7/18,87
29 | 28,"""Technology Vendor""","""some product64""",35.78,1/23/18,89
30 | 29,"""Home Products Vendor""","""some product67""",54.24,1/1/18,80
31 | 30,"""Home and Garden Vendor""","""the product7""",16.08,1/8/18,22
32 | 31,"""Home and Garden Vendor""","""the product64""",70.26,1/25/18,80
33 | 32,"""Home Products Vendor""","""rand product94""",17.04,1/23/18,55
34 | 33,"""Technology Vendor""","""rand product3""",45.31,1/29/18,15
35 | 34,"""Home and Garden Vendor""","""rand product54""",49.1,1/13/18,24
36 | 35,"""Services Vendor""","""a product26""",47.11,2/2/18,94
37 | 36,"""Home and Garden Vendor""","""the product60""",61.24,2/18/18,24
38 | 37,"""Home Products Vendor""","""some product99""",82.47,2/17/18,31
39 | 38,"""Home Products Vendor""","""the product57""",83.69,3/2/18,32
40 | 39,"""Technology Vendor""","""some product83""",26.34,2/9/18,30
41 | 40,"""Home and Garden Vendor""","""some product85""",68.7,3/6/18,50
42 | 41,"""Technology Vendor""","""some product93""",23.95,1/16/18,49
43 | 42,"""Technology Vendor""","""some product68""",10.48,2/23/18,72
44 | 43,"""Home Products Vendor""","""some product24""",30.42,1/9/18,19
45 | 44,"""Home and Garden Vendor""","""rand product79""",82.78,1/15/18,23
46 | 45,"""Technology Vendor""","""a product26""",29.48,2/10/18,72
47 | 46,"""Home Products Vendor""","""rand product29""",55.84,1/5/18,69
48 | 47,"""Home Products Vendor""","""the product32""",36.19,3/3/18,78
49 | 48,"""Services Vendor""","""a product28""",37.1,2/22/18,76
50 | 49,"""Technology Vendor""","""a product29""",24.53,2/26/18,82
51 | 50,"""Home Products Vendor""","""the product83""",74.95,2/9/18,18
52 | 51,"""Services Vendor""","""rand product78""",89.56,1/16/18,48
53 | 52,"""Home and Garden Vendor""","""rand product4""",66.13,3/7/18,86
54 | 53,"""Services Vendor""","""the product40""",49.48,2/6/18,41
55 | 54,"""Home and Garden Vendor""","""a product64""",29.11,2/24/18,88
56 | 55,"""Services Vendor""","""some product60""",53.88,2/22/18,90
57 | 56,"""Home Products Vendor""","""rand product45""",13.66,1/16/18,83
58 | 57,"""Home and Garden Vendor""","""some product15""",61.73,1/7/18,46
59 | 58,"""Technology Vendor""","""the product86""",4.73,1/19/18,31
60 | 59,"""Services Vendor""","""the product88""",84.42,1/13/18,67
61 | 60,"""Home Products Vendor""","""the product82""",87.54,2/27/18,79
62 | 61,"""Home and Garden Vendor""","""a product67""",22.36,2/13/18,82
63 | 62,"""Technology Vendor""","""rand product64""",12.24,2/8/18,38
64 | 63,"""Home and Garden Vendor""","""the product26""",61.93,1/11/18,37
65 | 64,"""Home Products Vendor""","""some product21""",18.59,1/31/18,45
66 | 65,"""Home Products Vendor""","""the product31""",9.91,2/25/18,63
67 | 66,"""Home and Garden Vendor""","""some product41""",77.56,1/8/18,39
68 | 67,"""Services Vendor""","""the product66""",65.36,2/6/18,23
69 | 68,"""Technology Vendor""","""the product14""",21.65,2/21/18,39
70 | 69,"""Services Vendor""","""some product73""",16.95,2/14/18,47
71 | 70,"""Technology Vendor""","""the product9""",41.92,2/20/18,52
72 | 71,"""Home Products Vendor""","""some product74""",52.83,1/7/18,59
73 | 72,"""Home and Garden Vendor""","""a product67""",25.31,1/23/18,81
74 | 73,"""Home and Garden Vendor""","""a product67""",9.76,2/25/18,24
75 | 74,"""Home Products Vendor""","""a product51""",40.85,2/15/18,93
76 | 75,"""Services Vendor""","""some product36""",5.62,1/28/18,57
77 | 76,"""Technology Vendor""","""some product65""",6.91,2/8/18,61
78 | 77,"""Services Vendor""","""the product7""",48.06,2/22/18,76
79 | 78,"""Services Vendor""","""rand product34""",69.6,2/9/18,80
80 | 79,"""Technology Vendor""","""a product16""",6.63,1/27/18,84
81 | 80,"""Home and Garden Vendor""","""rand product29""",23.14,2/19/18,34
82 | 81,"""Home and Garden Vendor""","""a product43""",55.76,2/26/18,65
83 | 82,"""Services Vendor""","""a product30""",49.5,2/24/18,84
84 | 83,"""Technology Vendor""","""the product65""",46.87,3/1/18,65
85 | 84,"""Home Products Vendor""","""the product27""",18.44,1/2/18,34
86 | 85,"""Technology Vendor""","""a product45""",57.21,1/5/18,21
87 | 86,"""Services Vendor""","""rand product50""",41.82,2/11/18,82
88 | 87,"""Services Vendor""","""some product79""",47.78,3/4/18,66
89 | 88,"""Home Products Vendor""","""the product85""",43.64,2/14/18,19
90 | 89,"""Services Vendor""","""rand product41""",51.84,2/2/18,17
91 | 90,"""Technology Vendor""","""the product20""",84.44,2/10/18,17
92 | 91,"""Home and Garden Vendor""","""the product78""",14.96,1/15/18,80
93 | 92,"""Technology Vendor""","""the product9""",10.85,3/6/18,72
94 | 93,"""Home Products Vendor""","""the product79""",28.04,2/5/18,43
95 | 94,"""Home and Garden Vendor""","""the product26""",13.67,2/23/18,51
96 | 95,"""Home and Garden Vendor""","""the product54""",30.65,2/25/18,81
97 | 96,"""Technology Vendor""","""some product78""",58.88,2/1/18,22
98 | 97,"""Home Products Vendor""","""the product11""",6.29,1/23/18,20
99 | 98,"""Services Vendor""","""some product13""",10.05,3/1/18,49
100 | 99,"""Home and Garden Vendor""","""a product12""",23.64,1/12/18,80
101 |
--------------------------------------------------------------------------------