MySQL查询表中大多数价格的最低价格

发布于 2025-01-15 05:06:14 字数 308 浏览 3 评论 0原文

我正在尝试构建一个查询,通过该查询可以提取表中的最低价格,该价格至少为表中所有价格的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

漫雪独思 2025-01-22 05:06:14

这里我们在cte中使用rank() over partition by来对每个productID的记录从低到高进行编号。我们感谢采取
排名数高于该文章记录数十分之一的最低价格。如果少于10条记录,则为最低价格。
下面我将来自 CTE 的查询放在它自己的上面,以展示它是如何工作的。最后还有测试模式的 DBfiddle 链接。

与 cte 作为
(选择
  产品编号,
  价格,
  排名()超过(按productid分区
       按价格排序) as rn,
  计数(productid)超过(分区
       按产品ID) cn
来自产品)
选择
  产品编号,
  最低(价格)最低10
来自 cte
在哪里
rn> cn/10
按产品编号分组;
产品ID |低10
--------: | ----:
        1 | 24
        2 | 42
        3 | 56
        4 | 66
        5 | 99

<前><代码>选择
产品编号,
价格,
排名()超过(按productid分区
按价格排序) as rn,
计数(productid)超过(分区
按产品ID)作为 cn
来自产品
按产品编号、价格排序;

产品ID |价格| RN |中文
--------: | ----: | -: | -:
        1 | 24 | 1 | 9
        1 | 32 | 32 2 | 9
        1 | 43 | 43 3 | 9
        1 | 45 | 45 4 | 9
        1 | 56 | 56 5 | 9
        1 | 56 | 56 5 | 9
        1 | 56 | 56 5 | 9
        1 | 85 | 85 8 | 9
        1 | 87 | 87 9 | 9
        2 | 34 | 34 1 | 11
        2 | 42 | 42 2 | 11
        2 | 44 | 44 3 | 11
        2 | 47 | 47 4 | 11
        2 | 55 | 55 5 | 11
        2 | 56 | 56 6 | 11
        2 | 57 | 57 7 | 11
        2 | 67 | 67 8 | 11
        2 | 74 | 74 9 | 11
        2 | 75 | 75 10 | 10 11
        2 | 87 | 87 11 | 11 11
        3 | 56 | 56 1 | 3
        3 | 67 | 67 2 | 3
        3 | 77 | 77 3 | 3
        4 | 66 | 66 1 | 1
        5 | 99 | 99 1 | 1

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 the
lowest 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.

with cte as
(select
  productid,
  price,
  rank() over (partition by productid
       order by price asc) as rn,
  count(productid) over (partition
       by productid) cn
from products)
select
  productid,
  min(price) low10
from cte
where
rn > cn/10
group by productid;
productid | low10
--------: | ----:
        1 |    24
        2 |    42
        3 |    56
        4 |    66
        5 |    99
select
  productid,
  price,
  rank() over (partition by productid
       order by price asc) as rn,
  count(productid) over (partition 
        by productid) as cn
from products
order by productid,price;
productid | price | rn | cn
--------: | ----: | -: | -:
        1 |    24 |  1 |  9
        1 |    32 |  2 |  9
        1 |    43 |  3 |  9
        1 |    45 |  4 |  9
        1 |    56 |  5 |  9
        1 |    56 |  5 |  9
        1 |    56 |  5 |  9
        1 |    85 |  8 |  9
        1 |    87 |  9 |  9
        2 |    34 |  1 | 11
        2 |    42 |  2 | 11
        2 |    44 |  3 | 11
        2 |    47 |  4 | 11
        2 |    55 |  5 | 11
        2 |    56 |  6 | 11
        2 |    57 |  7 | 11
        2 |    67 |  8 | 11
        2 |    74 |  9 | 11
        2 |    75 | 10 | 11
        2 |    87 | 11 | 11
        3 |    56 |  1 |  3
        3 |    67 |  2 |  3
        3 |    77 |  3 |  3
        4 |    66 |  1 |  1
        5 |    99 |  1 |  1

db<>fiddle here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文