0% found this document useful (0 votes)
36 views64 pages

Working With Expressions - Grouping and Summarizing Data - 16.05.20

Uploaded by

azraaltundas0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views64 pages

Working With Expressions - Grouping and Summarizing Data - 16.05.20

Uploaded by

azraaltundas0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 64

IST2142 – DATABASE

Week 14 – 16 May 2024

Case Study 3: Working with Expressions - Grouping


and Summarizing Data
www.ist.yildiz.edu.tr
Erhan CENE – Coskun PARIM
Outline of Case Study 3
• Example : FarmersMarketDatabase

ERHAN CENE – COSKUN PARIM 2


IST2142 – DATABASE WEEK 13 – 16.05.2024
Example: Farmers Market Database (FMD)

Please open FarmersMarketDatabase SQL file and run it. You can download it
from your lecturer’ avesis page.

ERHAN CENE – COSKUN PARIM 3


IST2142 – DATABASE WEEK 13 - 16.05.2024
Example: Farmers Market Database ERD

ERHAN CENE – COSKUN PARIM 4


IST2142 – DATABASE WEEK 13 - 16.05.2024
FMD - (Q1)

Q1)
Get a list of the customer IDs of customers who made purchases on each
market date: and sort your result by first the market date and then by the
customer ID.

You should use the customer_purchases table.


Compare the outputs of the SQL codes by
• Just using the SELECT and ORDER BY command.
• By using the SELECT DISTINCT and ORDER BY command.
• By using the SELECT, GROUP BY and ORDER BY command.

Sample Output

ERHAN CENE – COSKUN PARIM 5


IST2142 – DATABASE WEEK 13 - 16.05.2024
FMD - (A1)
SELECT DISTINCT market_date,
A1) customer_id
USE `farmers_market`; FROM customer_purchases
SELECT market_date, customer_id ORDER BY market_date,
FROM customer_purchases customer_id;
ORDER BY market_date, customer_id;

SELECT market_date, customer_id


FROM customer_purchases
GROUP BY market_date, customer_id
ORDER BY market_date, customer_id;
ERHAN CENE – COSKUN PARIM 6
IST2142 – DATABASE WEEK 13 - 16.05.2024
FMD - (Q2)

Q2)
Write an SQL code that extracts
number of purchases made by each
customer_id in each date.

Sort your result according to


market_date and customer_id.

You should use the


customer_purchases table.

ERHAN CENE – COSKUN PARIM 7


IST2142 – DATABASE WEEK 13 - 16.05.2024
FMD - (A2)

A2)
USE `farmers_market`;

SELECT market_date, customer_id,


COUNT(*) AS number_of_purchases
FROM customer_purchases
GROUP BY market_date, customer_id
ORDER BY market_date, customer_id
LIMIT 10;

ERHAN CENE – COSKUN PARIM 8


IST2142 – DATABASE WEEK 13 - 16.05.2024
FMD - (Q3)

Q3)
Write an SQL code that extracts the sum of
quantities and number of different
purchases made by each customer_id in
each date.

Sort your result according to market_date


and customer_id.

You should use the customer_purchases


table.

ERHAN CENE – COSKUN PARIM 9


IST2142 – DATABASE WEEK 13 - 16.05.2024
FMD - (A3)

A3)
USE `farmers_market`;

SELECT market_date, customer_id,


SUM(quantity) AS quantity_purchased,
COUNT(DISTINCT product_id)
AS different_products_purchased
FROM customer_purchases
GROUP BY market_date, customer_id
ORDER BY market_date, customer_id
LIMIT 10;

ERHAN CENE – COSKUN PARIM 10


IST2142 – DATABASE WEEK 13 - 16.05.2024
FMD - (Q4)

Q4)
Write an SQL code that extracts the price which
is calculated by multiplying quantity with
cost_to_customer_per_qty for the
customer_id=3.

Sort your result according to market_date and


vendor_id.

