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
Related
I have this table prices. Column categoy is type enum ['owner','tax','fees','misc'].
+--------------+--------------------+------------------+
| id | category | price |
+--------------+--------------------+------------------+
| 106 | tax | 0.20 |
| 609 | owner | -0.49 |
| 609 | tax | 1.20 |
| 609 | fees | 0.10 |
| 106 | fees | 2.20 |
| 106 | owner | -0.40 |
+--------------+--------------------+------------------+
I need to sort by having tax and fees at the top or fees and taxand those two sorted by lowest price first but sorted by id.
I want my result set to be
+--------------+--------------------+------------------+
| id | category | price |
+--------------+--------------------+------------------+
| 106 | tax | 0.20 |
| 106 | fees | 2.20 |
| 106 | owner | -0.40 |
| 609 | fees | 0.10 |
| 609 | tax | 1.20 |
| 609 | owner | -0.49 |
+--------------+--------------------+------------------+
Ive tried different queries but can't get the correct answer. How can I do this?
Something like order by id, field(category,'tax','fees'),price desc
You could try with CASE WHEN method
ORDER BY (CASE WHEN category IN ('tax','fees') THEN 0 ELSE 1 END) ASC, price ASC, id ASC
Ordering tax and fees first, others second, then orders by price ascending, also if same prices then orders by id in ascending order.
EDIT:
So you wish to be sorted by id, then tax and fees sorted first (and after that others), by price, then try this:
ORDER BY `id` ASC, (CASE WHEN category IN ('tax','fees') THEN 0 ELSE 1 END) ASC, price ASC
I've also tested the query and got your result:
if the enum is order as you need you could use
select id , category ,price
from my_table
order by id, category, price desc
otherwise you could use fiedld()
select id , category ,price
from my_table
order by id, field(category,'tax','fees', 'owner','misc'), price desc
or case
select id , category ,price
from my_table
order by id, case when category = 'tax' then 1
when category = 'fees' then 2
when category = 'owner' then 3
when category = 'misc' then 5
else 5 end A, price desc
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
I have a table
equipment : (id,quantity,cost_per_unit,category)
suppose the table containing
id | quantity |cost_per_unit| category
---+------------+-------------+----------
1 | 10 | 10 | dumples
2 | 7 | 15 | dumples
3 | 5 | 5 | fitness
I'm trying to get the category of equipment and the count of this category and the price of all pieces of this category
the output would be like :
category | count | price
---------+-------+------------------------
dumples | 17 | 205 // ( 10*10 + 7*15 )
fitness | 5 | 25
any help would be greatly appreciated
You can use sum and group by
select category,
sum(quantity) `count`,
sum(quantity * cost_per_unit) price
from table1
group by category
DEMO
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)
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