UFN_REPORT_VSECOUNTS
Returns constituent and solicit counts for the vse report.
Return
Return Type |
---|
table |
Parameters
Parameter | Parameter Type | Mode | Description |
---|---|---|---|
@STARTDATE | datetime | IN | |
@ENDDATE | datetime | IN | |
@INCLUDEINACTIVE | smallint | IN | |
@INCLUDEDECEASED | smallint | IN | |
@USAGETYPE_EXCLUDE | smallint | IN |
Definition
Copy
CREATE function dbo.UFN_REPORT_VSECOUNTS
(
@STARTDATE datetime = null,
@ENDDATE datetime = null,
@INCLUDEINACTIVE smallint = 0,
@INCLUDEDECEASED smallint = 0,
@USAGETYPE_EXCLUDE smallint = 0
)
returns table as
return (
select distinct
vc.vseconstituencycode,
case when @USAGETYPE_EXCLUDE = 0 then null else academiccatalogprogram.programcode end programcode,
count(distinct cc.constituentid) "numrecords",
count(distinct ca.constituentid) "numsolicit"
from dbo.vseconstituencymap vsemap
inner join dbo.vseconstituency vc on vc.id = vsemap.VSECONSTITUENCYID
inner join dbo.UFN_REPORT_VALIDCONSCODE(@STARTDATE, @ENDDATE) cc on cc.id = vsemap.constituencyid
inner join dbo.constituent cons on cons.id = cc.constituentid
left outer join dbo.constituentappeal ca on ca.constituentid = cons.id
left outer join dbo.deceasedconstituent dcons on dcons.id = cons.id
left outer join dbo.educationalhistory on cons.id = educationalhistory.constituentid
left outer join dbo.academiccatalogprogram on educationalhistory.academiccatalogprogramid = academiccatalogprogram.id
where vc.usagetypecode <> @USAGETYPE_EXCLUDE and
vc.typecode = (case when cons.isorganization = 0 then 0 else 1 end)
and vc.sequence =
(select min(vc2.sequence)
from dbo.vseconstituencymap vsemap2
inner join dbo.vseconstituency vc2 on vc2.id = vsemap2.VSECONSTITUENCYID
inner join dbo.UFN_REPORT_VALIDCONSCODE(@STARTDATE, @ENDDATE) cc2
on cc2.id = vsemap2.constituencyid
inner join dbo.constituent cons2 on cons2.id = cc2.constituentid
where
vc2.typecode = (case when cons2.isorganization = 0 then 0 else 1 end) and
vc2.usagetypecode <> @USAGETYPE_EXCLUDE and cc2.constituentid = cc.constituentid)
--and case when @INCLUDEINACTIVE <> 0 then 0 else cons.isinactive end = 0
and cons.isinactive in (0, @INCLUDEINACTIVE)
and case when @INCLUDEDECEASED <> 0 then
cons.id
else (case when dcons.id is null then cons.id else null end) end = cons.id
group by vc.vseconstituencycode, case when @USAGETYPE_EXCLUDE = 0 then null else academiccatalogprogram.programcode end
)