I'm new to SQL, so I've got some troubles with creating queries.
My task is: To select a description of a product which was selling the most in 1989 with maximum discount.Product Table. Price table. What I tried to do is
Select maximum discount by subtracting list_price - min_price
select max(list_price - min_price) from PRICE
Select description
select description from product
join price on PRODUCT.product_id = PRICE.product_id
where start_date = '1989'
The problem is that I can't make it in one query
select top 1
product.product_id,
description,
max_disc = max(list_price - min_price)
from PRICE
join product on product.product_id = PRICE.product_id
where start_date = '1989'
group by product.product_id, description
order by max_disc desc
Inner Join between the two tables using product_id, and calculate ist_price - min_price AS discount
Filter out the resultset to carry prices of 1989 only, using where YEAR(start_date) = 1989
Consider the resultset as a Derived Table, and sort it using discount value in descending order. Use Limit 1 to find the product with maximum discount given in the year 1989.
For MySQL, try the following:
SELECT dt.*
FROM (
SELECT
pt.product_id,
pt.description,
pe.list_price - pe.min_price AS discount
FROM PRODUCT AS pt
JOIN PRICE AS pe ON pt.product_id = pe.product_id
WHERE YEAR(pe.start_date) = 1989
) AS dt
ORDER BY dt.discount DESC LIMIT 1
you try like below
select * from
(
select description from product
join price
on PRODUCT.product_id = PRICE.product_id
where start_date = '1989'
) t1
cross join (select max(list_price - min_price) as p from PRICE) t
Related
I almost finished my system until I noticed that a product had a different sku with another supplier (I have more than 15,000 products in the table table_prices from 7 different suppliers, it is assumed that they all have to handle the same SKU (but they don't), I came up with adding sku2 to solve, but I got into a black hole.
Now taking advantage of the fact that I can get help from you, I would like to be able to search in the table table_prices for the lowest price if the sku, sku2, upc or ean matches table_products, in this way I will not have a major problem if a supplier decides to put a bad sku since I will have more information to relate.
I leave a link with what I have working ... I look for sku of table_products and I get the lowest price of table_prices
http://sqlfiddle.com/#!9/47809f/1/0
I hope to get $90 on product 1
SELECT p.*, x.supplier AS supplier, x.price , x.quantity AS quantity
FROM table_prices x
JOIN
(SELECT sku,
MIN(price) price
FROM table_prices
WHERE quantity != 0 AND active = 1
GROUP BY sku) y
ON y.sku = x.sku
AND y.price = x.price
JOIN table_products p
WHERE p.sku = x.sku
ORDER BY category, price
This seems to give the desired results:
SELECT p.*, x.supplier AS supplier, x.price , x.quantity AS quantity
FROM table_prices x
JOIN table_products p ON (p.sku = x.sku OR p.sku2 = x.sku OR p.upc = x.upc OR p.ean = x.ean)
WHERE x.price = (SELECT MIN(x2.price) FROM table_prices x2 WHERE (p.sku = x2.sku OR p.sku2 = x2.sku OR p.upc = x2.upc OR p.ean = x2.ean))
ORDER BY category, price
I have two tables: products and prices
products
id (PK)
name
prices
id (PK)
product_id (FK > products)
price
originalPrice
Each product might have multiple prices. What I want to achieve is a query that returns me all products on-sale with its cheapest price.
on-sale = price < originalPrice
if a product is not on-sale, it should not be included in the results
if a product has multiple prices that qualify for on-sale, only return the cheapest price.
The resulting table should have these columns
products.id
products.name
prices.id
prices.price
prices.originalPrice
With my attempts I'm ending up with this issue: #1055 - Expression #3 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'tbl.price' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by. Please note that I cannot change the config.
MySQL version: 5.7.22
I have uploaded a SQL export with sample data here: https://www.dropbox.com/s/6ucdv6592dum6n6/stackoverflow_export.sql?dl=0
select pro.name, MIN(pri.price) from products pro
inner join price pri on pri.product_id = pro.id
where pri.price < pri.originalPrice
group by pro.name
heres a shot without any data :p may need a little tweaking
Try this:
SELECT *
FROM `products` pro
JOIN price pri on pri.productId = pro.id
WHERE pri.price < pri.originalPrice
AND pri.price =
(
SELECT min(p.price)
FROM price p
WHERE p.productId = pro.id AND p.price < p.originalPrice
)
Hope this works for you
SELECT *,MIN(price) FROM (
SELECT name, products.id,price
FROM products
INNER JOIN productItems
ON products.id = productItems.productId
WHERE price < originalPrice
ORDER BY (price-originalPrice)
) as tbl GROUP BY id;
OR
SELECT *,MIN(diff) FROM (
SELECT name, products.id,price,(price-originalPrice) as "diff"
FROM products
INNER JOIN productItems
ON products.id = productItems.productId
WHERE price < originalPrice
ORDER BY products.id,(price-originalPrice)
) as tbl GROUP BY id;
This works with that dropbox link you gave: http://sqlfiddle.com/#!9/a6306d/3
select pro.name, MIN(pri.price) from products pro
inner join price pri on pri.productId = pro.id
where pri.price < pri.originalPrice
group by pro.name
I have 3 tables :
I need to retrieve the name of the sellers that have sold during January AT LEAST one product where the total amount of sales of this said product during January is greater than 1000.
I thought about starting like this :
SELECT c.nom, c.prenom
FROM Commerciaux c
LEFT JOIN Ventes v ON c.id_commerciaux = v.id_commerciaux
WHERE EXISTS (SELECT 1
FROM Produits p, Ventes v
WHERE p.id_produits = v.id_produits
AND MONTH(v.date) = 1
GROUP BY p.nom
HAVING SUM(v.montant) > 200)
AND MONTH(v.date) = 1
GROUP BY c.nom, c.prenom
The SELECT in the WHERE EXISTS seems to be working, but when I have to get link the table Sellers, I don't know how to write it.
Any help would be great !
You can use something like this:
select distinct s.name
from Sellers s,
( -- get all those products that qualify (more than 1000 sales)
select product_id, sum(amount) as total
from Sales
where Month(date) = 1
group by product_id
having total > 1000
) vp,
Sales sa
where
s.id = sa.commercial_id and
sa.product_id = vp.id and
Month(sa.date) = 1
#Leo answer would return only those Sellers that have sold more than 100 of the product in January instead of returning all Sellers that have sold any amount of the product that has been sold in an amount greater than 1000 across the board.
You could use a nested query:
SELECT s.name FROM
(Sellers as s JOIN Sales as sp on sp.commercial_id = s.id) JOIN
Product as p on p.id = sp.product_id
WHERE MONTH(sp.date) = 1 AND YEAR(sp.date) = 2017 AND
(SELECT SUM(Amount) FROM Sales as sp2 WHERE sp2.commercial_id = s.id
AND sp2.product_id = p.id
AND MONTH(sp2.date) = 1 AND YEAR(sp2.date) = 2017) > 1000
First, join the three tables on foreign keys, and then run a nested query to compute amount of the selected seller on a specific product to check to pass the amount limitation.
try this
SELECT C.name
FROM Sales A
JOIN Product B ON A.produit_id = B.id
JOIN Seller C ON A.commercial_id = C.id
WHERE MONTH(A.date) = 1
HAVING SUM(A.nAmount) > 100
GROUP BY C.name
Below are my tables
properties bookings
========== ==========
property_id booking_id
price property_id
city checkin_date
checkout_date
I have two tables, Properties and Bookings for a rental site. I want to do a search by check in date, check out date, price and location. It will check the availability from the foreign table bookings via the property_id FK.
Basically I want it to be something like:
SELECT property_id,price,city FROM properties
WHERE
price > 200,
city = "Toronto",
LEFT JOIN (
SELECT postid, COUNT( * ) AS total
FROM bookings
WHERE checkin_date *** SOMEHTING HERE ****
I am sure the above is incorrect already. Whether if I am to use left join or inner join.
SELECT DISTINCT p.property_id, p.price, p.city
FROM properties AS p
LEFT JOIN bookins AS b ON p.property_id = b.property_id
WHERE p.price > 200
AND p.city = 'Toronto'
AND (b.checkin_date >= '?' OR b.checkout_date <= '?')
replace ? with your search date
I'm asking for an awful lot here - but maybe some SQL guru can show me how to extract the data I want and save me 10+ hours of google-time(tm)?
These are my tables, with only relevant fields displayed:
**event**
id
cust_id
....
.
**art**
id
art_name
...
.
**event_art**
event_id
art_id
...
.
**price**
cust_id
art_id
price
...
Prices in the "price" DB with user ID "0" is standard price, if an entry exists with art_id and cust_id that is customer specific price for that article.
What I have is cust_id and what I have for output now is just the customer specific prices with SQL:
SELECT * FROM price WHERE cust_id='{$custID}'
But I'd like to include prices for previously ordered articles, even if they do not have a customer specific price.
So what I need is to:
1 Get all id's from table event where cust_id = custID
2 Get all distinct article ID's on those orders from table event_art
3 Output "id" and "art_name" of article from "art" and "price" from price table using custID or 0 for standard price if no entry exists.
To me this sounds like a multi-line JOIN that's a bit outside my scope of SQL knowledge. Could somebody help me out, point me to a guide that deals with similar issues or... well, something?
Thanks in advance!
SELECT art_id, price
FROM price
WHERE cust_id = $cust_id
UNION ALL
SELECT art_id, price
FROM (
SELECT DISTINCT art_id
FROM event e
JOIN event_art ea
ON ea.event_id = e.id
WHERE e.cust_id = $cust_id
AND ea.art_id NOT IN
(
SELECT art_id
FROM price
WHERE cust_id = $cust_id
)
) e
JOIN price p
ON p.cust_id = 0
AND p.art_id = e.art_id
Make sure that (cust_id, art_id) (in this order) is a PRIMARY KEY or a UNIQUE INDEX on price.
Had to make some small changes to indicate which table was used where in the SQL, but pretty much copy&paste so not bad at all :P
SELECT price.art_id, price.price
FROM price
WHERE cust_id =114
UNION ALL
SELECT e.art_id, p.price
FROM (
SELECT DISTINCT art_id
FROM event e
JOIN event_art ea ON ea.event_id = e.id
WHERE e.cust_id =114
AND ea.art_id NOT
IN (
SELECT price.art_id
FROM price
WHERE cust_id =114
)
)e
JOIN price p ON p.cust_id =0
AND p.art_id = e.art_id
SELECT DISTINCT
a.id,
a.art_name,
COALESCE(p.price, p0.price) AS price
FROM event e
INNER JOIN event_art ea ON e.id = ea.event_id
INNER JOIN art a ON ea
LEFT JOIN price p ON p.art_id = a.id AND p.cust_id = e.cust_id
LEFT JOIN price p ON p.art_id = a.id AND p.cust_id = 0