简体   繁体   English

MS Access选择不同的行

[英]MS Access Selecting distinct rows

I have table similar to this: 我有与此类似的表:

ID ProductName Price
1  Water       0.89
1  Water       0.99
1  Water       0.79
2  Coke        1.99
3  Sprite      1.99

What I would like is to get is the lowest price of every product. 我想要得到的是每种产品的最低价格。 ( ID can't change for same name ) If I could group just by one column it would be fine but I can't since Access doesn't let me. (ID不能更改为相同的名称)如果我只能按一列分组,那很好,但是由于Access不允许我,所以不能。 My current code that I've been trying to deal with is: 我一直在尝试处理的当前代码是:

SELECT DISTINCT Products.ProductName, Products.Price
FROM Products
GROUP BY Products.ProductName, Products.Price

Information that I would like to get should look like: 我想获取的信息应如下所示:

ProductName Price
Water       0.79
Coke        1.99
Sprite      1.99

Just use an aggregation on the product name (or id): 只需对产品名称(或ID)使用汇总:

SELECT Products.ProductName, MIN(Products.Price) as Price
FROM Products
GROUP BY Products.ProductName;

You are very close to it. 您非常接近。 Just use min() function: 只需使用min()函数:

SELECT Products.ProductName, min(Products.Price)
FROM Products
GROUP BY Products.ProductName;
SELECT P.ProductName, 
       MIN(P.Price) as Price
FROM Products p
GROUP BY P.ProductName

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM