您将如何实现这个查询?记录集或 Sql

发布于 2024-11-15 05:52:08 字数 915 浏览 6 评论 0原文

我刚刚在几天前的面试测试中看到这个问题,我有下表,

Region Company Sales ratio_of_sales_in_region  percentile_in_region
 NA     A1      1000            0.25                25
 NA     A2      1000             0.25                50
 NA     A3      1000             0.25                75
 NA     A4      1000             0.25                100 
 EU     B1      2000            0.5                  50
 EU     B2      1000             0.25                75
 EU     B3      1000             0.25                100
.......

我需要提取每个地区第 30 个百分位的公司和销售额 结果将是

Region  30th_percentile_company  30th_percentile_sales
NA           A2                       (1000*0.25 + 500 * 0.05)
EU           B1                        2000    (as B1 accounts for more than 30%)

查询需要检查上述条件,例如一家公司是否已占 30% 以上,并计算每个地区 30% 销售额的权重。

编辑:我试图通过添加新列来解释百分位数的含义。我很困惑,但我看到了所询问的结果表,它清楚地说明了 30% 的含义

I just saw this question in an interview test I took few days back, I have the following table

Region Company Sales ratio_of_sales_in_region  percentile_in_region
 NA     A1      1000            0.25                25
 NA     A2      1000             0.25                50
 NA     A3      1000             0.25                75
 NA     A4      1000             0.25                100 
 EU     B1      2000            0.5                  50
 EU     B2      1000             0.25                75
 EU     B3      1000             0.25                100
.......

I need to extract the 30th percentile company and sales for each region
the result would be

Region  30th_percentile_company  30th_percentile_sales
NA           A2                       (1000*0.25 + 500 * 0.05)
EU           B1                        2000    (as B1 accounts for more than 30%)

The query would need to check for the above conditions like does a company already account for more than 30% and also take the weight for 30 percentile sales for each region.

EDIT : I have tried to explain what percentile means by adding a new column. I was confused but I saw the result table that was asked and it made it clear as to what they meant by 30th percentile

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

聚集的泪 2024-11-22 05:52:08
SELECT
   Region,
   MIN(Company) as [30th_percentile_company],  --potentially, two companies would from the same region would have the exact same percentile_in_region.
FROM
(
SELECT
   Region,
   MIN(percentile_in_region) as percentile_in_region
WHERE
   percentile_in_region > 30
GROUP BY
   Region
) a

INNER JOIN
   TableName T1
ON
   T1.Region = a.Region
   AND T1.percentile_in_region = a.precentile_in_region

GROUP BY
   Region
SELECT
   Region,
   MIN(Company) as [30th_percentile_company],  --potentially, two companies would from the same region would have the exact same percentile_in_region.
FROM
(
SELECT
   Region,
   MIN(percentile_in_region) as percentile_in_region
WHERE
   percentile_in_region > 30
GROUP BY
   Region
) a

INNER JOIN
   TableName T1
ON
   T1.Region = a.Region
   AND T1.percentile_in_region = a.precentile_in_region

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