UFN_STEPSUMMARY_COMPLETED_3

Return

Return Type
table

Parameters

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

Definition

Copy


            CREATE function dbo.UFN_STEPSUMMARY_COMPLETED_3
            (
                @STARTDATE datetime
                @INCLUDESTEWARDSHIP bit,
                @ONLYOWNEDINTERACTIONS bit = 0,
                @ENDDATE datetime = null
            )
            returns table
            as return (
                with INCLUDEGRANTS_CTE as
        (
                    select dbo.UFN_GETINCLUDEGRANTS() as INCLUDEGRANTS
                )
                select
                    cast(DATA.ID as uniqueidentifier) as ID,
                    cast(count(*) as int) as COUNT
                from
                (
                    select 
                        INTERACTION.FUNDRAISERID as ID
                    from 
                        dbo.INTERACTION
                        cross join INCLUDEGRANTS_CTE
                    where 
                        INTERACTION.COMPLETED = 1
                        and INTERACTION.DATE >= @STARTDATE
                        and (INTERACTION.DATE <= @ENDDATE or @ENDDATE 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 = 1
                        and INTERACTION.DATE >= @STARTDATE
                        and (INTERACTION.DATE <= @ENDDATE or @ENDDATE 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 = 1
                        and STEP.ACTUALDATE >= @STARTDATE

                ) as DATA

                group by
                    DATA.ID
            )