This is the current SQL query I am working with:
SELECT Merchant.Product, Merchant.Name, Merchant.Price
FROM a_table AS Merchant
JOIN
(
SELECT Product, MIN(Price) AS MinPrice
FROM a_table
GROUP BY Product
) AS Price
ON Merchant.Product = Price.Product
AND Merchant.Price = Price.MinPrice
From this data set:
Product Name Price
11 Merch1 19.00
11 Merch2 20.00
11 Merch3 19.00
11 Merch4 19.50
12 Merch1 20.00
12 Merch2 20.00
13 Merch1 17.00
13 Merch3 15.00
The current SQL outputs multiple product records when prices are the same like this:
Product Name Price
11 Merch1 19.00
11 Merch3 19.00
12 Merch1 20.00
12 Merch2 20.00
13 Merch3 15.00
I want to Group By product and display the lowest price with corresponding row data. If two prices are the same on a product, use first record found.
Trying to get this result:
Product Name Price
11 Merch1 19.00
12 Merch1 20.00
13 Merch3 15.00
You don't need any joins to do this.
If you are looking to get the min price for every product by merchant you can do this:
SELECT Product, Name, MIN(Price) as MinPrice
FROM a_table
GROUP BY Product, Name
If you just want the min price of a product regardless of merchant you can do this:
SELECT Product, MIN(Price) as MinPrice
FROM a_table
GROUP BY Product
Here is the query that finally worked for my needs...
http://sqlfiddle.com/#!9/c8937c/35/0
SELECT emp2.product,
emp1.name,
emp2.MinPrice
FROM (
SELECT product,
Min(price) as MinPrice
FROM Merchant
GROUP BY product
) as emp2 JOIN Merchant as emp1 ON emp1.price = emp2.MinPrice
GROUP BY product;
Related
I have this records in products table with product_id and its price
product_id
price
1
150
1
190
2
20
2
12
3
123
4
513
5
157
5
147
and I want to get the top 3 products and arrange it by average price something like this
product_id
price
avg_price
4
513
513
1
150
170
1
190
170
5
157
152
5
147
152
how to write/code it in sql query or laravel eloquent query?
WITH AverageCTE AS
(
SELECT product_id, AVG(avg_price) as avg_price
FROM products
GROUP BY product_id
)
SELECT p.product_id, price, avg_price
FROM product p JOIN
(SELECT * FROM AverageCTE ORDER BY avg_price DESC LIMIT 3) a
on p.product_id = a.product_id
ORDER BY avg_price DESC
I've come across a problem that I don't really know how to solve.
I have a table which looks somewhat like this:
ID Name Price Quantity
1 BookA 5 10
2 BookB 10 15
3 BookA 15 15
4 BookA 5 25
How could I join rows which have same Name, same Price and sum Quantity? So it would look like this:
ID Name Price Quantity
1 BookA 5 35
2 BookB 10 15
3 BookA 15 15
Thank you in advance!
This is just a simple GROUP BY query:
SELECT Min(ID), Name, Price, Sum(Quantity) as Quantity
FROM yourtable
GROUP BY Name, Price;
I have a transaction table that like this: quantity is the total quantity in stock based on different unit price. let's call it T
id | transaction_time | item | unit_price | quantity | subtotal
1 2012-5-15 A 1.00 15 15.00
2 2012-5-15 A 3.00 15 45.00
3 2012-5-15 B 1.00 10 10.00
4 2012-6-10 A 2.00 15 30.00
5 2012-6-15 A 2.00 10 20.00
I need to get the total value of each item in stock over time...however, same items are based on different unit price. The result for A for example is:
transaction_time | item | quantity | subtotal
2012-5-15 A 30 60.00
2012-6-10 A 45 90.00
2012-6-15 A 40 80.00
2012-5-15, we have 15 item A with price 1.00, 15 item A with price 3.00, so the total quantity is 30, subtotal is 15*1+15*3=60.
2012-6-10 we have 15 more item A with price 2, so the total quantity become 30+15=45, subtotal become 60+15*2=90
2012-6-15 we have 10 item A with price 2, so item A with price 2 goes down from 15 to 10. the total quantity become 40, and the subtotal goes down -2*5, which become 80.
I tried
select transaction_time,sum(quantity),sum(subtotal)
where id in(select max(id) from T group by unit_price,item)
group by item
having item=A
This only gives me the last line
2012-6-15 A 40 80.00
You need first to identify all possible unit_price values for the specific item:
SELECT DISTINCT unit_price
FROM t
WHERE item = 'A'
Output:
unit_price
----------
1
3
2
You also need to identify all possible transaction_times:
SELECT DISTINCT transaction_time
FROM t
WHERE item = 'A';
Output:
transaction_time
----------------
2012-05-15
2012-06-10
2012-06-15
Now perform a CROSS JOIN between the above two sets
SELECT *
FROM (
SELECT DISTINCT transaction_time
FROM t
WHERE item = 'A') AS times
CROSS JOIN (
SELECT DISTINCT unit_price
FROM t
WHERE item = 'A') AS up
ORDER BY times.transaction_time
to get:
transaction_time unit_price
----------------------------
2012-05-15 3
2012-05-15 2
2012-05-15 1
2012-06-10 3
2012-06-10 2
2012-06-10 1
2012-06-15 1
2012-06-15 3
2012-06-15 2
Now use the above and perform a correlated subquery to get unit_price per transaction_time from item 'A':
SELECT transaction_time, unit_price,
(SELECT quantity
FROM t
WHERE t.item = 'A'
AND t.unit_price = up.unit_price
AND t.transaction_time <= times.transaction_time
ORDER BY transaction_time DESC LIMIT 1) AS quantity
FROM (
SELECT DISTINCT transaction_time
FROM t
WHERE item = 'A') AS times
CROSS JOIN (
SELECT DISTINCT unit_price
FROM t
WHERE item = 'A') AS up
ORDER BY times.transaction_time
Output:
transaction_time unit_price quantity
----------------------------------------
15.05.2012 00:00:00 1 15
15.05.2012 00:00:00 3 15
15.05.2012 00:00:00 2 NULL
10.06.2012 00:00:00 1 15
10.06.2012 00:00:00 3 15
10.06.2012 00:00:00 2 15
15.06.2012 00:00:00 1 15
15.06.2012 00:00:00 3 15
15.06.2012 00:00:00 2 10
The final result is simply a matter of performing a GROUP BY on the above:
SELECT transaction_time,
'A' AS item,
SUM(quantity) AS quantity,
SUM(quantity*unit_price) AS subtotal
FROM (
SELECT transaction_time, unit_price,
(SELECT quantity
FROM t
WHERE t.item = 'A'
AND t.unit_price = up.unit_price
AND t.transaction_time <= times.transaction_time
ORDER BY transaction_time DESC LIMIT 1) AS quantity
FROM (
SELECT DISTINCT transaction_time
FROM t
WHERE item = 'A') AS times
CROSS JOIN (
SELECT DISTINCT unit_price
FROM t
WHERE item = 'A') AS up) AS x
GROUP BY transaction_time
Output:
transaction_time item quantity subtotal
----------------------------------------------
15.05.2012 A 30 60
10.06.2012 A 45 90
15.06.2012 A 40 80
Demo here
Following query(kind of complex, maybe slow, needs optimization) works, check DEMO
SELECT tr_sub.cur_tt, tr_sub.item, sum(tr.quantity), sum(tr.quantity*tr.unit_price)
FROM
(SELECT tr1.transaction_time as cur_tt, max(tr2.transaction_time) as prev_tt, tr1.item as item,
IF (tr1.unit_price=tr2.unit_price, tr1.unit_price, tr2.unit_price) as t_p
FROM transactions tr1 LEFT JOIN transactions tr2 ON
tr1.transaction_time>=tr2.transaction_time AND tr1.item=tr2.item
GROUP BY tr1.item, tr1.transaction_time, t_p
) as tr_sub INNER JOIN transactions tr ON
tr_sub.prev_tt=tr.transaction_time
AND tr_sub.item=tr.item
AND tr_sub.t_p=tr.unit_price
GROUP BY tr_sub.item, tr_sub.cur_tt
ORDER BY tr_sub.cur_tt, tr_sub.item
This is my table which store id with its qty and price. I would like to sum qty based on price. Sometimes the qty might be show 0 if qty is negative value. Thus, i will not show the price of qty fall in 0 value when group by price.
id | id_item | qty | price
1 1 10 1.00
2 1 15 2.00
3 1 10 1.00
4 2 5 2.00
5 2 5 2.50
6 3 10 1.00
7 3 10 1.00
8 3 5 1.00
This is what i have tried.
Select id_item, price, sum(qty) as total from sales group by id_item, price having total !=0;
Result
id_item | qty | price
1 20 1.00
1 15 2.00
2 5 2.00
2 5 2.50
3 10 1.00
Expected result with concat
id_item | qty | price
1 20,15 1.00,2.00
2 5,5 2.00.2.50
3 10 1.00
How can i achieve the result as shown?
I think you need two levels of aggregation:
select id_item, group_concat(total order by price) as quantities,
group_concat(price order by price) as prices
from (Select id_item, price, sum(qty) as total
from sales
group by id_item, price
having total <> 0
) s
group by id_item;
I have a table with those fields:
phID, ProductID
The phID is the pharmacy id and the ProductID is the product id.
One pharmacy has multiple products
1 product is to multiple pharmacies
Example:
phID ProductID
-----------------
1001 9
1001 10
1001 11
1004 9
1004 12
1004 14
1004 11
The query that I want is so I can get all the phID that has the same product.
I have this query:
SELECT phID, ProductID
FROM ph_pd
WHERE ProductID IN (9,10,11)
I want the results to be
1001 9
1004 9
1001 11
1004 11
or just
1001
1004
If you want to get distinct phID which are fall under the certain productID, you have to use DISTINCT...
SELECT distinct phID FROM ph_pd WHERE ProductID IN (9,10,11)
You can use :
SELECT p.phID , p.ProductID FROM ph_pd p
WHERE (select count(*) from ph_pd where ProductID = p.ProductID) >= 2
ORDER BY ProductID ;
SELECT phID
FROM tableName
WHERE productID IN (9,10,11)
GROUP BY phID
HAVING COUNT(DISTINCT productID) = 2 -- since there are two stores
SQLFiddle Demo