Skip to content

Instantly share code, notes, and snippets.

@ludflu
Last active September 10, 2020 15:02
Show Gist options
  • Save ludflu/95976c2ef10e9cf624cf1b9f6aecb7ef to your computer and use it in GitHub Desktop.
Save ludflu/95976c2ef10e9cf624cf1b9f6aecb7ef to your computer and use it in GitHub Desktop.
--- for sqlfiddle go to http://sqlfiddle.com/#!17
create table customer(
id serial primary key,
name varchar(256)
);
create table ServiceOrder(
id serial primary key,
description varchar(256),
customer_id INTEGER REFERENCES customer(id),
timestamp TIMESTAMP
);
create table OrderLine(
id serial primary key,
part_number INTEGER,
description varchar(256),
quantity INTEGER,
service_order INTEGER REFERENCES ServiceOrder(id)
);
insert into customer (name) values ('Jim');
insert into customer (name) values ('Susan');
insert into customer (name) values ('John');
insert into customer (name) values ('Jaime');
insert into customer (name) values ('Sylvia');
insert into ServiceOrder (description, customer_id, timestamp) values ('widget order', 1, TO_TIMESTAMP('2019-01-01','YYYY-MM-DD'));
insert into ServiceOrder (description, customer_id, timestamp) values ('widget order', 2, TO_TIMESTAMP('2019-01-01','YYYY-MM-DD'));
insert into ServiceOrder (description, customer_id, timestamp) values ('widget order', 2, TO_TIMESTAMP('2019-01-02','YYYY-MM-DD'));
insert into ServiceOrder (description, customer_id, timestamp) values ('widget order', 2, TO_TIMESTAMP('2019-01-03','YYYY-MM-DD'));
insert into ServiceOrder (description, customer_id, timestamp) values ('widget order', 3, TO_TIMESTAMP('2019-01-01','YYYY-MM-DD'));
insert into ServiceOrder (description, customer_id, timestamp) values ('widget order', 4, TO_TIMESTAMP('2019-01-01','YYYY-MM-DD'));
insert into ServiceOrder (description, customer_id, timestamp) values ('widget order', 5, TO_TIMESTAMP('2019-01-01','YYYY-MM-DD'));
insert into OrderLine (part_number,description,quantity, service_order) values (123,'widget',45,1);
insert into OrderLine (part_number,description,quantity, service_order) values (123,'widget',1,2);
insert into OrderLine (part_number,description,quantity, service_order) values (123,'widget',5,3);
insert into OrderLine (part_number,description,quantity, service_order) values (123,'widget',2, 4);
insert into OrderLine (part_number,description,quantity, service_order) values (123,'widget',10, 5);
insert into OrderLine (part_number,description,quantity, service_order) values (123,'widget',11, 6);
insert into OrderLine (part_number,description,quantity, service_order) values (123,'widget',12,7);
--questions:
--who ordered the most stuff?
SELECT c.name, sum(ol.quantity) as qty
FROM customer c
JOIN serviceorder so on c.id = so.customer_id
JOIN orderline ol on so.id = ol.service_order
group by c.name order by qty;
--who placed the most orders ?
SELECT c.name, count(ol.quantity) as order_count
FROM customer c
JOIN serviceorder so on c.id = so.customer_id
JOIN orderline ol on so.id = ol.service_order
group by c.name order by order_count;
--who ordered most recently?
SELECT c.name, so.timestamp as order_date
FROM customer c
JOIN serviceorder so on c.id = so.customer_id
order by order_date;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment