2013-04-11 99 views
0

这是将每个查询记录在缓存中的表结构。如何计算不同范围和连续范围

SequenceId CacheInstance QueryCondition 
------------------------------------------ 
1   100    'x=1 ' 
2   100    'x=1' 
3   100    'y=a' 
4   100    'x=1' 
5   200    'x=1' 
5   200    'x=1' 

是否有一个简单的语句来获得以下“不同计数”?

CacheInstance QueryCondition distinctcount 
------------------------------------------- 
100   'x=1'    2 
100   'y=a'    1 
200   'x=1'    1 

如果'x = 1'连续出现,则将它计为相同的一个。但是,如果一个不同的查询条件后发生了,重复计数会增加1

回答

2

通过

select CacheInstance,QueryCondition ,COUNT(QueryCondition) as distinctcount from YourtableName group by CacheInstance,QueryCondition 
0

使用组对那些两列试试这个...使用GROUP BY

CREATE TABLE Cache 
(
    SequenceId int, 
    CacheInstance int, 
    QueryCondition nvarchar(20) 
) 

SELECT CacheInstance, QueryCondition, COUNT(QueryCondition) 
FROM Cache 
GROUP BY CacheInstance, QueryCondition