日期:2014-05-17 浏览次数:20635 次
declare
@UserId int,
@Category int,
@IncludeHandleStatus int,
@ExceptHandleStatus int,
@IncludeStatus int,
@ExceptStatus int,
@StartDate datetime,
@EndDate datetime
select nm2.MessageId,nm2.Category,nm2.ReferenceId,nm2.UserId from dbo.NotifyMessage nm2,
(
select nm1.MessageId
from dbo.NotifyMessage nm1
where --先行 过滤 时间、用户、分类、状态
(@StartDate is null or @StartDate<=nm1.CreateDate) And
(@EndDate is null or @EndDate>=nm1.CreateDate) And
(@Category is null or nm1.Category&@Category=@Category) And
(@UserId is null or @UserId=nm1.UserId) And
nm1.HandleStatus&@IncludeHandleStatus=@IncludeHandleStatus And
nm1.[Status]&@IncludeStatus=@IncludeStatus
) tmp1
where nm2.MessageId=tmp1.MessageId and
not exists
(
select 1 from dbo.NotifyMessage nm1
where
nm1.UserId=nm2.UserId and
nm1.Category=nm2.Category and nm1.ReferenceId=nm2.ReferenceId --以 分类及关联ID 分组
and
(
(@ExceptHandleStatus<>0 And nm1.HandleStatus&@ExceptHandleStatus<>0) or
(@ExceptStatus<>0 And nm1.[Status]&@ExceptStatus<>0)
)
)
CREATE NONCLUSTERED INDEX [NotifyMessage_Indexer] ON [dbo].[StoreToolsCommon_NotifyMessage]
(
[CreateDate] ASC,
[UserID] ASC,
[Category] ASC,
[ReferenceId] ASC,
[Status] ASC,
[HandleStatus] ASC
)