You should use the customer_purchases table.

ERHAN CENE – COSKUN PARIM 11


IST2142 – DATABASE WEEK 13 - 16.05.2024
FMD - (A4)

A4)
USE `farmers_market`;

SELECT market_date, customer_id, vendor_id,


quantity * cost_to_customer_per_qty
AS price
FROM customer_purchases
WHERE customer_id = 3
ORDER BY market_date, vendor_id;

ERHAN CENE – COSKUN PARIM 12


IST2142 – DATABASE WEEK 13 - 16.05.2024
FMD - (Q5)

Q5)
Write an SQL code that extracts the total
money spent in each date for the
customer_id=3.

Sort your result according to market_date.

You should use the customer_purchases table.

ERHAN CENE – COSKUN PARIM 13


IST2142 – DATABASE WEEK 13 - 16.05.2024
FMD - (A5)

A5)
USE `farmers_market`;

SELECT customer_id, market_date, SUM(quantity *


cost_to_customer_per_qty)
AS total_spent
FROM customer_purchases
WHERE customer_id = 3
GROUP BY market_date
ORDER BY market_date;

ERHAN CENE – COSKUN PARIM 14


IST2142 – DATABASE WEEK 13 - 16.05.2024
FMD - (Q6)

Q6)
Write an SQL code that extracts the total
money spent in each vendor id for the
customer_id=3.

Sort your result according to vendor id.

You should use the customer_purchases table.

ERHAN CENE – COSKUN PARIM 15


IST2142 – DATABASE WEEK 13 - 16.05.2024
FMD - (A6)

A6)
USE `farmers_market`;

SELECT customer_id, vendor_id,


SUM(quantity * cost_to_customer_per_qty)
AS total_spent
FROM customer_purchases
WHERE customer_id = 3
GROUP BY vendor_id
ORDER BY vendor_id;

ERHAN CENE – COSKUN PARIM 16


IST2142 – DATABASE WEEK 13 - 16.05.2024
FMD - (Q7)

Q7)
Write an SQL code that extracts the total
money spent by each customer.

Sort your result according to customer id.

You should use the customer_purchases table.

ERHAN CENE – COSKUN PARIM 17


IST2142 – DATABASE WEEK 13 - 16.05.2024
FMD - (A7)

A7)
USE `farmers_market`;

SELECT customer_id,
SUM(quantity * cost_to_customer_per_qty)
AS total_spent
FROM customer_purchases
GROUP BY customer_id
ORDER BY customer_id;

ERHAN CENE – COSKUN PARIM 18


IST2142 – DATABASE WEEK 13 - 16.05.2024
FMD - (Q8)

Q8)
Write an SQL code that extracts the customers
that spends more than 3000$.

Sort your result according to the total money


spend.

You should use the customer_purchases table.

ERHAN CENE – COSKUN PARIM 19


IST2142 – DATABASE WEEK 13 - 16.05.2024
FMD - (A8)

A8)
USE `farmers_market`;

SELECT customer_id,
SUM(quantity * cost_to_customer_per_qty)
AS total_spent
FROM customer_purchases
GROUP BY customer_id
HAVING total_spent >3000
ORDER BY total_spent DESC;

ERHAN CENE – COSKUN PARIM 20


IST2142 – DATABASE WEEK 13 - 16.05.2024
FMD - (Q9)

Q9)
Write an SQL code that extracts the total
money spent in each vendor id for the
customer_id 3, 4 and 5.

Sort your result according to customer id and


vendor id.

You should use the customer_purchases table.

ERHAN CENE – COSKUN PARIM 21


IST2142 – DATABASE WEEK 13 - 16.05.2024
FMD - (A9)
A9)
USE `farmers_market`;

SELECT customer_id, vendor_id,


SUM(quantity * cost_to_customer_per_qty)
AS total_spent
FROM customer_purchases
WHERE customer_id IN (3, 4, 5)
GROUP BY customer_id, vendor_id
ORDER BY customer_id,vendor_id;

