1.Laboratoriya ishi
Mavzu: Agregat funksiyalardan foydalanib so’rovlar yaratish.
Ishdan maqsad: Agregat funksiyailardan foydalanish bo`yicha ko`nikmaga ega bo`lish.
Masalani qo`yilishi: Berilgan predmet soha ma`lumotlar bazasidagi barcha ob`yektlarni ustida agregat funksiyalardan foydalanib amallarini bajarish.
Uslubiy ko`rsatmalar: oldingi darsda siz ma'lumotlar bilan ishlashga imkon beradigan bir qator foydali MySQL funktsiyalari haqida ma'lumotga ega bo'lishingiz mumkin. Ushbu darsda yana bir funktsiyalar to'plami ko'rib chiqiladi - ular agregat funksiyalari deyiladi: COUNT, SUM, MIN, MAX va AVG.
SQL agregat funksiyalari ustunlik qiymatlariga bitta natija qiymatini olish uchun qo'llaniladi. Sodda qilib aytganda, ushbu funktsiyalardan foydalanib, ustun qiymatlari to'plamidan bitta qiymatni olamiz. Bu bo'lishi mumkin: barcha qiymatlarning yig'indisi, minimal, maksimal yoki o'rtacha qiymat, shuningdek jadvalning qatorlarini hisoblash.
mysql> CREATE DATABASE AGREGAT;
Query OK, 1 row affected (0.14 sec)
mysql> USE AGREGAT;
Database changed
mysql> CREATE TABLE Products
(
Id INT AUTO_INCREMENT PRIMARY KEY,
ProductName VARCHAR(30) NOT NULL,
Manufacturer VARCHAR(20) NOT NULL,
ProductCount INT DEFAULT 0,
Price DECIMAL NOT NULL
);
Query OK, 0 rows affected (0.46 sec)
mysql>
mysql> INSERT INTO Products(ProductName, Manufacturer, ProductCount, Price)
VALUES
('iPhone X', 'Apple', 3, 76000),
('iPhone 8', 'Apple', 2, 51000),
('iPhone 7', 'Apple', 5, 32000),
('Galaxy S9', 'Samsung', 2, 56000),
('Galaxy S8', 'Samsung', 1, 46000),
('Honor 10', 'Huawei', 5, 28000),
('Nokia 8', 'HMD Global', 6, 38000);
Query OK, 7 rows affected (0.17 sec)
Records: 7 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM PRODUCTS;
+----+-------------+--------------+--------------+-------+
| Id | ProductName | Manufacturer | ProductCount | Price |
+----+-------------+--------------+--------------+-------+
| 1 | iPhone X | Apple | 3 | 76000 |
| 2 | iPhone 8 | Apple | 2 | 51000 |
| 3 | iPhone 7 | Apple | 5 | 32000 |
| 4 | Galaxy S9 | Samsung | 2 | 56000 |
| 5 | Galaxy S8 | Samsung | 1 | 46000 |
| 6 | Honor 10 | Huawei | 5 | 28000 |
| 7 | Nokia 8 | HMD Global | 6 | 38000 |
+----+-------------+--------------+--------------+-------+
7 rows in set (0.00 sec)
mysql> SELECT AVG(Price) AS Average_Price FROM Products;
+---------------+
| Average_Price |
+---------------+
| 46714.2857 |
+---------------+
1 row in set (0.10 sec)
mysql> SELECT AVG(Price) FROM Products
WHERE Manufacturer='Apple';
+------------+
| AVG(Price) |
+------------+
| 53000.0000 |
+------------+
1 row in set (0.00 sec)
mysql> SELECT AVG(Price * ProductCount) FROM Products
;
+---------------------------+
| AVG(Price * ProductCount) |
+---------------------------+
| 145142.8571 |
+---------------------------+
1 row in set (0.00 sec)
mysql> SELECT AVG(Price * ProductCount) AS JAMI FROM Products
;
+-------------+
| JAMI |
+-------------+
| 145142.8571 |
+-------------+
1 row in set (0.00 sec)
mysql>
mysql> SELECT COUNT(*) FROM Products;
+----------+
| COUNT(*) |
+----------+
| 7 |
+----------+
1 row in set (0.13 sec)
mysql>
mysql> SELECT COUNT(Manufacturer) FROM Products;
+---------------------+
| COUNT(Manufacturer) |
+---------------------+
| 7 |
+---------------------+
1 row in set (0.00 sec)
mysql> SELECT MIN(Price), MAX(Price) FROM Products;
+------------+------------+
| MIN(Price) | MAX(Price) |
+------------+------------+
| 28000 | 76000 |
+------------+------------+
1 row in set (0.05 sec)
mysql> SELECT SUM(ProductCount) FROM Products;
+-------------------+
| SUM(ProductCount) |
+-------------------+
| 24 |
+-------------------+
1 row in set (0.00 sec)
mysql> SELECT SUM(ProductCount * Price) FROM Products
;
+---------------------------+
| SUM(ProductCount * Price) |
+---------------------------+
| 1016000 |
+---------------------------+
1 row in set (0.00 sec)
mysql> SELECT COUNT(DISTINCT Manufacturer) FROM Products;
+------------------------------+
| COUNT(DISTINCT Manufacturer) |
+------------------------------+
| 4 |
+------------------------------+
1 row in set (0.05 sec)
mysql> SELECT COUNT(ALL Manufacturer) FROM Products;
+-------------------------+
| COUNT(ALL Manufacturer) |
+-------------------------+
| 7 |
+-------------------------+
1 row in set (0.00 sec)
mysql> SELECT COUNT(*) AS ProdCount,
SUM(ProductCount) AS TotalCount,
MIN(Price) AS MinPrice,
MAX(Price) AS MaxPrice,
AVG(Price) AS AvgPrice
FROM Products;
+-----------+------------+----------+----------+------------+
| ProdCount | TotalCount | MinPrice | MaxPrice | AvgPrice |
+-----------+------------+----------+----------+------------+
| 7 | 24 | 28000 | 76000 | 46714.2857 |
+-----------+------------+----------+----------+------------+
1 row in set (0.00 sec)
mysql> SELECT GROUP_CONCAT(Price) FROM Products;
+-------------------------------------------+
| GROUP_CONCAT(Price) |
+-------------------------------------------+
| 76000,51000,32000,56000,46000,28000,38000 |
+-------------------------------------------+
1 row in set (0.00 sec)
mysql> SELECT GROUP_CONCAT(PRODUCTNAME) FROM Products;
+-----------------------------------------------------------------+
| GROUP_CONCAT(PRODUCTNAME) |
+-----------------------------------------------------------------+
| iPhone X,iPhone 8,iPhone 7,Galaxy S9,Galaxy S8,Honor 10,Nokia 8 |
+-----------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> SELECT STD(Price) FROM Products;
+--------------------+
| STD(Price) |
+--------------------+
| 15144.204791784552 |
+--------------------+
1 row in set (0.00 sec)
mysql> SELECT STDDEV(Price) FROM Products;
+--------------------+
| STDDEV(Price) |
+--------------------+
| 15144.204791784552 |
+--------------------+
1 row in set (0.00 sec)
mysql> SELECT VAR_POP(Price) FROM Products;
+--------------------+
| VAR_POP(Price) |
+--------------------+
| 229346938.77551016 |
+--------------------+
1 row in set (0.00 sec)
Topshiriq: Har bir talaba o’ziga berilgan predmet soha mavzusi bo’yicha AVG,SUM, MIN, MAX, GROUP_CONCAT operatoridan foydalanib so’rovlar yaratishi va natijasi bilan ko’rsatib o’tishi kerak.
Do'stlaringiz bilan baham: |