sql - 在 SQL Server 中根据列总和查找百分比
问题描述
链接到我想要的图片 大家好,我需要有关在列上查找百分比的帮助。我想根据下面示例数据中的基列查找每行的百分比:
CREATE TABLE [dbo].[TestData](
[Selection] [nvarchar](150) NULL,
[Criteria] [nvarchar](500) NULL,
[Heading] [nvarchar](550) NOT NULL,
[Base] [int] NULL,
[WhatImGetting] [numeric](5, 0) NULL,
[WhatIWant] [numeric](5, 0) NULL
) ON [PRIMARY]
GO
INSERT [dbo].[TestData] ([Selection], [Criteria], [Heading], [Base], [WhatImGetting], [WhatIWant]) VALUES (N'All', N'All', N'', 5, CAST(100 AS Numeric(5, 0)), CAST(100 AS Numeric(5, 0)))
GO
INSERT [dbo].[TestData] ([Selection], [Criteria], [Heading], [Base], [WhatImGetting], [WhatIWant]) VALUES (N'Gender', N'Male', N'', 5, CAST(100 AS Numeric(5, 0)), CAST(100 AS Numeric(5, 0)))
GO
INSERT [dbo].[TestData] ([Selection], [Criteria], [Heading], [Base], [WhatImGetting], [WhatIWant]) VALUES (N'Location', N'Abuja', N'', 2, CAST(40 AS Numeric(5, 0)), CAST(40 AS Numeric(5, 0)))
GO
INSERT [dbo].[TestData] ([Selection], [Criteria], [Heading], [Base], [WhatImGetting], [WhatIWant]) VALUES (N'Location', N'Kano', N'', 3, CAST(60 AS Numeric(5, 0)), CAST(60 AS Numeric(5, 0)))
GO
INSERT [dbo].[TestData] ([Selection], [Criteria], [Heading], [Base], [WhatImGetting], [WhatIWant]) VALUES (N'MaritalStatus', N'Married', N'', 2, CAST(40 AS Numeric(5, 0)), CAST(40 AS Numeric(5, 0)))
GO
INSERT [dbo].[TestData] ([Selection], [Criteria], [Heading], [Base], [WhatImGetting], [WhatIWant]) VALUES (N'MaritalStatus', N'Single', N'', 3, CAST(60 AS Numeric(5, 0)), CAST(60 AS Numeric(5, 0)))
GO
INSERT [dbo].[TestData] ([Selection], [Criteria], [Heading], [Base], [WhatImGetting], [WhatIWant]) VALUES (N'Question1', N'3yrs – 4 yrs.', N'Q1. How long has this business existed?', 3, CAST(60 AS Numeric(5, 0)), CAST(75 AS Numeric(5, 0)))
GO
INSERT [dbo].[TestData] ([Selection], [Criteria], [Heading], [Base], [WhatImGetting], [WhatIWant]) VALUES (N'Question1', N'16yrs – 20yrs', N'Q1. How long has this business existed?', 1, CAST(20 AS Numeric(5, 0)), CAST(25 AS Numeric(5, 0)))
GO
这是我的示例代码
with cte as (
Select
Selection,
Criteria,
Heading,
Base
from TestData
)
Select Selection,
Criteria,
Heading,
Base,
cast(round(Base * 100.0/Nullif(
(select
SUM(case when Selection = 'Gender' then Base Else 0 End)
from cte),0),0) as numeric(5,0)) Male
from cte
其他都是正确的,到了选择下的Question1时计算错误,显然所有的计算都是错误的。
显然我想做的是根据选择列中的值对基数求和,并使用求和来计算百分比,例如对于总和为 4 的 Question1,百分比将为 Base * 100/4,这将给我 25% 和75%
谢谢
蒂姆
解决方案
The issue is, you have sum
on base
column when selection is gender
which will be equal to 5.
SUM(case when Selection = 'Gender' then Base Else 0 End
现在,当您计算基数为 3 的“问题 1 - 3 年 - 4 年”的百分比时,它将达到 60%,而不是您预期的 75%。(3*100)/5
您假设SUM
通过对选择进行分组并将其用作总数来计算组内每一行的百分比。我已经修改了您的查询,如下所示,这应该可以按照您的问题的预期工作
with cte as (
Select
Selection,
SUM(BASE) as SelSum -- Sum for each selection group
from TestData
group by selection
)
Select t.Selection,
Criteria,
Heading,
Base,
cast(round(Base * 100.0/Nullif(c.SelSum,0),0) as numeric(5,0)) Male
from TestData t
inner join cte c on t.selection = c.selection
Selection Criteria Heading Base Male
All All 5 100
Gender Male 5 100
Location Abuja 2 40
Location Kano 3 60
MaritalStatus Married 2 40
MaritalStatus Single 3 60
Question1 3yrs – 4 yrs. Q1. How long has this business existed? 3 75
Question1 16yrs – 20yrs Q1. How long has this business existed? 1 25
推荐阅读
- python - Django pk 在锚标记中无法正常工作
- typescript - 将类型定义为包含所有且仅包含联合类型的成员
- c - 找出与 sendto 函数中使用的套接字关联的 src IP 和端口
- java - 如果我们可以使用@component,bean 工厂有什么用?
- ruby - 如何在 Haml 部分中为 Google Analytics 跟踪 ID 使用环境变量
- spring-boot - org.elasticsearch.client.Cancellable 的类文件未找到
- c# - 任务中的收益回报
> - react-native - React Native use_native_modules 文档
- c# - 使用 GhostScript 将 eps 转换为 svg 的 Magic.Net 在 Illustrator 中为空白
- node.js - 如何在nodejs中进行mongoexport以在mongodb atlas中收集