Skip to content

Instantly share code, notes, and snippets.

@SlavikArt
Created January 22, 2024 10:14
Show Gist options
  • Save SlavikArt/33980fc7bb3e2b6291eeb78792795e19 to your computer and use it in GitHub Desktop.
Save SlavikArt/33980fc7bb3e2b6291eeb78792795e19 to your computer and use it in GitHub Desktop.

Aggregation querries (on Product)

Aggregation

-- 1. Показать товары, средняя цена продажи которых
-- была больше 50 гривен
SELECT
Product.name AS 'product',
AVG(Sale.price) AS 'avg_sell_price'
FROM Product
JOIN Sale ON Product.id = Sale.id_product
GROUP BY Product.name
HAVING AVG(Sale.price) > 50
-- 2. Вывести количество товаров каждой категории,
-- средняя цена поставки которых больше 100 гривен
SELECT
Category.name AS 'category',
COUNT(Product.name) AS 'quantity'
FROM Product
JOIN Category ON Category.id = Product.id_category
JOIN Delivery ON Product.id = Delivery.id_product
GROUP BY Category.name
HAVING AVG(Delivery.price) > 100
-- 3. Показать информацию о наименьшей стоимости поставки товаров
-- за последний месяц для каждого поставщика.
-- Отсортировать стоимость по возрастанию
SELECT
Supplier.name AS 'supplier',
MIN(Delivery.price) AS 'min_delivery_price'
FROM Product
JOIN Delivery ON Product.id = Delivery.id_product
JOIN Supplier ON Supplier.id = Delivery.id_supplier
WHERE
Delivery.date_of_delivery BETWEEN
DATEADD(MONTH, -1, GETDATE()) AND GETDATE()
GROUP BY Supplier.name
ORDER BY MIN(Delivery.price)
-- 4. Показать информацию о производителе,
-- его полном адресе, и количестве товаров, которые он выпускает.
-- Общая стоимость продаж товаров каждого производителя
-- при этом должна быть от 500 до 20000 гривен
SELECT
Producer.name AS 'producer',
(Country.name + ', ' + Region.name + ', '
+ City.name + ', ' + Address.street) AS 'full address',
COUNT(Product.name) AS 'product_quantity',
SUM(Sale.price) AS 'total_sale_price'
FROM Product
JOIN Producer ON Producer.id = Product.id_producer
JOIN Address ON Address.id = Producer.id_address
JOIN City ON City.id = Address.id_city
JOIN Region ON Region.id = City.id_region
JOIN Country ON Country.id = Region.id_country
JOIN Sale ON Product.id = Sale.id_product
GROUP BY
Producer.name,
(Country.name + ', ' + Region.name + ', '
+ City.name + ', ' + Address.street)
HAVING SUM(Sale.price) BETWEEN 500 AND 20000
-- 5. Показать количество товаров каждой категории,
-- при этом учитывая только те товары,
-- стоимость поставки которых превышала 400 гривен.
-- Условие: выведенная информация касается только трёх определённых поставщиков
SELECT
Category.name AS 'category',
COUNT(Product.name) AS 'quantity',
Supplier.name AS 'supplier',
Delivery.price AS 'delivery_price'
FROM Product
JOIN Category ON Category.id = Product.id_category
JOIN Delivery ON Product.id = Delivery.id_product
JOIN Supplier ON Supplier.id = Delivery.id_supplier
WHERE Supplier.name IN ('OOO Panky', 'eBay', 'OOO Kakie Ludi')
GROUP BY Category.name, Supplier.name, Delivery.price
HAVING SUM(Delivery.price) > 400
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment