0

With Rollup 関数を使用して、クエリで返される行の TOTAL 数を特定し、その数を使用して、クエリの TOTAL 行に対する各結果のパーセンテージを特定しようとしています。これが私のコードです:

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 を示しているレコードの合計数を取得しますが、クエリでパーセンテージを計算する方法がわかりません。

ありがとうアンディ

4

1 に答える 1

0

グループ化されていないバージョンのクエリから行数を読み込み、パーセンテージを計算します。

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');
于 2012-05-20T14:39:22.683 に答える