SELECT customer_id, vendor_id,


SUM(quantity * cost_to_customer_per_qty)
AS total_spent
FROM customer_purchases
GROUP BY customer_id, vendor_id
HAVING customer_id IN (3,4,5)
ORDER BY customer_id,vendor_id;

ERHAN CENE – COSKUN PARIM 22


IST2142 – DATABASE WEEK 13 - 16.05.2024
FMD - (Q10)

Q10)
Write an SQL code that extracts the total
money spent in each vendor id for the
customer_id 1,2, 3, 4 and 5.

But this time limit the results where total spent


is greater than 1000$.

Sort your result according to customer id and


vendor id.

You should use the customer_purchases table.


ERHAN CENE – COSKUN PARIM 23
IST2142 – DATABASE WEEK 13 - 16.05.2024
FMD - (A10)
A10)
USE `farmers_market`;

SELECT customer_id, vendor_id,


SUM(quantity * cost_to_customer_per_qty) Error Code: 1054. Unknown column
AS total_spent
FROM customer_purchases 'total_spent' in 'where clause'
WHERE total_spent >1000
AND customer_id IN (1,2,3,4,5)
GROUP BY customer_id, vendor_id
ORDER BY customer_id,vendor_id;

SELECT customer_id, vendor_id,


SUM(quantity * cost_to_customer_per_qty)
AS total_spent
FROM customer_purchases
GROUP BY customer_id, vendor_id
HAVING total_spent >1000
AND customer_id IN (1,2,3,4,5)
ORDER BY customer_id,vendor_id;

ERHAN CENE – COSKUN PARIM 24


IST2142 – DATABASE WEEK 13 - 16.05.2024
FMD - (Q11)

Q11)
Write an SQL code that extracts the vendor
inventory table sorted by original price in
increasing order.

ERHAN CENE – COSKUN PARIM 25


IST2142 – DATABASE WEEK 13 - 16.05.2024
FMD - (A11)

A11)
USE `farmers_market`;
SELECT *FROM vendor_inventory
ORDER BY original_price
LIMIT 10;

ERHAN CENE – COSKUN PARIM 26


IST2142 – DATABASE WEEK 13 - 16.05.2024
FMD - (Q12)

Q12)
Write an SQL code that extracts the minimum
and the maximum price in the vendor inventory
table.

ERHAN CENE – COSKUN PARIM 27


IST2142 – DATABASE WEEK 13 - 16.05.2024
FMD - (A12)

A12)
USE `farmers_market`;

SELECT MIN(original_price)
AS minimum_price,
MAX(original_price)
AS maximum_price
FROM vendor_inventory;

ERHAN CENE – COSKUN PARIM 28


IST2142 – DATABASE WEEK 13 - 16.05.2024
FMD - (Q13)

Q13)
Write an SQL code that extracts the product
count in each day.
Sort the results by date.

You should use the vendor inventory table

ERHAN CENE – COSKUN PARIM 29


IST2142 – DATABASE WEEK 13 - 16.05.2024
FMD - (A13)

A13)
USE `farmers_market`;

SELECT market_date,
COUNT(product_id) AS product_count
FROM vendor_inventory
GROUP BY market_date
ORDER BY market_date;

ERHAN CENE – COSKUN PARIM 30


IST2142 – DATABASE WEEK 13 - 16.05.2024
FMD - (Q14)

Q14)
Write an SQL code that extracts number of
different products offered and average product
price for each vendor id between the dates
2019-07-03 and 2019-07-17.

Sort the results by vendor id.

You should use the vendor inventory table.

ERHAN CENE – COSKUN PARIM 31


IST2142 – DATABASE WEEK 13 - 16.05.2024
FMD - (A14)
A14)
USE `farmers_market`;

