I have the following table
MySQL [distributor]> select * from orderitems;
+-----------+------------+---------+----------+------------+
| order_num | order_item | prod_id | quantity | item_price |
+-----------+------------+---------+----------+------------+
| 20005 | 1 | BR01 | 100 | 5.49 |
| 20005 | 2 | BR03 | 100 | 10.99 |
| 20006 | 1 | BR01 | 20 | 5.99 |
| 20006 | 2 | BR02 | 10 | 8.99 |
| 20006 | 3 | BR03 | 10 | 11.99 |
| 20007 | 1 | BR03 | 50 | 11.49 |
| 20007 | 2 | BNBG01 | 100 | 2.99 |
| 20007 | 3 | BNBG02 | 100 | 2.99 |
| 20007 | 4 | BNBG03 | 100 | 2.99 |
| 20007 | 5 | RGAN01 | 50 | 4.49 |
| 20008 | 1 | RGAN01 | 5 | 4.99 |
| 20008 | 2 | BR03 | 5 | 11.99 |
| 20008 | 3 | BNBG01 | 10 | 3.49 |
| 20008 | 4 | BNBG02 | 10 | 3.49 |
| 20008 | 5 | BNBG03 | 10 | 3.49 |
| 20009 | 1 | BNBG01 | 250 | 2.49 |
| 20009 | 2 | BNBG02 | 250 | 2.49 |
| 20009 | 3 | BNBG03 | 250 | 2.49 |
+-----------+------------+---------+----------+------------+
18 rows in set (0.098 sec)
I am able to summarize the total price of a specified order as;
MySQL [distributor]> select order_num, sum(quantity*item_price)
as total_price from orderitems where order_num = 20008;
+-----------+-------------+
| order_num | total_price |
+-----------+-------------+
| 20008 | 189.60 |
+-----------+-------------+
1 row in set (0.026 sec)
How could I aggregate the total-price of each order and display them all together?
A SELECT statement clause that divides the query result into groups of rows, usually for the purpose of performing one or more aggregations on each group. The SELECT statement returns one row per group.
So in your all things are fine just need group by order_num
select order_num, sum(quantity*item_price) as total_price from orderitems
group by order_num
Use GROUP BY:
select order_num, sum(quantity*item_price) as total_price
from orderitems group by order_num
If you want to show total price of all orders which order details, you can remove order num from select:-
select sum(quantity*item_price) as total_price from orderitems;
However, if you are looking to show total price of all orders against each order, you can use below query :-
select order_num, sum(sum(quantity*item_price)) OVER() as total_price
from orderitems
group by order_num;
Related
I have two mysql tables. The two tables with the sample data are as follows.
select * from stock;
+----------+----------+--------+---------+-------------------+---------------+-------------+---------------------+
| stock_id | qty_type | qty | item_id | stock_location_id | stock_type_id | purchase_id | created_date |
+----------+----------+--------+---------+-------------------+---------------+-------------+---------------------+
| 48 | v | 44.00 | 1 | 1 | 1 | 38 | 2022-05-16 14:27:19 |
| 49 | v | 8.00 | 263 | 1 | 1 | 38 | 2022-05-16 14:27:19 |
| 50 | a | 6.00 | 1 | 1 | 1 | 39 | 2022-05-16 14:30:04 |
| 51 | a | 4.00 | 263 | 1 | 1 | 39 | 2022-05-16 14:30:04 |
| 56 | a | 28.00 | 1 | 1 | 1 | 41 | 2022-05-16 14:51:59 |
| 57 | a | 57.00 | 263 | 1 | 1 | 41 | 2022-05-16 14:51:59 |
| 58 | a | 6.00 | 264 | 1 | 1 | 41 | 2022-05-16 14:51:59 |
| 59 | a | 19.00 | 301 | 1 | 1 | 41 | 2022-05-16 14:51:59 |
| 64 | a | 15.00 | 263 | 1 | 5 | 0 | 2022-05-18 17:23:37 |
| 65 | a | 546.00 | 264 | 1 | 5 | 0 | 2022-05-18 17:23:37 |
| 66 | a | 15.00 | 263 | 1 | 5 | 0 | 2022-05-18 17:24:21 |
| 67 | a | 546.00 | 264 | 1 | 5 | 0 | 2022-05-18 17:24:21 |
| 72 | v | 20.00 | 720 | 1 | 1 | 44 | 2022-05-24 09:24:43 |
| 73 | v | 2.00 | 729 | 1 | 1 | 44 | 2022-05-24 09:24:43 |
+----------+----------+--------+---------+-------------------+---------------+-------------+---------------------+
select * from sales;
+----------+---------+----------+-----------+
| sales_id | item_id | quantity | basket_id |
+----------+---------+----------+-----------+
| 7 | 1 | 20.00 | 4 |
| 8 | 263 | 3.00 | 4 |
| 9 | 1 | 2.00 | 5 |
| 10 | 263 | 4.00 | 5 |
| 11 | 264 | 6.00 | 5 |
| 12 | 301 | 1.00 | 5 |
+----------+---------+----------+-----------+
By this I want to build up a update query. This should deduct the quantity of the items in the sales table from the stock table. If such a query can be created in mysql it should be updated in ascending order on the stock_id in the stock table.
If such an update query can be built, based on the data in the two tables above, I expect the result to be as follows.
select stock_id, qty_type, qty, item_id from stock where qty_type = 'a';
+----------+----------+--------+---------+
| stock_id | qty_type | qty | item_id |
+----------+----------+--------+---------+
| 50 | a | 0.00 | 1 | -- clear by sales
| 51 | a | 0.00 | 263 | -- clear by sales
| 56 | a | 12.00 | 1 | -- deduct qty by sales
| 57 | a | 54.00 | 263 | -- deduct qty by sales
| 58 | a | 0.00 | 264 | -- clear by sales
| 59 | a | 18.00 | 301 | -- deduct qty by sales
| 64 | a | 15.00 | 263 |
| 65 | a | 546.00 | 264 |
| 66 | a | 15.00 | 263 |
| 67 | a | 546.00 | 264 |
+----------+----------+--------+---------+
Any help would be highly appreciated.
Here is my understanding of the problem:
According to your stock table, there are 06 quantity in hand of item_id #1
+----------+----------+-------+---------+
| stock_id | qty_type | qty | item_id |
+----------+----------+-------+---------+
| 50 | a | 6.00 | 1 |
+----------+----------+-------+---------+
Then again a new stock comes in and new entry is made in stock table. For example, another 28 quantity has come in, so the table would look like as follows:
+----------+----------+-------+---------+
| stock_id | qty_type | qty | item_id |
+----------+----------+-------+---------+
| 50 | a | 6.00 | 1 |
| 56 | a | 28.00 | 1 |
+----------+----------+-------+---------+
Now, there are 6 + 28 = 34 quantity in stock. According to your sales data, you have sold twice of item_id #1. As first time 20 items and next time 2 items. Since we have more quantity in stock than sold quantity, so we can do both sales.
As you mentioned in your question, now we have to adjust the sold items from stock in ascending order on the stock_id in the stock table. Of course here you are using the First In First Out (FIFO) method to maintain your stock. This method works in such a way that you have to clear stock which has come first.
Now it would not be a very simple SQL which can update the stock in hand, however this is my attempt.
INSERT INTO stock (stock_id, qty)
SELECT stock_id
, newqty
FROM (
SELECT
stock_id
, #sales := IF(#prev = k.item_id, #sales, sold) as sales
, IF(qty <= #sales, 0, qty - #sales) as newqty
, #sales := IF(#sales >= qty, #sales - qty, 0)
, #prev := k.item_id as item
FROM stock k
JOIN (
SELECT item_id
, sum(quantity) as sold
FROM sales
GROUP BY item_id
) s ON k.item_id = s.item_id AND qty_type = 'a'
JOIN (SELECT #prev:=null, #sales:=0) init
ORDER BY k.item_id, stock_id
) calc
ON DUPLICATE KEY UPDATE
qty = VALUES(qty);
My query can only be run once for a set of sales data as each time it reduces the stock and updates the stock table (as you requested). Not the best method as it means storing derived data.
The following is the result of running the above query one at a time each time data is added to the sales table.
Starting Stock
select * from stock;
+----------+----------+-------+---------+-------------------+---------------+-------------+
| stock_id | qty_type | qty | item_id | stock_location_id | stock_type_id | purchase_id |
+----------+----------+-------+---------+-------------------+---------------+-------------+
| 1 | a | 6.00 | 1 | 1 | 5 | 0 |
| 2 | a | 28.00 | 1 | 1 | 5 | 0 |
+----------+----------+-------+---------+-------------------+---------------+-------------+
Result 01 - After 1st sale
select * from sales;
+----------+---------+----------+-----------+
| sales_id | item_id | quantity | basket_id |
+----------+---------+----------+-----------+
| 1 | 1 | 20.00 | 1 |
+----------+---------+----------+-----------+
select * from stock;
+----------+----------+-------+---------+-------------------+---------------+-------------+
| stock_id | qty_type | qty | item_id | stock_location_id | stock_type_id | purchase_id |
+----------+----------+-------+---------+-------------------+---------------+-------------+
| 1 | a | 0.00 | 1 | 1 | 5 | 0 |
| 2 | a | 14.00 | 1 | 1 | 5 | 0 |
+----------+----------+-------+---------+-------------------+---------------+-------------+
Result 02 - After 2nd sale
select * from sales;
+----------+---------+----------+-----------+
| sales_id | item_id | quantity | basket_id |
+----------+---------+----------+-----------+
| 1 | 1 | 20.00 | 1 |
| 2 | 1 | 2.00 | 2 |
+----------+---------+----------+-----------+
select * from stock;
+----------+----------+-------+---------+-------------------+---------------+-------------+
| stock_id | qty_type | qty | item_id | stock_location_id | stock_type_id | purchase_id |
+----------+----------+-------+---------+-------------------+---------------+-------------+
| 1 | a | 0.00 | 1 | 1 | 5 | 0 |
| 2 | a | 12.00 | 1 | 1 | 5 | 0 |
+----------+----------+-------+---------+-------------------+---------------+-------------+
I have some tables that used to manage vehicle details. Related tables as follows:
store_item
+---------+-----------+--------+
| item_id | item_name | status |
+---------+-----------+--------+
| 1 | Diesel | 1 |
| 2 | Petrol | 1 |
+---------+-----------+--------+
tbl_vehicle
+------------+---------------+
| vehicle_id | registered_no |
+------------+---------------+
| 1 | LE-7476 |
| 2 | 270-0523 |
+------------+---------------+
tbl_direct_fuel
+----------------+----------+---------+------------+------------+
| direct_fuel_id | vehicle | orderNo | issue_date | milo_meter |
+----------------+----------+---------+------------+------------+
| 1 | LE-7476 | 173072 | 2019-11-12 | 30,000 |
| 2 | LE-7476 | 173069 | 2019-11-08 | 29,600 |
| 3 | LE-7476 | 173059 | 2019-11-05 | 29,000 |
| 4 | LE-7476 | 173055 | 2019-10-08 | 25,000 |
| 5 | 270-0523 | 173068 | 2019-11-02 | 10,000 |
| 6 | 270-0523 | 173067 | 2019-10-02 | 8,500 |
+----------------+----------+---------+------------+------------+
tbl_direct_fuel_details
+------------------------+----------------+------+----------+------------+
| direct_fuel_details_id | direct_fuel_id | item | fuel_qty | fuel_price |
+------------------------+----------------+------+----------+------------+
| 100 | 1 | 1 | 20 | 105 |
| 101 | 2 | 1 | 15 | 105 |
| 102 | 3 | 1 | 12 | 105 |
| 103 | 4 | 2 | 50 | 165 |
| 104 | 5 | 1 | 25 | 100 |
| 105 | 6 | 2 | 18 | 165 |
+------------------------+----------------+------+----------+------------+
Desired Output
I want to get no_of_kms, vs issued fuel quantities using issue_date & milo_meter (current milo_meter - previous milo_meter). The output as follows :
+----------+-----------+
| vehicle | no_of_kms |
+----------+-----------+
| LE-7476 | 400 |
| LE-7476 | 600 |
| LE-7476 | 4,000 |
| 270-0523 | 1500 |
+----------+-----------+
I used the following query :
select v1.registered_no as vehicle, si.item_name as fuel, df.milo_meter - df.milo_meter as no_of_kms
from (select dfd.item, sum(dfd.fuel_qty) AS qty
from tbl_direct_fuel df
join tbl_direct_fuel_details dfd on df.direct_fuel_id = dfd.direct_fuel_id
join tbl_vehicle v1 on df.vehicle = v1.vehicle_id
where df.status = 1
group by registered_no) dfd
join store_item si on dfd.item = si.item_id
join (select item, sum(dfd.fuel_qty) AS fuel_qty
from tbl_direct_fuel_details
group by item) dfd on si.item_id=dfd.item
But the above query didn't working fine. What may be going wrong ? Can anyone help me ?
If you are running MySQL 8.0, you can do this simply with window function lag():
select *
from (
select
issue_date,
vehicle,
milo_meter
- lag(milo_meter) over(partition by vehicle order by issue_date) no_of_kms
from tbl_direct_fuel
) t
where no_of_kms is not null
order by vehicle desc, issue_date desc
I added the issue_date to the output columns, since this seems like a sensible information to understand the results.
In earlier versions, I think that an inline query might do the trick:
select *
from (
select
issue_date,
vehicle,
milo_meter - (
select max(milo_meter)
from tbl_direct_fuel d1
where d1.vehicle = d.vehicle and d1.issue_date < d.issue_date
) no_of_kms
from tbl_direct_fuel d
) t
where no_of_kms is not null
order by vehicle desc, issue_date desc
This assumes that the milo_meter of a given vehicule can only increase, which seems like a reasonable assumption.
Demo on DB Fiddle
Both queries return:
issue_date | vehicle | no_of_kms
:--------- | :------- | --------:
2019-11-12 | LE-7476 | 400
2019-11-08 | LE-7476 | 600
2019-11-05 | LE-7476 | 4000
2019-11-02 | 270-0523 | 1500
I have the following data table from which I would like to sum the values of the field 'pts' for each 'pid' as follows:
The sum of the top 3 values per 'cont' plus the values of any other 'cont' per 'pid'. The results should be presented in DESC order by 'total'
+--------+-----+------+
| pid | pts | cont |
+--------+-----+------+
| 121693 | 40 | 1 |
| 121693 | 80 | 2 |
| 121693 | 120 | 1 |
| 121693 | 100 | 1 |
| 121693 | 500 | 1 |
| 121694 | 20 | 1 |
| 121694 | 0 | 2 |
| 121694 | 30 | 3 |
| 121695 | 0 | 1 |
| 121695 | 30 | 2 |
| 121695 | 0 | 1 |
+--------+-----+------+
In this example the query should return something like this
+--------+-------+
| pid | total |
+--------+-------+
| 121693 | 800 |
| 121694 | 50 |
| 121695 | 30 |
+--------+-------+
Is this possible?
Thanks in advance.
SELECT DISTINCT pid, SUM(Pts) AS Total
FROM your tablename
GROUP BY Pid
ORDER BY TOTAL
(Requires testing and minor fixes on small syntax)
i have following tables,
mysql> select * from purchase_order;
+-------------------+-------------------------+-------+---------------------+
| purchase_order_id | purchase_order | cost | created_on |
+-------------------+-------------------------+-------+---------------------+
| 1 | Dell Computer 000001256 | 10000 | 2015-02-19 22:14:52 |
| 2 | HP Computer 000001256 | 50000 | 2015-02-19 22:14:52 |
+-------------------+-------------------------+-------+---------------------+
2 rows in set (0.00 sec)
mysql> select * from purchase_order_detail;
+--------------------------+-------------------+---------+------------------+
| purchase_order_detail_id | purchase_order_id | item_id | ordered_quantity |
+--------------------------+-------------------+---------+------------------+
| 1 | 1 | 279 | 100 |
| 2 | 1 | 286 | 100 |
| 3 | 2 | 279 | 200 |
| 4 | 2 | 286 | 300 |
+--------------------------+-------------------+---------+------------------+
4 rows in set (0.00 sec)
mysql> select * from delivery_order;
+-------------------+--------------------------+-------------------+---------------------+
| delivery_order_id | purchase_order_detail_id | recieved_quantity | recieved_on |
+-------------------+--------------------------+-------------------+---------------------+
| 1 | 1 | 50 | 2015-02-19 22:22:51 |
| 2 | 2 | 50 | 2015-02-19 22:24:59 |
| 3 | 1 | 50 | 2015-02-19 22:34:14 |
| 4 | 3 | 70 | 2015-02-20 11:11:31 |
| 5 | 4 | 150 | 2015-02-20 11:11:31 |
| 6 | 3 | 90 | 2015-02-20 11:12:20 |
| 7 | 4 | 100 | 2015-02-20 11:12:20 |
| 8 | 3 | 40 | 2015-02-20 11:12:55 |
| 9 | 4 | 50 | 2015-02-20 11:12:55 |
+-------------------+--------------------------+-------------------+---------------------+
9 rows in set (0.00 sec)
mysql> select * from stock;
+----------+-------------------+------------+----------+
| stock_id | delivery_order_id | project_id | quantity |
+----------+-------------------+------------+----------+
| 1 | 1 | 1 | 30 |
| 2 | 1 | 2 | 20 |
| 3 | 2 | 1 | 50 |
| 4 | 3 | 1 | 40 |
| 5 | 3 | 2 | 10 |
+----------+-------------------+------------+----------+
i want to fetch all purchase_order and their quantity in stock for those purchase who has item_id = 279 in it.
The Goal is to create views in which i simply pass the item_id and it fetches the list of all purchase_order in which item_id that will be input parameter and their total quantity in stock.
so, far i have write this query, i am new to mysql and views
select po.purchase_order_id, po.purchase_order from purchase_order po, purchase_order_detail pod where po.purchase_order_id = pod.purchase_order_id and pod.item_id = 279;
+-------------------+-------------------------+
| purchase_order_id | purchase_order |
+-------------------+-------------------------+
| 1 | Dell Computer 000001256 |
| 2 | HP Computer 000001256 |
+-------------------+-------------------------+
but it want some thing like this,
+-------------------+-------------------------+----------+-----------+
| purchase_order_id | purchase_order | item_id | quantity |
+-------------------+-------------------------+----------+-----------+
| 1 | Dell Computer 000001256 | 279 | 100 +
| 2 | HP Computer 000001256 | 279 | 0 +
+-------------------+-------------------------+----------+-----------+
Try this untested query:
select po.purchase_order_id, po.purchase_order, sum(s.quantity)
from purchase_order po
join purchase_order_detail pod on po.purchase_order_id = pod.purchase_order_id
join delivery_order do on do.purchase_order_id = pod.purchase_order_id
join stock s on s.delivery_order_id = do.delivery_order_id
where pod.item_id = 279
group by po.purchase_order_id, po.purchase_order;
i want to find solution for my query problem. I need to find the SUM of all priceProduct*quantity and separated with each of productcategory. I have already made a query, but it takes longer time to executed it. this is my query,
SELECT
pb.ProductCategoryID,
pb.ProductCategoryDescription,
(SELECT
SUM((SELECT pd.HPP FROM `price details` pd WHERE pd.ProductID = pdt.ProductID ORDER BY pd.PriceDetailID DESC LIMIT 1)*
(SELECT StockProductBallance FROM `stock product` sp WHERE sp.ProductID = pdt.ProductID ORDER BY sp.StockProductID DESC LIMIT 1))
FROM product pdt
WHERE pdt.ProductCategoryID = pb.ProductCategoryID
) AS Total
FROM `product category` pb
GROUP BY pb.ProductCategoryID
this my example table
table product:
+------+-------+
| id_p | id_pc |
+------+-------+
| 1 | 3 |
| 2 | 4 |
| 3 | 3 |
| 4 | 4 |
+------+-------+
table productcategory:
+-------+---------+
| id_pc | pc_name |
+-------+---------+
| 3 | new_pc |
| 4 | old_pc |
+-------+---------+
table price details:
+---------------+------+-----+
| PriceDetailID | id_p | hpp |
+---------------+------+-----+
| 1 | 1 | 100 |
| 2 | 1 | 110 |
| 3 | 1 | 120 |
| 4 | 2 | 200 |
| 5 | 2 | 210 |
| 6 | 2 | 220 |
+---------------+------+-----+
table stockProduct:
+-----------------+------+---------------+
| id_stockProduct | id_p | stockballance |
+-----------------+------+---------------+
| 1 | 1 | 10 |
| 2 | 1 | 11 |
| 3 | 1 | 12 |
| 4 | 2 | 20 |
| 5 | 2 | 21 |
| 6 | 2 | 22 |
+-----------------+------+---------------+
Really need your help guys, for better query..