好的,所以我正在尝试为今天、上周、上个月和本周的表中的每种消息类型创建一个 TSQL 结果集。不同的消息类型是 101,103,202。
以下 sql 产生我需要的输出,但我不知道如何根据三种消息类型过滤结果。
Select distinct
( Select Count(*) from MsgDateDetail
Where convert(date,MsgDate,103) = Convert(date,GETDATE())
And SenderBIC = @senderbic) As Today,
( Select Count(*) from MsgDateDetail
Where MsgDate Between DATEADD(dd, -(DATEPART(dw, GetDate())-1)-7, GetDate())
And DATEADD(dd, 7-(DATEPART(dw, GetDate()))-7, GetDate())
And SenderBIC = @senderbic) As LastWeek,
( Select Count(*) FROM MsgDateDetail
Where convert(date,MsgDate,103) = (DATEADD(mm,-1, Convert(date,GETDATE())))
And SenderBIC = @senderbic) AS LastMonth,
( Select Count(*) from MsgDateDetail
Where MsgDate between (DATEADD(week, DATEDIFF(week,0,GETDATE()),-1)) AND getDate()
And SenderBIC = @senderbic) AS ThisWeek
FROM MsgDateDetail h1
*编辑 *
发布来自 Lolo 的结果