Tsql 如何使用汇总结果确定百分比
我试图使用With Rollup函数来确定查询中返回的行总数,并使用该数字来确定每个结果相对于查询中总行数的百分比。 这是我的密码:Tsql 如何使用汇总结果确定百分比,tsql,Tsql,我试图使用With Rollup函数来确定查询中返回的行总数,并使用该数字来确定每个结果相对于查询中总行数的百分比。 这是我的密码: Declare @startDate DateTime, @endDate DateTime; Set @startDate = '01/01/01' Set @endDate = '01/01/13' SELECT isnull(EducationType.EducationTypeDesc, 'UNKNOWN') as 'Education Type D
Declare @startDate DateTime, @endDate DateTime;
Set @startDate = '01/01/01'
Set @endDate = '01/01/13'
SELECT
isnull(EducationType.EducationTypeDesc, 'UNKNOWN') as 'Education Type Description',
COUNT(isnull(EducationType.EducationTypeDesc, 'UNKNOWN')) as 'Record Count'
FROM
EducationType LEFT OUTER JOIN
Education ON EducationType.EducationTypeID = Education.EducationTypeID LEFT OUTER JOIN
Volunteer ON Volunteer.PartyID = Education.PartyID
WHERE (Volunteer.SwornDate BETWEEN @startDate AND @endDate)
GROUP BY isnull(EducationType.EducationTypeDesc, 'UNKNOWN')
WITH ROLLUP
ORDER BY isnull(EducationType.EducationTypeDesc, 'UNKNOWN')
我在EducationType.EducationTypeDesc显示为NULL但无法计算查询中百分比的记录中获取总计数
谢谢
Andy从查询的未分组版本加载行计数,然后计算百分比:
Declare @startDate DateTime, @endDate DateTime;
Set @startDate = '01/01/01';
Set @endDate = '01/01/13';
DECLARE @rows MONEY;
SELECT @rows=COUNT(1)
FROM
EducationType LEFT OUTER JOIN
Education ON EducationType.EducationTypeID = Education.EducationTypeID LEFT OUTER JOIN
Volunteer ON Volunteer.PartyID = Education.PartyID
WHERE (Volunteer.SwornDate BETWEEN @startDate AND @endDate);
SELECT
isnull(EducationType.EducationTypeDesc, 'UNKNOWN') as 'Education Type Description',
COUNT(isnull(EducationType.EducationTypeDesc, 'UNKNOWN')) as 'Record Count',
COUNT(isnull(EducationType.EducationTypeDesc, 'UNKNOWN')) / @rows * 100 as 'Percent'
FROM
EducationType LEFT OUTER JOIN
Education ON EducationType.EducationTypeID = Education.EducationTypeID LEFT OUTER JOIN
Volunteer ON Volunteer.PartyID = Education.PartyID
WHERE (Volunteer.SwornDate BETWEEN @startDate AND @endDate)
GROUP BY isnull(EducationType.EducationTypeDesc, 'UNKNOWN')
WITH ROLLUP
ORDER BY isnull(EducationType.EducationTypeDesc, 'UNKNOWN');
不客气,请通过以下答案选中已接受复选框:)