在 Select 语句中使用子查询
我有一个输出下表的查询:
(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我更喜欢第一个查询,但您的数据库可能不支持它。
I prefer the first query, but your database may not support it.
我对你的回答有点困惑。也许我需要更好地解释一下布局。
该查询会为 20-24 岁和 30-34 岁这两个群体中的任何人生成结果。
抱歉,它看起来很乱。本质上,我需要在“总计百分比”之后添加一个新列,该列与“总计百分比”相同。但它不会除以整个基数,而是仅除以此查询 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.
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.