Sum data from same table based on another column id's - mysql

I have table that looks like this
+-----------+----------+----------+
| ProductID | ShopID | Quantity |
+-----------+----------+----------+
| 1 | 1 | 10 |
| 2 | 1 | 10 |
| 3 | 1 | 15 |
| 1 | 2 | 25 |
| 4 | 1 | 5 |
| 2 | 2 | 6 |
| 4 | 3 | 7 |
+-----------+------------+--------+
And I need to get sum of quantity from multiple shops for single product.
For example: sum of quantity for ProductID 1 from ShopID 1 and 2.
So result for that would be 35.
Is it possible to do that in single query?

you need to group by product id
select sum(Quantity) as total from TableName
where ProductID =1 and ShopID in (1,2)
group by ProductID

Use Sum aggregate function on Quantity column and use the needed condition on productid and shopid.
select sum(Quantity) as Total_Quantity from YourTableName
where ProductID = 1 and ShopID in (1,2)

Related

Mysql fetch rows limited be another table column value

I have list of products and seller who added this products
Each Seller has a limit to show the products.
I need to write a query to list all the products with pagination 100 and show only seller products limited to that seller
`Seller Table`
| seller id| Seller Name | limit |
|:-------- |:-----------:| ------:|
| 1 | Seller One | 1 |
| 2 | Seller Two | 3 |
| 3 | Seller Three| 2 |
`Products Table
| product id| seller id | Product Name |
|:-------- |:----------:| ------------:|
| 1 | 1 | Product 1 |
| 2 | 2 | Product 2 |
| 3 | 1 | Product 3 |
| 4 | 1 | Product 4 |
| 5 | 2 | Product 5 |
| 6 | 2 | Product 6 |
| 7 | 2 | Product 7 |
| 8 | 3 | Product 8 |
| 9 | 3 | Product 9 |
| 9 | 3 | Product 10 |
| 9 | 3 | Product 11 |
(Output or Result I am expecting)
| product id| seller id | Product Name |
|:-------- |:----------:| ------------:|
| 1 | 1 | Product 1 |
| 2 | 2 | Product 2 |
| 5 | 2 | Product 5 |
| 6 | 2 | Product 6 |
| 8 | 3 | Product 8 |
| 9 | 3 | Product 9 |
Seller 1 = Product 1
Seller 2 = Product 2 , Product 5 and Product 6
Seller 3 = Product 8 and Product 9
I want to get this products
How do I write a query?
and also is it possible to selected random products of the seller
I have query only to fetch only the products.
$products = Products::paginate(100);
```
I need modify it based on Seller Model
Enable WHERE clause when searching particular seller and product otherwise disable it. Use LIMIT and OFFSET for pagination. Please try this
-- MySQL (v5.8)
SELECT t.product_id, s.seller_id, t.product_name
FROM seller s
INNER JOIN (SELECT product_id
, seller_id
, product_name
, ROW_NUMBER() OVER (PARTITION BY seller_id ORDER BY product_id) row_num
FROM Products
-- WHERE seller_id = 1
-- AND product_id = 1
) t
ON s.seller_id = t.seller_id
AND t.row_num <= s.t_limit;
Please check from url https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=d142f50fb24763ec9fc1e937f8b8d6a7
If needed comma separated product_name then try this one
SELECT CONCAT(MAX(s.seller_name), ' = ', GROUP_CONCAT(t.product_name)) expected_output
FROM seller s
INNER JOIN (SELECT product_id
, seller_id
, product_name
, ROW_NUMBER() OVER (PARTITION BY seller_id ORDER BY product_id) row_num
FROM Products
-- WHERE seller_id = 1
-- AND product_id = 1
) t
ON s.seller_id = t.seller_id
AND t.row_num <= s.t_limit
GROUP BY s.seller_id;
Please check from https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=0e4cfa7dbb744e7aa416b79d6f964775
As per #RahulBiswas sql code I wrote a Laravel Query. It is working
$subq = Products::select(DB::raw('ROW_NUMBER() OVER (PARTITION BY seller_id ORDER BY id) row_num, *'));
$query = Sellers::select('t.*',)->join(\DB::raw('('.$subq->toSql().') as t'), function ($join) use ($subq) {
$join->on('sellers.user_id','t.seller_id')
$join->on('t.row_num', '<=', "sellers.limit")
})

Get those items which are ordered after they have been delivered

I have two tables, namely itemOrders and itemDelivered.
itemOrders
+-------+---------+--------+
| id | orderid | itemid |
+-------+---------+--------+
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 2 | 1 |
| 4 | 2 | 2 |
| 5 | 3 | 1 |
| 6 | 3 | 2 |
+-------+---------+--------+
And
itemDelivered
+-------+-------------+--------+
| id | orderId | itemid |
+-------+-------------+--------+
| 1 | 2 | 2 |
| 2 | 3 | 2 |
| 3 | 2 | 1 |
+-------+-------------+--------+
From the above scenario I want all those distinct items whose max orderId in the table itemDelivered is less than max orderId in the table itemOrders.
In the above example I should get itemid 1 as the result, as it's max orderid is 2 in table itemDelivered, which is less than its max orderid in table itemOrders which is 3.
I wrote the following query but it gives me both the items, 1 and 2 as item No. 2 doesn't have orderId 1 in itemDelivered table.
SELECT DISTINCT( itemid )
FROM itemorders
WHERE orderid NOT IN (SELECT orderid
FROM itemdelivered)
You can LEFT JOIN between the two table using itemid, and GROUP BY on the itemid.
Eventually use HAVING clause to consider only those itemid values, where MAX(itemdelivered.orderid) < MAX(itemorders.orderid)
View on DB Fiddle
SELECT io.itemid
FROM itemorders AS io
LEFT JOIN itemdelivered AS id
ON id.itemid = io.itemid
GROUP BY io.itemid
HAVING MAX(id.orderid) < MAX(io.orderid)
OR MAX(id.orderid) IS NULL
Result
| itemid |
| ------ |
| 1 |
Ok, manage to write a query which gives the desired output.
SELECT io.itemid
FROM itemorders as io
LEFT JOIN itemdelivered AS id ON io.orderid = id.orderid AND io.itemid = id.itemid
WHERE id.itemid IS NULL
HAVING MAX(id.orderid) IS NULL
ORDER BY io.id

