I need to write a statement using min and max together.
SELECT companyname FROM companies JOIN stocklist
USING (companyid) where price =some(
( select max(price) from stocklist) , ( select min(price) from stocklist) ) ;
I need to get the companies name for the most low and max value but this isn't working (because of the last line) what is wrong and how should I do that?
UPDATE:
select companyname from stockList join companies using (companyid) WHERE price IN (select min(price) from stocklist) ;
this is working for me but gets only one operand, how can I get two?
I don't like the idea of having two subqueries in an IN clause. Here is another way with the join being made explicit:
SELECT companyname
FROM companies c JOIN
stocklist s
c.companyid = s.companyid join
(select min(price) as minprice, max(price) as maxprice
from stocklist
) sm
on s.price = minprice or s.price = maxprice
Try this:
SELECT companyname FROM companies c
INNER JOIN stocklist s
ON c.companyid = s.companyid
WHERE price IN
((SELECT MAX(price) FROM stocklist ),
(SELECT MIN(price) FROM stocklist ))
SEE THIS FIDDLE
Related
SELECT avg(Product.product_price)
From product
Where (
SELECT from customer
customer.city = "Tucson"
and Customer.cust_id = orders.cust_id
and Product.product_id = Orderline.product_id
)
group by product_name
UNION
SELECT sum(product.product_price)
From product
Where (
SELECT from customer
customer.city = "Tucson"
and Customer.cust_id = orders.cust_id
and Product.product_id = Orderline.product_id
)
group by product_name
I'm trying to display the average order from customers who order from tucson and the sum of the products going to tuscon
I have these tables with these (rows )orders (order_id, order_date, cust_id), product (product_id, product_name, product_price), orderLine (order_id, product_id, quantity), customer (cust_id, cust_name, street, city, state, zip) I need to 8. Show the average price and total price of products bought by customers from ‘Tucson’(Use Union) –
The problem is not with UNION - it's with the two queries you're attempting to UNION together. It looks to me like the AVG query should be something like:
SELECT avg(p.product_price)
From product p
INNER JOIN orderline ol
ON ol.product_id = p.product_id
INNER JOIN orders o
ON o.??????? = ol.???????
INNER JOIN customer c
ON c.cust_id = o.cust_id
WHERE c.city = 'Tuscon'
What's not clear from the code you posted is how the orders and orderline tables are to be joined - that is, what the common field(s) are in those tables. You'll need to fill that in.
Make similar changes to your SUM query.
my query is
SELECT productscrapeddatalog.*, product.productname
FROM productscrapeddatalog JOIN product
ON productscrapeddatalog.productID = product.productID
WHERE price = (SELECT MIN(price) FROM productscrapeddatalog ORDER BY productID)
this is my table screenshot
Your approach with a join and filtering in the where clause is ok - but you need to correlate the subquery with the outer query so you get the lowest price per product rathern thatn the overall min:
SELECT l.*, p.productname
FROM productscrapeddatalog l
JOIN product p ON l.productID = l.productID
WHERE l.price = (
SELECT MIN(l1.price)
FROM productscrapeddatalog l1
WHERE l1.productID = t.productID
)
If you are running MySQL 8.0, you can also do this with window functions:
SELECT l.*, p.productname
FROM product p
JOIN (
SELECT l.*, RANK() OVER(PARTITION BY productID ORDER BY price) rn
FROM productscrapeddatalog l
) p ON l.productID = l.productID AND l.rn = 1
You should try this:
SELECT productscrapeddatalog.*, product.productname, MIN(productscrapeddatalog.price)
FROM productscrapeddatalog JOIN product
ON productscrapeddatalog.productID = product.productID
ORDER BY productID
I have 2 MySQL tables:
Persons:
person_id
person_name
Orders:
person_id
cost
order_name
I need to make a ONE sql-query to get person name and it's cheapest order.
Want to write some like this: SELECT person_name, order=(SELECT order_name FROM orders WHERE order.person_id = person.person_id ORDER BY order.cost ASC LIMIT 1) FROM person
But it don't work.
I would use a correlated query. There are many ways to do it: as a subquery in the SELECT clause, or as a subquery in the WHERE clause.
In this case, I implemented the second option:
SELECT
person_name,
order_name
FROM Persons
INNER JOIN Orders ON Persons.person_id = Orders.person_id
WHERE
(
cost = (
SELECT MIN(cost)
FROM Orders
WHERE
(Orders.person_id = Persons.person_id)
)
)
remove order= and add AS order_name
Something like this:
SELECT p.person_name
, ( SELECT o.order_name
FROM orders o
WHERE o.person_id = p.person_id
ORDER BY o.cost ASC
LIMIT 1
) AS order_name
FROM person p
ORDER BY p.person_name
SELECT person_name, order_id, order_name, MIN(cost) as cost
FROM persons
INNER JOIN orders
ON persons.person_id = orders.person_id
GROUP BY person.person_name, order_id, order_name;
You can do this just by joining your 2 tables and use MIN.
I have a below price table
required each product have a multiple prices in single row
my sql query is
select a.out_of_stack, product.*,
( select min(price) from temp_attr
where product_id =product.product_id
) as min_price ,
( select max(price) from temp_attr
where product_id = product.product_id
) as max_price from product
left join users a
on a.user_id = product.user_id
where product.category_id in (37903,4707)
and product.product_status in ('0')
and product.draft=0
and a.active=0
and product.product_close=0
and product.price between 10 and 4000
group by product.product_id
i need to get the product when search using price with temp_attr table.
Find Max/Min prices for products in temp_attr :
SELECT MIN(price) , MAX(price) from temp_attr group by product_id
Join them with product and user tables:
SELECT a.out_of_stack , product.*, t1.* FROM
product
left join (SELECT MIN(price) , MAX(price) from temp_attr group by product_id ) as t1
on t1.product_id = product.product_id
left join users as a
on a.user_id = product.user_id
where product.category_id in (37903,4707)
and product.product_status in ('0')
and product.draft=0
and a.active=0
and product.product_close=0
and product.price between 10 and 4000
group by product.product_id
I try to get a list of products with each newest and lowest offer price
Table product:
id | name
Table offer:
id | product_id | price | created | dealer_id
Table invalids:
id | offer_id | status
I have tried:
SELECT * FROM product INNER JOIN
(
SELECT offer.product_id , offer.price
FROM offer
LEFT JOIN invalids
ON offer.id = invalids.offer_id
WHERE invalids.id IS NULL
GROUP BY offer.dealer_id
ORDER BY offer.created DESC
) o
ON o.product_id = product.id
ORDER BY product.name
I have tried an sqlfiddle http://sqlfiddle.com/#!9/32658/3 with this offer values:
(`id`, `price`, `dealer_id`, `product_id`, `created`)
(1,12.60,1,1,'2015-05-17 08:44:45'),
(2,13.00,1,1,'2015-08-17 08:44:45'),
(3,20.00,1,1,'2015-08-17 08:45:30'),
(4,10.00,1,1,'2015-08-17 08:45:46'),
(5,4.00,2,1,'2015-05-17 08:44:11'),
(6,11.00,2,1,'2015-08-17 08:44:46'),
(7,5.00,2,1,'2015-08-17 08:45:31'),
(9,110.00,2,2,'2015-08-17 08:46:58'),
(10,11.00,2,2,'2015-08-17 08:47:12');
Expected value for product ID 1 is offer ID 7 with price 5.
These steps I think I must realize:
Order offers by created and group by dealer_id to get newest entries
Take result from step 1 and order it by price to get smallest price.
Make this for all products
Maybe I must use a second SELECT FROM offer with GROUP BY and ORDER BY but how do I get I the product_id from the first (outer) select?
Well I would start by getting the latest date for each product offer like this:
SELECT product_id, MAX(created) AS latestOffer
FROM offer
GROUP BY product_id;
Once you have that, you can join it to the original table to get that offer:
SELECT o.*
FROM offer o
JOIN(
SELECT product_id, MAX(created) AS latestOffer
FROM offer
GROUP BY product_id) tmp ON tmp.product_id = o.product_id AND tmp.latestOffer = o.created;
Here is an SQL Fiddle example.
This query should help you:
SELECT *
FROM product
JOIN (
SELECT product_id, min(price) as minPrice, max(created) as newestOffer
FROM offer
WHERE id NOT IN (SELECT offer_id FROM invalids)
GROUP BY 1
) as b
ON product.id = b.product_id
A shot in the dark based on what I understand you to be after...
lots of nested subqueries.. keep thinking there's got to be a better way...
SELECT OO.ID, OO.Price, OO.Dealer_Id, OO.Product_ID, OO.created, P.name
FROM Offer OO
INNER JOIN (
SELECT Min(Price) as MinP
FROM offer O
INNER JOIN (
SELECT max(OI.created) as LatestOffer, OI.Dealer_ID, OI.Product_ID
FROM Offer OI
LEFT JOIN invalids I
on OI.Id = I.offer_Id
WHERE I.ID is null
GROUP BY OI.Dealer_Id, OI.Product_Id
) B
on O.Dealer_Id = B.Dealer_Id
and O.Product_Id = B.Product_Id
and O.Created = B.LatestOffer
) Z
on OO.Price = Z.MinP
INNER JOIN product P
on P.ID = OO.Product_ID
SQL FIDDLE