SELECT vendor_id,
COUNT(DISTINCT product_id)
AS different_products_offered,
AVG(original_price)
AS average_product_price
FROM vendor_inventory
WHERE market_date
BETWEEN '2019-07-03' AND '2019-07-17’
GROUP BY vendor_id
ORDER BY vendor_id;

ERHAN CENE – COSKUN PARIM 32


IST2142 – DATABASE WEEK 13 - 16.05.2024
FMD - (Q15)

Q15)
Write an SQL code that extracts
• number of different products offered,
• value of inventory which is defined as sum of quantity times original price,
• inventory item count which is defined as sum of quantities,
• average item price which is defined as value of inventory divided by inventory item count, and
• average product price
for each vendor id between the dates 2019-07-03 and 2019-07-17.

Sort the results by vendor id.


You should use the vendor inventory table.

ERHAN CENE – COSKUN PARIM 33


IST2142 – DATABASE WEEK 13 - 16.05.2024
FMD - (A15)
A15)
USE `farmers_market`;

SELECT vendor_id, COUNT(DISTINCT product_id) AS different_products_offered,


SUM(quantity * original_price) AS value_of_inventory,
SUM(quantity) AS inventory_item_count,
ROUND(SUM(quantity * original_price) / SUM(quantity), 2) AS average_item_price,
AVG(original_price) AS average_product_price
FROM vendor_inventory
WHERE market_date BETWEEN '2019-07-03' AND '2019-07-17’
GROUP BY vendor_id
ORDER BY vendor_id;

ERHAN CENE – COSKUN PARIM 34


IST2142 – DATABASE WEEK 13 - 16.05.2024
FMD - (Q16)
Q16)
Write an SQL code that extracts
• number of different products offered,
• value of inventory which is defined as sum of quantity times original price,
• inventory item count which is defined as sum of quantities,
• average item price which is defined as value of inventory divided by inventory item count, and
• average product price
for each vendor id between the dates 2019-07-03 and 2019-07-17 where the inventory item count is greater
than 700.

Sort the results by vendor id.


You should use the vendor inventory table.

ERHAN CENE – COSKUN PARIM 35


IST2142 – DATABASE WEEK 13 - 16.05.2024
FMD - (A16)
A16)
USE `farmers_market`;

SELECT vendor_id, COUNT(DISTINCT product_id) AS different_products_offered,


SUM(quantity * original_price) AS value_of_inventory,
SUM(quantity) AS inventory_item_count,
ROUND(SUM(quantity * original_price) / SUM(quantity), 2) AS average_item_price,
AVG(original_price) AS average_product_price
FROM vendor_inventory
WHERE market_date BETWEEN '2019-07-03' AND '2019-07-17’
GROUP BY vendor_id
HAVING inventory_item_count >= 700
ORDER BY vendor_id;

ERHAN CENE – COSKUN PARIM 36


IST2142 – DATABASE WEEK 13 - 16.05.2024
FMD - (Q17)

Q17)
Write an SQL code that finds minimum and
maximum sale date for each vendor id.

Sort the result by min date first, and max date


second.

You should use the vendor inventory table.

ERHAN CENE – COSKUN PARIM 37


IST2142 – DATABASE WEEK 13 - 16.05.2024
FMD - (A17)

A17)
USE `farmers_market`;

SELECT vendor_id,
min(market_date),
max(market_date)
FROM vendor_inventory
GROUP BY vendor_id
ORDER BY min(market_date), max(market_date);

ERHAN CENE – COSKUN PARIM 38


IST2142 – DATABASE WEEK 13 - 16.05.2024
FMD - (Q18)

Q18)
Write an SQL code that finds number of vendors with
inventory in each month of the database.

You should use the EXTRACT command to extract year


and month from the market_date.

Sort the result first by year and then by month.

You should use the vendor inventory table.

ERHAN CENE – COSKUN PARIM 39


IST2142 – DATABASE WEEK 13 - 16.05.2024
FMD - (A18)
A18)
USE `farmers_market`;

