Find details for minimum price entry for each group of rows with the same article number

a 夏天 提交于 2019-12-23 23:16:08

问题


I read a lot about this, but none worked for me. Can someone help?

I have a big table with a lot of different articles (a lot with same EAN) and need always only the cheapest one (sort by price) with the correct AN:

*art   price   an    ean
*Test |79,00|15770|0808736558136
*Test |85,00|k3238|0808736558136
*Test |68,00|r4850|0808736558136
*Test |65,00|a1117|0808736558136
*Test |78,00|t8619|0808736558136

Expect this one:

*Test |65,00|a1117|0808736558136

回答1:


SELECT B.*
  FROM BigTable AS B -- Why do SQL questions omit the table names so often?
  JOIN (SELECT EAN, MIN(Price) AS Price
          FROM BigTable
         GROUP BY EAN
       ) AS P
    ON B.EAN = P.EAN AND B.Price = P.Price
 ORDER BY B.EAN;

The sub-query finds the minimum price for each EAN; the outer query finds the details that match the EAN and minimum price for that EAN. If there are two records with the same minimum price for a given EAN, both will be chosen.




回答2:


SELECT * FROM myTable ORDER BY price ASC LIMIT 1


来源:https://stackoverflow.com/questions/10700892/find-details-for-minimum-price-entry-for-each-group-of-rows-with-the-same-articl

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