UFN_STEPSUMMARY_PLANNED_4

Return

Return Type
table

Parameters

Parameter Parameter Type Mode Description
@ENDDATE datetime IN
@ONLYOWNEDINTERACTIONS bit IN
@STARTDATE datetime IN
@INCLUDESTEWARDSHIP bit IN

Definition

Copy


            CREATE function dbo.UFN_STEPSUMMARY_PLANNED_4
            (
                @ENDDATE datetime,
                @ONLYOWNEDINTERACTIONS bit = 0,
                @STARTDATE datetime = null,
                @INCLUDESTEWARDSHIP bit = 0
--The _4 version of this function has been released to 3.0 SP 9.  If you are adding parameters or otherwise doing something that would require

--a new numbered version you need to update the version even in 4.0.  If you update the number or 4.0 has been released this comment can be removed

            )
            returns table
            as return (
                with INCLUDEGRANTS_CTE as
        (
          select dbo.UFN_GETINCLUDEGRANTS() as INCLUDEGRANTS
                )
                select
                    DATA.ID,
                    count(*) as COUNT
                from
                (
                    select
                        INTERACTION.FUNDRAISERID as ID
                    from 
                        dbo.INTERACTION
                        cross join INCLUDEGRANTS_CTE
                    where 
                        INTERACTION.COMPLETED = 0 
                        and INTERACTION.DATE < @ENDDATE
                        and (INTERACTION.DATE >= @STARTDATE or @STARTDATE is null)
                        and (
                            INTERACTION.PROSPECTPLANID is not null
                            or (INCLUDEGRANTS_CTE.INCLUDEGRANTS = 1 and INTERACTION.FUNDINGREQUESTID is not null)
                        )

                    union all

                    select
                        INTERACTIONADDITIONALFUNDRAISER.FUNDRAISERID as ID
                    from 
                        dbo.INTERACTION
                        left join dbo.INTERACTIONADDITIONALFUNDRAISER on INTERACTION.ID = INTERACTIONADDITIONALFUNDRAISER.INTERACTIONID
                        cross join INCLUDEGRANTS_CTE
                    where 
                        @ONLYOWNEDINTERACTIONS = 0 
                        and INTERACTION.COMPLETED = 0 
                        and INTERACTION.DATE < @ENDDATE
                        and (INTERACTION.DATE >= @STARTDATE or @STARTDATE is null)
                        and (
                            INTERACTION.PROSPECTPLANID is not null
                            or (INCLUDEGRANTS_CTE.INCLUDEGRANTS = 1 and INTERACTION.FUNDINGREQUESTID is not null)
                        )

                    union all

                    select
                        STEP.CONSTITUENTID as ID
                    from
                        dbo.STEWARDSHIPPLANSTEP STEP                
                    where
                        @INCLUDESTEWARDSHIP = 1
                        and not STEP.CONSTITUENTID is null
                        and STEP.COMPLETED = 0
                        and STEP.TARGETDATE < @ENDDATE
                        and (STEP.TARGETDATE >= @STARTDATE or @STARTDATE is null)

                ) as DATA

                group by
                    DATA.ID
            )