SELECT COUNT(DISTINCT vendor_id)


AS vendors_with_inventory
FROM vendor_inventory;

SELECT EXTRACT(YEAR FROM market_date)


AS market_year,
EXTRACT(MONTH FROM market_date)
AS market_month,
COUNT(DISTINCT vendor_id)
AS vendors_with_inventory
FROM vendor_inventory
GROUP BY EXTRACT(YEAR FROM market_date),
EXTRACT(MONTH FROM market_date)
ORDER BY EXTRACT(YEAR FROM market_date),
EXTRACT(MONTH FROM market_date);
ERHAN CENE – COSKUN PARIM 40
IST2142 – DATABASE WEEK 13 - 16.05.2024
FMD - (Q19)

Q19)
Write an SQL code that finds how many
products are there inside each category.

For this, you should first calculate how


many product_id exists for each
product_category_id and then join product
and product category tables to obtain the
product_category_name.

ERHAN CENE – COSKUN PARIM 41


IST2142 – DATABASE WEEK 13 - 16.05.2024
FMD - (A19)

A19)
USE `farmers_market`;

SELECT DISTINCT product_category_id


FROM product;

USE `farmers_market`;

SELECT DISTINCT product_category_id


FROM product_category;

ERHAN CENE – COSKUN PARIM 42


IST2142 – DATABASE WEEK 13 - 16.05.2024
FMD - (A19)

A19)
USE `farmers_market`;

SELECT pc.product_category_id,
pc.product_category_name,
count(product_id) AS count_of_products
FROM product_category AS pc
LEFT JOIN product AS p
ON pc.product_category_id = p.product_category_id
GROUP BY pc.product_category_id;

ERHAN CENE – COSKUN PARIM 43


IST2142 – DATABASE WEEK 13 - 16.05.2024
FMD - (A19)

A19)
USE `farmers_market`;

SELECT pc.product_category_id,
pc.product_category_name,
count(product_id) AS count_of_products
FROM product_category AS pc
INNER JOIN product AS p
ON pc.product_category_id = p.product_category_id
GROUP BY pc.product_category_id;

ERHAN CENE – COSKUN PARIM 44


IST2142 – DATABASE WEEK 13 - 16.05.2024
FMD - (A19)

A19)
USE `farmers_market`;

SELECT pc.product_category_id,
pc.product_category_name,
count(product_id) AS count_of_products
FROM product_category AS pc
RIGHT JOIN product AS p
ON pc.product_category_id = p.product_category_id
GROUP BY pc.product_category_id;

ERHAN CENE – COSKUN PARIM 45


IST2142 – DATABASE WEEK 13 - 16.05.2024
FMD - (Q20)

Q20)
Write an SQL code that finds product id,
product name, minimum price and
maximum price for each product inside the
vendor inventory table.

You should calculate minimum and


maximum price from the vendor_inventory
table and take product name from product
table and join both tables with product id.

ERHAN CENE – COSKUN PARIM 46


IST2142 – DATABASE WEEK 13 - 16.05.2024
FMD - (A20)

A20)
USE `farmers_market`;

SELECT p.product_id, p.product_name,


MIN(vi.original_price) AS minimum_price,
MAX(vi.original_price) AS maximum_price FROM
vendor_inventory AS vi
INNER JOIN product AS p
ON vi.product_id = p.product_id
GROUP BY p.product_id, p.product_name;

ERHAN CENE – COSKUN PARIM 47


IST2142 – DATABASE WEEK 13 - 16.05.2024
FMD - (A20)

A20)
USE `farmers_market`;

SELECT p.product_id, p.product_name,


MIN(vi.original_price) AS minimum_price,
MAX(vi.original_price) AS maximum_price FROM
vendor_inventory AS vi
LEFT JOIN product AS p
ON vi.product_id = p.product_id
GROUP BY p.product_id, p.product_name;

