30. April 2010 09:29
Declare @SQL1 nVarchar(max)
Declare @SQL2 nVarchar(max)
Declare @NoActivityDays as nVarchar(4)
set @NoActivityDays = cast( @NoActivity as nVarchar(4))
Set @SQL1 = '
Create table #NARollupIds (
ParentId uniqueidentifier,
ChildId uniqueidentifier
primary key clustered
(
[ParentId],
[ChildId]
)
)
create statistics rupnegacctstat on #NARollupIds(ParentId, ChildId)
declare @DateUTC datetime
set @DateUTC=GetUtcDate()
declare @DateTargetUTC datetime
set @DateTargetUTC=@DateUTC-' + @NoActivityDays + '
insert into #NARollupIds
select accountid as Parentid, accountid as Childid
from (' + @CRM_FilteredAccount + ') as fa
where @DateTargetUTC >= createdonutc
exec p_NeglectedAccountRollup ' + @SubAccounts + ', 3, ' + @SubEntities + ', 3
Declare @TempDel table(
Parentid uniqueidentifier
)
insert into @TempDel
select acct.ParentId as accountid
from (' + @CRM_FilteredActivityPointer + ') as ap, #NARollupIds acct
where ap.regardingobjectid = acct.ChildId and
ap.statecode = 1 and
ap.actualendutc >= @DateTargetUTC
group by acct.ParentId
delete from #NARollupIds where ParentId in (select Parentid from @TempDel)
Declare @Temp table(
Accountid uniqueidentifier,
DaysWithoutActivity integer,
LastActivityDate DateTime,
AccountCategoryCode integer,
AccountCategoryCodeName nVarchar(max),
Ownerid uniqueidentifier,
OwneridName nVarchar(max),
Braket nVarchar(10)
)
insert into @Temp
SELECT qq.accountid, qq.DaysWithoutActivity, qq.LastActivityDate,
IsNull(acct.accountcategorycode, -1) as accountcategorycode,
IsNull(acct.accountcategorycodename, ''_CRM_NOTSPECIFIED'') as accountcategorycodename,
acct.ownerid, IsNull(acct.owneridname, ''_CRM_NOTSPECIFIED'') as owneridname,
dbo.fn_RptBracket(DaysWithoutActivity, ' + @NoActivityDays + ') as braket
FROM (
select acct.ParentId as accountid,
DateDiff(day, max(isnull(ap.actualendutc,facct.createdonutc)),@DateUTC) as DaysWithoutActivity,
max(isnull(ap.actualendutc,facct.createdonutc)) as LastActivityDate
from FilteredAccount as facct, #NARollupIds as acct left join (' + @CRM_FilteredActivityPointer + ') as ap on ap.regardingobjectid = acct.ChildId
where
((ap.regardingobjectid = acct.ChildId and ap.statecode = 1)
or ap.statecode is null) and facct.accountid = acct.ParentId
group by acct.ParentId
) as qq
join FilteredAccount acct on acct.accountid = qq.accountid '
If @GroupBy = 'ownerid'
set @SQL2 = '
Declare @Top15 table(GroupById uniqueidentifier, GroupByIdName nVarchar(max), accountcount int)
Insert into @Top15
Select Top 15 Ownerid,OwneridName, Count(*)
From @Temp
Group by Ownerid,OwneridName
order by Count(*) Desc, OwneridName asc
Declare @Total integer
select @Total = count(*) From @Temp
select * from (
select cast(GroupById as nVarchar(50)) as GroupById, GroupByIdName, accountcount from @Top15
Union all
Select ''_CRM_OTHER'' , ''_CRM_OTHER'', @Total - (select sum(accountcount) from @Top15)) as qq
where accountcount <> 0
Drop table #NARollupIds
'
else If @GroupBy = 'accountcategorycode'
set @SQL2 = '
Select AccountCategoryCode as GroupById,AccountCategoryCodeName as GroupByIdName, Count(*) as accountcount
From @Temp
Group by AccountCategoryCode,AccountCategoryCodeName
order by Count(*) Desc
Drop table #NARollupIds
'
else If @GroupBy = 'Bracket'
set @SQL2 = '
Select Braket as GroupById,Braket as GroupByIdName, Count(*) as accountcount
From @Temp
Group by Braket
order by Count(*) Desc
Drop table #NARollupIds
'
Exec(@SQL1 + @SQL2)
1. Mai 2010 08:57
3. Mai 2010 09:35