我想这可能是你想要的:
SELECT IsNull(EventType,'SUMMARY') as [EventType],
IsNull(EventUserId, 'TOTAL') as [EventUserId],
COUNT(EventUserID) AS [Total]
FROM dbo.vSystemEventLog
GROUP BY EventType, EventUserID With Cube
我不完全确定您想要什么 - 示例输出和/或数据将是最有帮助的。
但是,我猜您需要将其分组,以便查询分组
EventUserId先。
好的 - 所以我已经创建了这个测试数据和 SQL。我想这就是你想要的。
Create Table #t
(
EventType int,
EventUserId int
)
Insert Into #t
Select 100, 18 union all Select 100, 18 union all Select 100, 18
Insert Into #t
Select 101, 16 union all Select 101, 16 union all Select 101, 16
union all Select 101, 16 union all Select 101, 16 union all Select 101, 16
Insert Into #t
Select 101, 18 union all Select 101, 18 union all Select 101, 18 union all Select 101, 18
Insert Into #t
Select 102, 18 union all Select 102, 18
Select IsNull(Convert(varchar(50), EventUserId), 'SUMMARY') As [EventUserId],
IsNull(Convert(varchar(50), EventType), 'TOTAL') as [EventType],
Count(EventUserId) as [Total]
From #t
Group By EventUserId, EventType with cube
Order by 1
drop table #t
这会产生以下输出: