我在使用
问题在于
以下是一个测试场景,以说明我的意思:
对于这个特定的表,如果我运行以下查询:
我得到了以下的结果:
Group By With Rollup
计算Distinct
的数量时遇到了一个小问题。问题在于
Rollup
汇总只是所有分组中Distinct
值的总数,而不是所有分组的总结。以下是一个测试场景,以说明我的意思:
Create Table #Test
(
GroupId Int Not Null,
Value Int Not Null
)
Insert #Test (GroupId, Value)
Values (1, 1),(1, 2),(1, 3),(1, 4),(1, 5),(1, 5),(1, 1),
(2, 1),(2, 6),(2, 7),(2, 5),(2, 7),(2, 5),
(3, 9),(3, 10),(3, 11),(3, 4),(3, 5),(3, 7),(3, 8),(3, 5),(3, 7),(3, 8)
对于这个特定的表,如果我运行以下查询:
Select Case When Grouping(GroupId) = 1 Then 'Total:' Else Str(GroupId) End As GroupId,
Count(Distinct Value) Count
From #Test
Group By GroupId With Rollup
Order By Grouping(GroupId), GroupId
我得到了以下的结果:
GroupId Count
-------------
1 5
2 4
3 7
Total: 11
我期望总行的结果是16,但实际上我只得到了11——这是所有组中不同值的总数。
如果从查询中移除Distinct
,则可以显示该Rollup
的预期结果:
Select Case When Grouping(GroupId) = 1 Then 'Total:' Else Str(GroupId) End As GroupId,
Count(Value) Count
From #Test
Group By GroupId With Rollup
Order By Grouping(GroupId), GroupId
这将产生以下结果:
GroupId Count
-------------
1 7
2 6
3 10
Total: 23
这符合预期的分组总结。
我的问题是:对于 Count Distinct
, Rollup
是否正常?是否有其他像 Rollup
一样的选项可用于 Grouping
以显示16而不是上面例子中的11个?