MySQL selecting rows that are not in grouping - mysql

I have the following MySQL table of sample data:
create table prices (
id INTEGER PRIMARY KEY AUTO_INCREMENT,
store_id INTEGER NOT NULL,
item INTEGER NOT NULL,
price FLOAT NOT NULL
);
insert into prices values(1,1,1,1.25);
insert into prices values(2,1,2,.99);
insert into prices values(3,2,2,1.20);
insert into prices values(4,3,3,3.25);
insert into prices values(5,3,1,1.35);
I am looking for a query to select any store_id that a company would not use to purchase product. For example in prices, store_id 2 would not be used because item 2 can be bought for cheaper from store_id 1.
How would I go about this? Thank you.

With this query:
select item, min(price) minprice
from prices
group by item
you get the minimum price for each item.
If you left join the table prices to the above query and keep only the non matching rows (meaning rows that don't have the cheapest price for that item), you will get all the stores and items that can be found cheaper:
select p.*
from prices p left join (
select item, min(price) minprice
from prices
group by item
) g on g.item = p.item and g.minprice = p.price
where g.item is null
See the demo.
Results:
| id | store_id | item | price |
| --- | -------- | ---- | ----- |
| 3 | 2 | 2 | 1.2 |
| 5 | 3 | 1 | 1.35 |
If you want only the stores that you would not buy anything from:
select p.store_id
from prices p left join (
select item, min(price) minprice
from prices
group by item
) g on g.item = p.item and g.minprice = p.price
group by p.store_id
having sum(g.item is not null) = 0
See the demo.
Results:
| store_id |
| -------- |
| 2 |

SELECT *, MIN(price) from prices group by item
Gives you the store_id which has the cheapest item.
Example output:
id | store_id | item | price | MIN(price)
1 | 1 | 1 | 1.25 | 1.25
2 | 1 | 2 | 0.99 | 0.99
4 | 3 | 3 | 3.25 | 3.25

Related

Get cheapset prices in price table

I have a price table. I want to list the cheapest price of the products with the same IDs in my table. How can I do it?
Table Name : prices
userID| productsID | price | stock | maks
-------------------------------------------
1 | C120221 | 100 | 3 | 1
2 | C120221 | 200 | 5 | 5
3 | BR120221 | 500 | 7 | 3
4 | BR120221 | 600 | 9 | 0
5 | BR120221 | 700 | 11 | 2
SQL
SELECT
MIN(price ) AS price ,
GROUP_CONCAT(userID) AS userID
FROM prices
WHERE price > 0
GROUP BY productsID
ORDER BY price ASC
In this case I want to list all the info of the user with the cheapest price. In the current query, I can get the data in the productsID and price fields, but I cannot get the data in the userID, stock and max fields of the user with these data. Where is the problem?
You are probably trying to find the line or lines that contain the lowest price.
SELECT
prices.*
FROM
(
SELECT
MIN(price) AS price,
productsID
FROM
prices
WHERE
price > 0
GROUP BY
productsID
) AS t1
JOIN prices ON prices.price = t1.price
AND prices.productsID = t1.productsID

Filter all records based on a flag in the relational table

OK so I have two tables
products:
+------------+----------------+
| product_id | name |
+------------+----------------+
| 1 | A product here |
+------------+----------------+
| 2 | B product here |
+------------+----------------+
and sellers
+------------+----------------+------------+
| seller_id | in_stock | product_id |
+------------+----------------+------------+
| 1 | 1 | 1 |
+------------+----------------+------------+
| 2 | 0 | 1 |
+------------+----------------+------------+
| 2 | 0 | 2 |
+------------+----------------+------------+
I'm trying to figure out how to return only products where all sellers have in_stock = 0.
So in this particular example it would only return the product with product_id of 2 because product_id has one row with in_stock=1
Well the maximum of the in_stock column for this product has to be 0:
SELECT
product_id
FROM
sellers
GROUP BY
product_id
HAVING
MAX(in_stock) = 0;
So you get the product info with a join
SELECT
*
FROM
products
INNER JOIN (
SELECT
product_id
FROM
sellers
GROUP BY
product_id
HAVING
MAX(in_stock) = 0
) t
ON
products.product_id = t.product_id;
I would use the below to account for products not sold by any sellers (essentially equating to not being in stock), as well as those products that are sold by sellers but which are all out of stock.
select product_id
from sellers
group by product_id
having max(in_stock) = 0
union all
select p.product_id
from products p
left join sellers s
on p.product_id = s.product_id
where s.product_id is null

make the price of one line the price of all lines

I just started using mysql for SAP.
I would like to get this result:
DocEntry Description Price
00001 item A 100.00
00003 item C 110.00
From this data
DocEntry Description Quantity Price
00001 item A 2 300.00
00001 freight - 100.00
00002 item A 1 300.00
00003 item C 1 300.00
00003 freight - 110.00
I tried to filter the items with freight through this:
create view table_a
as
select DocEntry, Description, Price
from inv1 where Description like 'freight'
create view table_b
as
select DocEntry, Description, Price
from inv1
where exist (select * from table_a where table_a.DocEntry = table_b.DocEntry
I don't know though what to do with price?
Thanks!
It looks like you want the freight fit each item:
select distinct a.DocEntry, a.Description, b.Price
from inv1 a
join inv1 b on a.DocEntry = b.DocEntry
where a.Description != 'freight'
and b.Description = 'freight'
although the data does not require it, the distinct keyword has been added in case there are multiple "item" rows for each "freight" row. If the is no possibility of that ever happening, distinct may be removed.
It's not quite clear what you're asking, but maybe something like this...
DROP TABLE IF EXISTS my_table;
CREATE TABLE my_table
(DocEntry INT NOT NULL, Description VARCHAR(12) NOT NULL, Quantity INT NULL,Price INT NOT NULL,PRIMARY KEY(DocEntry,Description));
INSERT INTO my_table VALUES
(1,'item A',2,300),
(1,'freight',NULL,100),
(2,'item A',1,300),
(3,'item C',1,300),
(3,'freight',NULL,110);
mysql> SELECT * FROM my_table;
+----------+-------------+----------+-------+
| DocEntry | Description | Quantity | Price |
+----------+-------------+----------+-------+
| 1 | freight | NULL | 100 |
| 1 | item A | 2 | 300 |
| 2 | item A | 1 | 300 |
| 3 | freight | NULL | 110 |
| 3 | item C | 1 | 300 |
+----------+-------------+----------+-------+
SELECT docentry
, MAX(CASE WHEN description = 'freight' THEN price END) price
, MAX(CASE WHEN quantity IS NOT NULL THEN description END) description
FROM my_table
GROUP
BY docentry
HAVING price IS NOT NULL;
+----------+-------+-------------+
| docentry | price | description |
+----------+-------+-------------+
| 1 | 100 | item A |
| 3 | 110 | item C |
+----------+-------+-------------+

MySQL subtract from isolated subquery

I have a table containing inventory
ID | Product ID | In_Transit | Quantity | Cumulative Quantity
=====+================+==============+==============+====================
1 | 1 | 0 | 1000 | 1000
2 | 1 | 0 | 1 | 1001
3 | 1 | 1 | 54 | 1055
4 | 1 | 1 | 1 | 1056
So the total inventory for product id 1 is '1056' I get this using a SELECT MAX(ID) subquery join with the table to get its cumulative quantity which is 1056.
I would like to get the Inventory total (subtracting all the amounts in transit)
So 1056 - 54 - 1 = 1001
How would I get this in one query so i get
Product ID | Total Inventory | Inventory on Hand (Excluding in Transit |
===========+=================+=========================================
1 | 1056 | 1001
Also i need to use the cumulative inventory to get the total as opposed to 'SUM', except for summing those in transit because (those not in transit) have a large number of records and they take ages to SUM. I can use it to sum those in transit because there are far fewer records
SELECT
a.product_id
, a.cumulative as total_inventory
, a.cumulative - COALESCE(b.quantity,0) AS inventory_on_hand
FROM table1 a
JOIN
( SELECT MAX(id) AS max_id
FROM table1
GROUP BY product_id
) m ON (m.max_id = a.id)
LEFT JOIN
( SELECT product_id, SUM(quantity)
FROM table1
WHERE in_transit = 1
GROUP BY product_id
) b ON (a.product_id = b.product_id)

Calculate price with mysql query

gurus! I am stuck. Catalog items have prices dependent to its quantity. Here example of tables:
items: just item definitions
-------------------------
item_id | item_title
-------------------------
1 | "sample item"
2 | "another item"
items_prices: prices dependent to item quantity.
Less price taken for more quantity of item
----------------------------
item_id | quantity | price
----------------------------
1 | 1 | 100
1 | 5 | 80
1 | 10 | 60
2 | 1 | 120
2 | 3 | 100
cart
-------------------
item_id | quantity
-------------------
1 | 20
2 | 2
Is it possible to get current cart cost with single query?
select sum(x.totalcost)
from (
select c.item_id, c.quantity * ip.price as totalcost
from cart c
join items_prices ip
on c.item_id = ip.item_id
left join items_prices ip2
on ip2.quantity > ip.quantity
and c.quantity >= ip2.quantity
where c.quantity >= ip.quantity
and ip2.quantity is null
) x
The join back onto items_price again lets us filter out any cases where there is a greater quantity which still meets our criteria. This should be getting close to what we're after