91 lines
3.3 KiB
Plaintext
91 lines
3.3 KiB
Plaintext
--Q1
|
|
SELECT items.item_id AS '商品ID',
|
|
items.name AS '商品名',
|
|
items.price AS '価格',
|
|
item_category.name AS 'カテゴリ名'
|
|
FROM items INNER JOIN item_category ON items.category_id = item_category.category_id
|
|
ORDER BY items.item_id ASC;
|
|
|
|
--Q2
|
|
SELECT orders.order_id AS '注文ID',
|
|
orders.order_time AS '注文日時',
|
|
users.name AS 'ユーザ名',
|
|
items.name AS '商品名',
|
|
items.price AS '価格',
|
|
order_items.quantity AS '数量'
|
|
FROM orders
|
|
INNER JOIN order_items ON orders.order_id = order_items.order_id
|
|
INNER JOIN users ON orders.user_id = users.user_id
|
|
INNER JOIN items ON order_items.item_id = items.item_id
|
|
ORDER BY orders.order_id ASC;
|
|
|
|
--Q3
|
|
SELECT orders.order_id AS '注文ID',
|
|
orders.order_time AS '注文日時',
|
|
users.name AS 'ユーザ名',
|
|
SUM(items.price*order_items.quantity) AS '合計額'
|
|
FROM orders
|
|
INNER JOIN order_items ON orders.order_id = order_items.order_id
|
|
INNER JOIN users ON orders.user_id = users.user_id
|
|
INNER JOIN items ON order_items.item_id = items.item_id
|
|
GROUP BY orders.order_id
|
|
ORDER BY orders.order_id ASC;
|
|
|
|
--Q4
|
|
SELECT SUBSTRING(orders.order_time, 1, 7) AS '注文月',
|
|
SUM(order_items.quantity) AS '注文数'
|
|
FROM orders
|
|
INNER JOIN order_items ON orders.order_id = order_items.order_id
|
|
GROUP BY SUBSTRING(orders.order_time, 1, 7);
|
|
|
|
--Q5
|
|
SELECT SUBSTRING(orders.order_time, 1, 4) AS '注文年',
|
|
SUM(order_items.quantity*items.price) AS '売上額'
|
|
FROM orders
|
|
INNER JOIN order_items ON orders.order_id = order_items.order_id
|
|
INNER JOIN items ON order_items.item_id = items.item_id
|
|
GROUP BY SUBSTRING(orders.order_time, 1, 4)
|
|
ORDER BY SUBSTRING(orders.order_time, 1, 4) ASC;
|
|
|
|
--Q6
|
|
SELECT SUM(order_items.quantity*items.price) AS '売上額'
|
|
FROM order_items INNER JOIN items ON order_items.item_id = items.item_id;
|
|
|
|
--Q7
|
|
SELECT item_category.name AS 'カテゴリ名',
|
|
SUM(order_items.quantity*items.price) AS '売上額'
|
|
FROM order_items
|
|
INNER JOIN items ON items.item_id = order_items.item_id
|
|
INNER JOIN item_category ON item_category.category_id = items.category_id
|
|
GROUP BY item_category.category_id
|
|
ORDER BY 売上額 DESC;
|
|
|
|
--Q8
|
|
SELECT items.name AS '商品名',
|
|
SUM(order_items.quantity*items.price) AS '売上額'
|
|
FROM order_items
|
|
INNER JOIN items ON items.item_id = order_items.item_id
|
|
GROUP BY items.name
|
|
ORDER BY 売上額 DESC;
|
|
|
|
--Q9
|
|
SELECT item_category.name AS 'カテゴリ名',
|
|
SUM( 100*order_items.quantity*items.price/
|
|
(SELECT SUM(order_items.quantity*items.price)
|
|
FROM order_items INNER JOIN items ON order_items.item_id = items.item_id) ) AS '構成比(%)'
|
|
FROM order_items
|
|
INNER JOIN items ON items.item_id = order_items.item_id
|
|
INNER JOIN item_category ON item_category.category_id = items.category_id
|
|
GROUP BY item_category.category_id
|
|
ORDER BY 構成比(%) DESC;
|
|
|
|
--Q10
|
|
SELECT items.name AS '商品名',
|
|
SUM( 100*order_items.quantity*items.price/
|
|
(SELECT SUM(order_items.quantity*items.price)
|
|
FROM order_items INNER JOIN items ON order_items.item_id = items.item_id) ) AS '構成比(%)'
|
|
FROM order_items
|
|
INNER JOIN items ON items.item_id = order_items.item_id
|
|
GROUP BY items.name
|
|
ORDER BY 構成比(%) DESC;
|