Getting a total from multiple rows in MySQL

I am not very good at MySQL queries. Can someone help me figure out how to do this?
I have a table like this (lets call it stats):
+----+-------+-----+
| id | memid | qty |
+----+-------+-----+
| 1 | 99 | 0 |
+----+-------+-----+
| 2 | 102 | 22 |
+----+-------+-----+
| 3 | 102 | 10 |
+----+-------+-----+
| 4 | 99 | 100 |
+----+-------+-----+
| 5 | 17 | 25 |
+----+-------+-----+
| 6 | 87 | 72 |
+----+-------+-----+
| 7 | 36 | 0 |
+----+-------+-----+
| 8 | 102 | 6 |
+----+-------+-----+
I need a MySQL query that will combine the qty of all the memids and ORDER BY ASC the total qty for each memid.
Thank you in advance for your help! :)
You can select SUM as another field in query and order it by qty, e.g.:
SELECT id, memid, qty, SUM(qty)
FROM table
ORDER BY qty;
Please note that SUM will return the same value for all the rows as it will be a constant value.
If you have multiple records per memid and want to calculate SUM per memid then you can use GROUP BY e.g.:
SELECT memid, SUM(qty) AS `sum`
FROM table
GROUP BY memid
ORDER BY sum;

MySQL SUM and GROUP BY from AS column

table1 structure and sample data
+----+-------------+----------+------+-------+
| ID | DESCRIPTION | QUANTITY | EACH | PRICE |
+----+-------------+----------+------+-------+
| 1 | Product 1 | 1 | 12 | 1*12 |
| 2 | Product 2 | 2 | 3 | 2* 3 |
| 3 | Prodcut 3 | NULL | 3 | |
| 4 | Product 1 | 2 | 10 | 2*10 |
| 5 | Product 3 | NULL | 7 | |
+----+-------------+----------+------+-------+
MySQL query:
SELECT
DESCRIPTION,
QUANTITY,
EACH,
COALESCE(QUANTITY, 1) * EACH AS PRICE
FROM table1
GROUP BY DESCRIPTION
How could I make SUM for the PRICE column and GROUP BY the DESCRIPTION column? I don't want to use UPDATE because I can't change the values in the table1.
Well why can't you add SUM(COALESCE(QUANTITY, 1) * EACH) AS PRICETOTAL in your select list.
SELECT
DESCRIPTION,
SUM(COALESCE(QUANTITY,1)*each) as 'Price'
FROM table1
GROUP BY DESCRIPTION
returns
DESCRIPTION Price
Product 1 32
Product 2 6
Product 3 10

How to sum multiple Product Prices for a unique order

I have a table with ordered items where occasionally I have one order ID with multiple lines (1 line for each ordered item).
OrderID | Order.Item_id | Item.Price | Quantity | OrderTotal | Seller ID |
1 | 1 | 10 | 1 | 10 |1 |
2 | 2 | 15 | 1 | 25 |1 |
2 | 3 | 10 | 1 | 25 |2 |
3 | 4 | 15 | 1 | 44 |1 |
3 | 5 | 10 | 1 | 44 |1 |
3 | 6 | 19 | 1 | 44 |2 |
I need a way to export/display the OrderTotal ONLY for the given Seller ID.
Ideally I need a result in the lines of:
OrderID | Order.Item_id | Item.Price | Quantity | OrderTotal | Seller ID |
1 | 1 | 10 | 1 | 10 |1 |
2 | 2 | 15 | 1 | 15 |1 |
3 | 4 | 15 | 1 | 25 |1 |
3 | 5 | 10 | 1 | 25 |1 |
In the case of OrderID 2 I can easily pick simply the Item Price but this won't work for OrderID 3 where I need to sum two items. Is this possible using only SQL. Could you please help me?
Many Thanks in advance!
You could grab the sum of orders by order and seller, then join back to order info get the other values:
select o.order_id, o.item_id, o.price, o.quantity, q.total, o.seller_id from (
select order_id, seller_id, sum(price) total
from orders
where seller_id = 1
group by order_id, seller_id
) q
join orders o on (o.order_id = q.order_id and q.seller_id = o.seller_id);
Fiddle
try as below
Select SellerID,
OrderID,
-- below will display the items id comma seperated
group_concat(Itme_id) as itemlist,
-- below will display the item price comma seperated, you can use sum(Price) also
group_concat(Price) as pricelist,
sum(quantity) as totalquantity,
sum(OrderTotal) as totalorders
From Table
where SellerID = 1
group by SellerID,OrderID