ERHAN CENE – COSKUN PARIM 48


IST2142 – DATABASE WEEK 13 - 16.05.2024
FMD - (A20)

A20)
USE `farmers_market`;

SELECT p.product_id, p.product_name,


MIN(vi.original_price) AS minimum_price,
MAX(vi.original_price) AS maximum_price FROM
vendor_inventory AS vi
RIGHT JOIN product AS p
ON vi.product_id = p.product_id
GROUP BY p.product_id, p.product_name;

ERHAN CENE – COSKUN PARIM 49


IST2142 – DATABASE WEEK 13 - 16.05.2024
FMD - (Q21)
Q21)
Write an SQL code that finds product category id, First you should join vendor_inventory and product tables
product category name, minimum price and with product id.
maximum price for each product category.
Then you should use a second join on the first one by
You should calculate minimum and maximum using product_category_id from the product_category
price from the vendor_inventory table and select table
product category id and product category name
from category table.

ERHAN CENE – COSKUN PARIM 50


IST2142 – DATABASE WEEK 13 - 16.05.2024
FMD - (Q21)
A21)
USE `farmers_market`;
SELECT pc.product_category_id, pc.product_category_name,
MIN(vi.original_price) AS minimum_price,
MAX(vi.original_price) AS maximum_price
FROM vendor_inventory AS vi
INNER JOIN product AS p
ON vi.product_id = p.product_id
INNER JOIN product_category AS pc
ON p.product_category_id = pc.product_category_id
GROUP BY pc.product_category_id, pc.product_category_name;

ERHAN CENE – COSKUN PARIM 51


IST2142 – DATABASE WEEK 13 - 16.05.2024
FMD - (Q22)
Q22) price is defined as
Write an SQL code that will provide quantity * cost_to_customer_per_qty
the following output.
Note that table only contains values for
You should use customer, customer_id = 3 and sorted with vendor_id.
customer_purchases and vendor
tables.

ERHAN CENE – COSKUN PARIM 52


IST2142 – DATABASE WEEK 13 - 16.05.2024
FMD - (A22)
A22)
USE `farmers_market`;
SELECT c.customer_first_name, c.customer_last_name,
c.customer_id, v.vendor_name, v.vendor_id,
cp.quantity * cp.cost_to_customer_per_qty AS price
FROM customer AS c
LEFT JOIN customer_purchases AS cp
ON c.customer_id = cp.customer_id
LEFT JOIN vendor as v
ON cp.vendor_id = v.vendor_id
WHERE cp.customer_id = 3
ORDER BY vendor_id;

ERHAN CENE – COSKUN PARIM 53


IST2142 – DATABASE WEEK 13 - 16.05.2024
FMD - (Q23)
Q23) total_spent is defined as
Write an SQL code that will provide the following sum of quantity * cost_to_customer_per_qty
output.
Note that table only contains values for
You should use customer, customer_purchases customer_id = 1,2,3,4 and 5, grouped by customer id and
and vendor tables. vendor_id and sorted with customer id and vendor_id.

ERHAN CENE – COSKUN PARIM 54


IST2142 – DATABASE WEEK 13 - 16.05.2024
FMD - (A23)
A23)
USE `farmers_market`;
SELECT c.customer_first_name,
c.customer_last_name,
c.customer_id, v.vendor_name, v.vendor_id,
SUM(cp.quantity * cp.cost_to_customer_per_qty) AS total_spent
FROM customer AS c
LEFT JOIN customer_purchases AS cp
ON c.customer_id = cp.customer_id
LEFT JOIN vendor AS v
ON cp.vendor_id = v.vendor_id
WHERE cp.customer_id IN (1,2,3,4,5)
GROUP BY c.customer_id, v.vendor_id
ORDER BY c.customer_id, v.vendor_id;

ERHAN CENE – COSKUN PARIM 55


IST2142 – DATABASE WEEK 13 - 16.05.2024
FMD - (Q24)
Q24)

