MySQL-এ প্রোডাক্ট টেবিলে কোয়ান্টিটি এবং প্রাইস গুণ করে সব প্রোডাক্টের টোটাল ভ্যালু বের করতে SUM() এগরিগেট ফাংশন ব্যবহার করা হয়।
টেবিল তৈরি এবং ডেটা ইনসার্ট
mysql> CREATE TABLE DemoTable (
-> ProductId INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
-> ProductQuantity INT,
-> ProductPrice INT
-> );
mysql> INSERT INTO DemoTable(ProductQuantity, ProductPrice) VALUES(10, 100);
mysql> INSERT INTO DemoTable(ProductQuantity, ProductPrice) VALUES(5, 11);
mysql> INSERT INTO DemoTable(ProductQuantity, ProductPrice) VALUES(3, 140);
mysql> INSERT INTO DemoTable(ProductQuantity, ProductPrice) VALUES(2, 450);
সব রেকর্ড দেখুন
mysql> SELECT * FROM DemoTable;
+-----------+-----------------+--------------+
| ProductId | ProductQuantity | ProductPrice |
+-----------+-----------------+--------------+
| 1 | 10 | 100 |
| 2 | 5 | 11 |
| 3 | 3 | 140 |
| 4 | 2 | 450 |
+-----------+-----------------+--------------+
টোটাল প্রোডাক্ট ভ্যালু ক্যালকুলেট
mysql> SELECT SUM(ProductQuantity * ProductPrice) AS Total_of_Products
-> FROM DemoTable;
ফলাফল
+-------------------+
| Total_of_Products |
+-------------------+
| 2375 |
+-------------------+
গণনা:
- Product 1: 10 × 100 = 1,000
- Product 2: 5 × 11 = 55
- Product 3: 3 × 140 = 420
- Product 4: 2 × 450 = 900
- মোট: 1,000 + 55 + 420 + 900 = 2,375
প্রোডাক্ট বাই গ্রুপ করিয়ে টোটাল
SELECT ProductName, SUM(ProductQuantity * ProductPrice) AS TotalValue
FROM Products
GROUP BY ProductName;
স্টক ভ্যালু রিপোর্ট (যদি UnitCost কলাম থাকে)
SELECT
ProductName,
SUM(ProductQuantity) AS TotalQty,
SUM(ProductQuantity * UnitPrice) AS RetailValue,
SUM(ProductQuantity * UnitCost) AS CostValue,
SUM(ProductQuantity * (UnitPrice - UnitCost)) AS ProfitPotential
FROM Inventory
GROUP BY ProductName;