在SQL Server中基于列总和查找百分比

时间:2018-08-10 21:45:28

标签: sql sql-server common-table-expression

Link to Image of what i want 大家好 请我需要有关在列上找到百分比的帮助。我想根据下面的示例数据中的“基础列”找到每一行的百分比:

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

其他都是正确的,它在选择问题1到Question1时,显然所有计算都是错误的。

显然,我要做的是基于“选择”列中的值求和并使用总和来计算百分比 例如,问题1的总和为4,则百分比为基数* 100/4 这将使我分别占25%和75%

谢谢

蒂姆

1 个答案:

答案 0 :(得分:1)

问题是,当选择为sum等于5时,您在base列上有gender

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

Output

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