2015-12-01 83 views
0

問候我已經有喜歡集團由主要計數

select top 100 tab.user as username, 
case tab.event 
    when 1 then 'Add' 
    when 2 then 'Remove' 
    when 3 then 'Update' 
end as event, 
count(tab.countOfEvents) as count_events 
from history tab 
where tab.even is not null and tab.date <= DATEADD(day, 1, getdate()) 
group by tab.event, tab.user 
order by count_events desc 

即時得到結果,如SQL一個問題:

user event count_event 
    a Update 100 
    b Update 89 
    a Add  19 
    c Remove 18 
    b Add  10 
    a Remove 9 

現在是可能的是,我可以得到結果通過highes用戶活動排序(總和的activieties),但還是由用戶來分類(我需要事件名稱)例如:

user event count_event 
    a Update 100 
    a Add  19 
    a Remove 9 
    b Update 89 
    b Add  10 
    c Remove 18 

    etc ... 
+0

order by user,count_events desc – jarlh

+0

您是否在搜索'order asc,count_events desc'? –

+0

我無法在任何地方看到用戶活動字段:( – Arvo

回答

3

你只count_ev有序ents字段。

如果你想改變順序標準,改變這種:

order by count_events desc 

order by tab.user, count_events desc 

這樣,你會在第一用戶,然後通過count_events訂購。