すべてのビジネスの利益を計算することが重要です。 WebサイトまたはオンラインストアがMySQLで実行されている場合は、MySQLでマージンを簡単に計算できます。この記事では、MySQLでマージンを計算する方法、各製品のマージンを計算する方法、および1日のマージンを計算する方法について説明します。
MySQLでマージンを計算する方法
2つのテーブルpurchases(order_id、product_id、quantity、cost_price、order_date があるとします。 )および sales(order_id、product_id、quantity、selling_price、order_date) 各製品の購入および販売情報の詳細が含まれています。
mysql> create table purchases(order_id int,
product_id int,
quantity int,
cost_price int,
order_date date);
mysql> insert into purchases(order_id, product_id,
quantity, cost_price, order_date)
values(1,101,10,30,'2021-01-01'),
(2,102,15,50,'2021-01-02'),
(3,101,30,80,'2021-01-03'),
(4,103,20,35,'2021-01-04'),
(5,105,10,50,'2021-01-05'),
(6,104,17,40,'2021-01-06'),
(7,101,30,45,'2021-01-07'),
(8,102,60,60,'2021-01-08'),
(9,110,19,70,'2021-01-09'),
(10,108,20,80,'2021-01-10');
mysql> select * from purchases;
+----------+------------+----------+------------+------------+
| order_id | product_id | quantity | cost_price | order_date |
+----------+------------+----------+------------+------------+
| 1 | 101 | 10 | 30 | 2021-01-01 |
| 2 | 102 | 15 | 50 | 2021-01-02 |
| 3 | 101 | 30 | 80 | 2021-01-03 |
| 4 | 103 | 20 | 35 | 2021-01-04 |
| 5 | 105 | 10 | 50 | 2021-01-05 |
| 6 | 104 | 17 | 40 | 2021-01-06 |
| 7 | 101 | 30 | 45 | 2021-01-07 |
| 8 | 102 | 60 | 60 | 2021-01-08 |
| 9 | 110 | 19 | 70 | 2021-01-09 |
| 10 | 108 | 20 | 80 | 2021-01-10 |
+----------+------------+----------+------------+------------+
mysql> create table sales(order_id int,
product_id int,
quantity int,
selling_price int,
order_date date);
mysql> insert into sales(order_id, product_id,
quantity, selling_price, order_date)
values(1,101,8,70,'2021-01-01'),
(2,102,10,150,'2021-01-02'),
(3,101,25,280,'2021-01-03'),
(4,103,20,135,'2021-01-04'),
(5,105,10,350,'2021-01-05'),
(6,104,15,140,'2021-01-06'),
(7,101,20,65,'2021-01-07'),
(8,102,50,160,'2021-01-08'),
(9,110,15,120,'2021-01-09'),
(10,108,15,180,'2021-01-10');
mysql> select * from sales;
+----------+------------+----------+---------------+------------+
| order_id | product_id | quantity | selling_price | order_date |
+----------+------------+----------+---------------+------------+
| 1 | 101 | 8 | 70 | 2021-01-01 |
| 2 | 102 | 10 | 150 | 2021-01-02 |
| 3 | 101 | 25 | 280 | 2021-01-03 |
| 4 | 103 | 20 | 135 | 2021-01-04 |
| 5 | 105 | 10 | 350 | 2021-01-05 |
| 6 | 104 | 15 | 140 | 2021-01-06 |
| 7 | 101 | 20 | 65 | 2021-01-07 |
| 8 | 102 | 50 | 160 | 2021-01-08 |
| 9 | 110 | 15 | 120 | 2021-01-09 |
| 10 | 108 | 15 | 180 | 2021-01-10 |
+----------+------------+----------+---------------+------------+
これらのテーブルを使用して、MySQLのマージンを計算します。これがマージンの公式です。
利益率=(販売数量*販売価格–購入数量*原価 )/販売数量*販売価格
総売上高と総コストを取得したら、要件に応じて上記の式を変更して、粗利益、純利益などを計算できます。
MySQLで合計マージンを計算する
総利益率を計算するためのSQLクエリは次のとおりです。
mysql> select (total_sales-total_cost)*100/total_sales as total_margin
from
(select sum(quantity*cost_price) as total_cost
from purchases) as total_purchases,
(select sum(quantity*selling_price) as total_sales
from sales) as total_sales;
+--------------+
| total_margin |
+--------------+
| 57.6059 |
+--------------+ 上記のクエリを詳しく見てみましょう。
サブクエリを使用して、総コストと総売上を別々に計算します
select sum(quantity*cost_price) as total_cost from purchases
および
select sum(quantity*selling_price) as total_sales from sales
次に、これらのサブクエリの結果を使用して、総利益率を計算します。
ボーナスリード:MySQLで複数のカウントを取得する方法
MySQLで製品マージンを計算する
各製品の利益率を計算するためのSQLクエリは次のとおりです。
mysql> select total_purchases.product_id,
(total_sales-total_cost)*100/total_sales as total_margin
from
(select product_id,sum(quantity*cost_price) as total_cost
from purchases
group by product_id) as total_purchases,
(select product_id,sum(quantity*selling_price) as total_sales
from sales
group by product_id) as total_sales
where total_purchases.product_id=total_sales.product_id;
+------------+--------------+
| product_id | total_margin |
+------------+--------------+
| 101 | 54.2889 |
| 102 | 54.2105 |
| 103 | 74.0741 |
| 104 | 67.6190 |
| 105 | 85.7143 |
| 108 | 40.7407 |
| 110 | 26.1111 |
+------------+--------------+ 上記のクエリでは、次の2つのサブクエリを使用して、各製品の売上とコストを個別に計算します。
select product_id,sum(quantity*cost_price) as total_cost from purchases group by product_id
および
select product_id,sum(quantity*selling_price) as total_sales from sales group by product_id
次に、 product_idに基づいてこれら2つのサブクエリの結果を結合します 各製品の総利益率を計算する列。
MySQLで1日のマージンを計算する
同様に、次のSQLクエリを使用して1日の利益率を計算できます。
mysql> select total_purchases.order_date,(total_sales-total_cost)*100/total_sales as total_margin
from
(select date(order_date) as order_date,
sum(quantity*cost_price) as total_cost
from purchases group by date(order_date)
) as total_purchases,
(select date(order_date) as order_date,
sum(quantity*selling_price) as total_sales
from sales group by date(order_date)
) as total_sales
where total_purchases.order_date=total_sales.order_date;
+------------+--------------+
| order_date | total_margin |
+------------+--------------+
| 2021-01-01 | 46.4286 |
| 2021-01-02 | 50.0000 |
| 2021-01-03 | 65.7143 |
| 2021-01-04 | 74.0741 |
| 2021-01-05 | 85.7143 |
| 2021-01-06 | 67.6190 |
| 2021-01-07 | -3.8462 |
| 2021-01-08 | 55.0000 |
| 2021-01-09 | 26.1111 |
| 2021-01-10 | 40.7407 |
+------------+--------------+ 上記のクエリでは、次の2つのサブクエリを使用して、各日の売上とコストを個別に計算します。
select date(order_date) as order_date,sum(quantity*cost_price) as total_cost from purchases group by date(order_date)
および
select date(order_date) as order_date,sum(quantity*selling_price) as total_sales from sales group by date(order_date)
次に、 order_dateに基づいてこれら2つのサブクエリの結果を結合します 毎日の合計マージンを計算する列。
Ubiqでマージンを計算する
Ubiq Reportingツールは、上記のすべてのSQLクエリをサポートし、さまざまな方法でSQL結果を簡単に視覚化できるようにします。また、MySQLデータからダッシュボードとチャートを作成することもできます。これは、Ubiqでマージンを毎日計算するためのSQLクエリです。
実際、クエリを実行した後、視覚化タイプをクリックするだけで、結果をグラフにプロットできます。
>
MySQL用のレポートツールが必要ですか? Ubiqを使用すると、データを数分で簡単に視覚化し、リアルタイムのダッシュボードで監視できます。今日それを試してみてください!