MySQL查询表中大多数价格的最低价格
我正在尝试构建一个查询,通过该查询可以提取表中的最低价格,该价格至少为表中所有价格的 10% 或更多。
我知道我可以使用 MIN(price) 来获取表中的最低价格。但是,我想使用查询向用户显示代表最低多数价格的表单价格。
这意味着它是公平的,并向用户表明它的价格是现实的。与可能只提供一次的一种产品相反,他们很可能永远不会获得这个价格。
价格表很简单。
产品 ID、价格。
它有数千个费率,我觉得使用 MIN 来设置最低费率是有误导性的。
我真的在寻找一些其他人如何解决这个问题的示例查询。是否可以在单个查询中实现?
I'm trying to build a query where by I can extract the lowest price in the table, where the price is at least 10% or more of all the prices in the table.
I understand I can use MIN(price) to get the bottom rate in the table. However, I want to use the query to display a form price to the user which represents the lowest majority price.
This means it's fair, and demonstrates to the user that it's a realistic from price. As oppose to one product that might only be available once, and the likelihood is they'll never get that rate.
The price table is simple.
product_id, price.
It has thousands of rates, and I feel putting the lowest rate using MIN is misleading.
I'm really looking for some example queries of how others might tackle this. Is it possible in a single query?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里我们在cte中使用
rank() over partition by
来对每个productID的记录从低到高进行编号。我们感谢采取排名数高于该文章记录数十分之一的最低价格。如果少于10条记录,则为最低价格。
下面我将来自 CTE 的查询放在它自己的上面,以展示它是如何工作的。最后还有测试模式的 DBfiddle 链接。
db<>fiddle 此处
Here we use
rank() over partition by
in a cte to number the records of each productID from lowest to highest. We thank take thelowest price where the rank number is higher than one tenth of the number of records for that article. If there are less than 10 records it will be the lowest price.
Below I have put the query from the CTE on it's own to show how it works. Finally there is the link to DBfiddle for the test schema.
db<>fiddle here