我需要写一个proc,它给我报告我所有的学生,男性和女性但是每个国家?

时间:2016-12-13 07:25:27

标签: sql-server stored-procedures report

我的查阅数据表中有我的学生数据(已注册,已删除,已插入,所有内容均为ldTypeID = 129)。我的其余数据存储在vStudent中。我需要写proc告诉我所有学生,男性和女性的报告,但每个国家?!我错了我的代码吗?!

学生身份总数#男性总数#女性

已登记
掉了出来 取消
毕业

CREATE PROCEDURE [dbo]。[pr_ReportPerCountry]     @CountryID int     AS

DELETE FROM ReportData WHERE rdTypeID = 2 

INSERT INTO ReportData
    (
    rdSortBy,
    rdTypeID,
    rdName1,
    rdValue1,
rdValue2,
rdValue3,
    )

Select 
        0 AS SoryBy,
        2 AS rdTypeID,
        finalStudentStatus,
sum (case when peFinalStatusID in (303) then 1 else 0 end) as TotalEnrolled,
sum(case when (peFinalStatusID in (303) and peSexID=7) then 1 else 0 end) as MaleEnrolled,
sum(case when (peFinalStatusID in (303) and peSexID=8) then 1 else 0 end) as FemaleEnrolled
from vStudentPerson
left join (SELECT ldLookupDataID,ldValue FROM LookupData 
                    WHERE ldTypeID = 129 
                    group by ldLookupDataID,ldValue
                    ) sta on peFinalStatusID=sta.ldLookupDataID 
WHERE peFinalStatusID in (297,298,299,303) and peCountryofResidencyID = 0 or peCountryofResidencyID=@CountryID

我找到了解决方案,无论如何,谢谢。

INSERT INTO ReportData
    (
    rdSortBy,
    rdTypeID,
    rdName1,
    rdValue1, 
    rdValue2,
    rdValue3
    )
Select 
        0 AS SoryBy,
        1 AS rdTypeID,
        academicYear,
        count (pePersonID) as TotalAwarded,
        sum (case when peSexID = 7 then 1 else 0 end ) as TotalAwardedMale,
        sum (case when peSexID = 8 then 1 else 0 end ) as TotalAwardedFemale
from vStudentPerson
left join (SELECT * FROM vStudentPersonScholarship) sch on pePersonID=sch.psPersonID
where (@CountryID = -1 or peCountryofResidencyID = @CountryID) and psScholarahipRenewalsTypeID=281
group by sch.academicYear

0 个答案:

没有答案