Use LEFT JOIN to return Customer_id,


customer_first_name, customer_last_name and
average_spend_by_customer.

price is defined as
quantity*cost_to_customer_per_qty
and average_spend_by_customer is defined as
average price.

You should use customer_purchases and


customer table.

ERHAN CENE – COSKUN PARIM 56


IST2142 – DATABASE WEEK 13 - 16.05.2024
FMD - (A24)
A24)

SELECT c.customer_id,
c.customer_first_name,
c.customer_last_name,
AVG(cp.quantity*cp.cost_to_customer_per_qty) AS
average_spend_by_customer
FROM customer AS c
LEFT JOIN customer_purchases AS cp
ON c.customer_id = cp.customer_id
GROUP BY c.customer_id, c.customer_first_name,
c.customer_last_name;

ERHAN CENE – COSKUN PARIM 57


IST2142 – DATABASE WEEK 13 - 16.05.2024
FMD - (Q25)
Q25)

Use RIGHT JOIN to return Customer_id,


customer_first_name, customer_last_name and
average_spend_by_customer.

price is defined as
quantity*cost_to_customer_per_qty
and average_spend_by_customer is defined as
average price.

You should use customer_purchases and


customer table.

ERHAN CENE – COSKUN PARIM 58


IST2142 – DATABASE WEEK 13 - 16.05.2024
FMD - (A25)
A25)

SELECT c.customer_id,
c.customer_first_name,
c.customer_last_name,
AVG(cp.quantity*cp.cost_to_customer_per_qty) AS
average_spend_by_customer
FROM customer_purchases AS cp
RIGHT JOIN customer AS c
ON cp.customer_id = c.customer_id
GROUP BY c.customer_id, c.customer_first_name,
c.customer_last_name;

ERHAN CENE – COSKUN PARIM 59


IST2142 – DATABASE WEEK 13 - 16.05.2024
FMD - (Q26)
Q26)

Creating Data for Time Series:


Market Date vs Daily Sales

ERHAN CENE – COSKUN PARIM 60


IST2142 – DATABASE WEEK 13 - 16.05.2024
FMD - (A26)
A26)

USE `farmers_market`;
SELECT market_date,
ROUND(SUM(quantity * cost_to_customer_per_qty),2)
AS total_sales
FROM customer_purchases
GROUP BY market_date
ORDER BY market_date;

ERHAN CENE – COSKUN PARIM 61


IST2142 – DATABASE WEEK 13 - 16.05.2024
FMD - (Q27)
Q27)

Creating Data for Time Series:


First Market Date of the Week vs Weekly Sales

ERHAN CENE – COSKUN PARIM 62


IST2142 – DATABASE WEEK 13 - 16.05.2024
FMD - (A27)
A27)

SELECT MIN(cp.market_date)
AS first_market_date_of_week,
ROUND(SUM(cp.quantity * cp.cost_to_customer_per_qty),2)
AS weekly_sales
FROM farmers_market.customer_purchases AS cp
LEFT JOIN market_date_info AS md
ON cp.market_date = md.market_date
GROUP BY md.market_year, md.market_week
ORDER BY md.market_year, md.market_week;

ERHAN CENE – COSKUN PARIM 63


IST2142 – DATABASE WEEK 13 - 16.05.2024
FMD - (A27)
A27 – ALTERNATE SOLUTION)

SELECT MIN(market_date) AS first_market_date_of_week,


EXTRACT(YEAR FROM market_date) AS year_no,
EXTRACT(WEEK FROM market_date) AS week_no, ROUND(SUM(quantity
* cost_to_customer_per_qty),2)
AS weekly_sales
FROM customer_purchases
GROUP BY year_no, week_no
ORDER BY year_no, week_no;

ERHAN CENE – COSKUN PARIM 64


IST2142 – DATABASE WEEK 13 - 16.05.2024

You might also like