Showing Distinct Values with Aggregates

走远了吗. 提交于 2019-12-25 18:32:13

问题


I have a table for recording daily price from different suppliers. My goal is to find the best (low price) supplier. The table structure is Table Name: lab1 Columns: ID, Product_ID, Price_date, Price, Supplier

-----------------------------------------------------------------------------------
ID  Product_ID  Price_date  Price   Supplier
--------------------------------------------------------------------------------------
1   8           26-10-2014  1300    SP1
2   8           05-10-2014  1600    SP2
3   8           15-10-2014  1300    SP1
4   8           14-12-2014  1200    SP3
------------------------------------------------------------------------------------------

Create Table Structure

CREATE TABLE clickpic_pricecompare.lab1 (
  ID int(11) NOT NULL AUTO_INCREMENT,
  Product_ID int(11) DEFAULT NULL,
  Price_Date date DEFAULT NULL,
  Price decimal(19, 2) DEFAULT NULL,
  Supplier varchar(255) DEFAULT NULL,
  PRIMARY KEY (ID)
)
ENGINE = MYISAM
COMMENT = 'testing-purpose';

INSERT INTO  lab1(ID, Product_ID, Price_Date, Price, Supplier) VALUES
(1, 8, '2014-10-26', 1300.00, 'SP1');
INSERT INTO  lab1(ID, Product_ID, Price_Date, Price, Supplier) VALUES
(2, 8, '2014-10-05', 1600.00, 'SP2');
INSERT INTO  lab1(ID, Product_ID, Price_Date, Price, Supplier) VALUES
(3, 8, '2014-10-15', 1300.00, 'SP1');
INSERT INTO  lab1(ID, Product_ID, Price_Date, Price, Supplier) VALUES
(4, 8, '2014-10-14', 1200.00, 'SP3');

I NEED THE RESULT LOOKS LIKE BELOW

--------------------------------------------------------------------------------------
ID  Product_ID  Month   Price   Supplier
--------------------------------------------------------------------------------------
4   8           October 1200    SP3
-------------------------------------------------------------------------------------------

Please help...


回答1:


You can use self join with the product id and minimum amount of price to get the lowest price row per product id

select l.ID,
l.Product_ID,
monthname(l.Price_Date) `Month`,
l.Price,
l.Supplier
from lab1 l
join (select Product_ID,min(Price) Price
     from lab1
     group by Product_ID) l1
using(Product_ID,Price)

DEMO




回答2:


select temp2.id,
       temp2.Product_ID,
       DATENAME(month, temp2.Price_Date) AS MONTH,
       temp1.Min_Price,
       temp2.Supplier
from
(
     select Product_ID, min(Price) as Min_Price
     from lab1
     group  by Product_ID
) as temp1
inner join
lab1 temp2
on temp1.Product_ID = temp1.Product_ID
and temp1.Min_Price = temp2.Min_Price



回答3:


I think you are looking for:

select l.ID, l.Product_ID, monthname(l.Price_Date) as Month, l.Price, l.Supplier

from lab1 l join
     (select Product_ID, year(l.Price_date) as yr, month(l.Price_Date) as mon, min(Price) as Price
      from lab1
      group by Product_ID, year(l.Price_date), month(l.Price_Date)
     ) lmin
     on l.Product_id = lmin.Product_id and
        year(l.Price_Date) = lmin.yr and
        month(l.Price_Date) = lmin.mon;

If you want data only for October, then add a where clause:

where l.Price_Date >= '2014-10-01' and l.Price_Date < '2014-11-01'


来源:https://stackoverflow.com/questions/26570744/showing-distinct-values-with-aggregates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!