在 Select 语句中使用子查询

发布于 2024-08-18 10:11:35 字数 529 浏览 2 评论 0原文

我有一个输出下表的查询:

(0) Age <= 19----------76-------0.12 

(1) Age 20 – 24--------661------1.06 

(2) Age 25 – 29-------4060------6.53 

(3) Age 30 – 34-------7231------11.64 

(4) Age 35 – 39-------9281------14.94 

(5) Age 40 – 44-------9539------15.35 

Total ----------------30848 -----49.65

第一列是特定段的名称。第二列是该细分市场的人数。

第三列是该部分的人数占整个表的百分比。请注意,总数仅占整个基数的约 50%。

我需要的是另一列,该列是该群体中的人数占该群体的百分比。因此,第一行的公式为 (76/30848)*100,这将给出 76 作为 30848 的百分比。问题是,直到使用 ROLLUP 查询结束时才知道数字 30848。我该怎么做呢?

I've got a query that outputs the following table:

(0) Age <= 19----------76-------0.12 

(1) Age 20 – 24--------661------1.06 

(2) Age 25 – 29-------4060------6.53 

(3) Age 30 – 34-------7231------11.64 

(4) Age 35 – 39-------9281------14.94 

(5) Age 40 – 44-------9539------15.35 

Total ----------------30848 -----49.65

The first column is the name of a particular segment. The second column is the number of people in that segment.

The third column is the number of people in that segment as a percentage of the entire table. Note that the total is only approx 50% of the entire base.

What I need is another column that is the percentage of the people in the segment as a percentage of only this base. So the formula for the first row would be (76/30848)*100 which would give 76 as a percentage of 30848. The problem is that the number 30848 isn't known until the end of the query using ROLLUP. How would I go about doing this?

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

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

发布评论

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

评论(2

以往的大感动 2024-08-25 10:11:35

我更喜欢第一个查询,但您的数据库可能不支持它。

SELECT
  segment,
  COUNT(*) AS people,
  100 * people / SUM(people) OVER () AS percentage
FROM table1
GROUP BY
  segment;

SELECT
  t1.segment,
  COUNT(*) AS people,
  100 * t1.people / t2.total_people AS percentage
FROM table1 t1
CROSS JOIN (
    SELECT
      COUNT(*) AS total_people
    FROM table1) t2
GROUP BY
  segment;

I prefer the first query, but your database may not support it.

SELECT
  segment,
  COUNT(*) AS people,
  100 * people / SUM(people) OVER () AS percentage
FROM table1
GROUP BY
  segment;

SELECT
  t1.segment,
  COUNT(*) AS people,
  100 * t1.people / t2.total_people AS percentage
FROM table1 t1
CROSS JOIN (
    SELECT
      COUNT(*) AS total_people
    FROM table1) t2
GROUP BY
  segment;
你怎么敢 2024-08-25 10:11:35

我对你的回答有点困惑。也许我需要更好地解释一下布局。

该查询会为 20-24 岁和 30-34 岁这两个群体中的任何人生成结果。

SELECT CONCAT('(',s.code,') ',s.description) as `Segment`, count(`Age`) AS `Records Selected`, 
ROUND((count(`Age`)/(SELECT count(*) FROM mainTable))*100, 2) 
AS `Percentage of Total`
FROM segment_fields s, mainTable c 
WHERE `segment` = 'Age' AND `code` = `Age` AND `c`.`Age` IN ('1', '3') 
GROUP BY `Age` WITH ROLLUP

抱歉,它看起来很乱。本质上,我需要在“总计百分比”之后添加一个新列,该列与“总计百分比”相同。但它不会除以整个基数,而是仅除以此查询 ROLLUP 结果的记录数。

I'm a bit confused with your answer. Maybe I need to explain the layout a bit better.

This is a query that produces results for anyone in the two groups aged 20-24 and 30-34.

SELECT CONCAT('(',s.code,') ',s.description) as `Segment`, count(`Age`) AS `Records Selected`, 
ROUND((count(`Age`)/(SELECT count(*) FROM mainTable))*100, 2) 
AS `Percentage of Total`
FROM segment_fields s, mainTable c 
WHERE `segment` = 'Age' AND `code` = `Age` AND `c`.`Age` IN ('1', '3') 
GROUP BY `Age` WITH ROLLUP

Sorry that it looks so messy. Essentially I need a new column just after "percentage of total" that will be the same as "percentage of total". But instead of dividing on the whole base it will only divide by the number of records that are a result of this queries ROLLUP.

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